Skip to content

Using at_exit and Signal.trap possibly creates a race-condition #5437

@Adithya-copart

Description

@Adithya-copart

Environment

Provide at least:

  • JRuby version: jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 Java HotSpot(TM) 64-Bit Server VM 25.181-b13 on 1.8.0_181-b13 +jit [darwin-x86_64]
  • Operating system and platform (e.g. uname -a): Darwin MB-ADPENTELA.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
    Other relevant info you may wish to add:
  • Installed or activated gems
  • Application/framework version (e.g. Rails, Sinatra)
  • Environment variables

Expected Behavior

  • The Signal.trap and at_exit hooks are executed in different threads leading to a race-condition where both at_exit and Signal.trap are defined.

Example: Puma traps SIGINT and sucker_punch uses the at_exit hook leading to a SystemExit before sucker_punch workers are shutdown.
puma/puma#1675
brandonhilkert/sucker_punch#223

Actual Behavior

MRI is using the same thread for both at_exit and Signal.trap. It would be nice if there is some synchronization.

### test.rb
at_exit do
  sleep(1)
  puts "Thread in at_exit: #{Thread.current}"
end

Signal.trap("SIGINT") do
 puts "Thread in trap: #{Thread.current}"
 exit
end

sleep

## JRuby
$ RUBYOPT=-d jruby test_in_jruby.rb 
Exception `Gem::MissingSpecError' at /Users/adpentela/.rvm/rubies/jruby-9.2.0.0/lib/ruby/stdlib/rubygems/dependency.rb:310 - Gem::MissingSpecError
^CThread in trap: #<Thread:0x19b33233>
Exception `SystemExit' at org/jruby/RubyKernel.java:735 - exit
Thread in at_exit: #<Thread:0x37d31475>

## MRI
 $ ruby test_in_jruby.rb 
^CThread in trap: #<Thread:0x00007fd94d085c40 run>
Thread in at_exit: #<Thread:0x00007fd94d085c40 run>

Let me know if I'm doing something wrong or if this is a duplicate issue.
I had a quick glance at #4956 but not sure if this is related.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions