How can the ruby mongo driver not be used in a SIGNAL/trap context, rails (latest) and minitest (latest) both trap

Viewed 83

I am have a problem with the latest version of ruby 3.1.1p18, Rails 7.0.2.2, Minitest 5.15.0, and mongo 2.17

The problem is that mongo calls synchronize which in minitest is called in the context of a ruby trap (Signal handler). This causes the following error:

/home/deployer/.bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb:21: warning: Exception in finalizer #<Proc:0x00007f4d81d10e90 bundle/ruby/3.1.0/gems/mongo-2.17.0/lib/mongo/cursor.rb:114>
bundle/ruby/3.1.0/gems/mongo-2.17.0/lib/mongo/cluster/reapers/cursor_reaper.rb:59:in `synchronize': can't be called from trap context (ThreadError)
        from bundle/ruby/3.1.0/gems/mongo-2.17.0/lib/mongo/cluster/reapers/cursor_reaper.rb:59:in `schedule_kill_cursor'q
        from bundle/ruby/3.1.0/gems/mongo-2.17.0/lib/mongo/cluster.rb:350:in `block (2 levels) in <class:Cluster>'
        from bundle/ruby/3.1.0/gems/mongo-2.17.0/lib/mongo/cursor.rb:115:in `block in finalize'

There is a stackoverflow question addressing the root cause of this issue:

MongoDB Ruby driver - `synchronize': can't be called from trap context which says that you can't call synchronize instead of a trap('?') do ... end.

However the 2 main issues appear to be that

Minitest traps signals via

Signal handler: https://github.com/seattlerb/minitest/blob/fe3992e85b40792cf7bff2a876887d8d9e392068/lib/minitest.rb#L368

Rails traps signals via: https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/cli.rb#L10 & https://github.com/rails/rails/blob/346ae79d5a11152f508df8d5506f8a0682ddb74b/railties/lib/rails/commands/server/server_command.rb#L33

I could monkey patch Rails & minitest to no longer trap which feels very wrong and counter productive in some cases.

Attempting to modify the mongo ruby driver code is counter productive, since that would imply threading is impossible under rails and minitest because code therein MAY use threading

Does anyone have any thoughts? How to get out of this catch-22 between threading and traps?

Thank you in advance!

-daniel

0 Answers
Related