Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workers::TaskGroup::run is not signal safe #7

Closed
jlfwong opened this issue Dec 8, 2017 · 0 comments
Closed

Workers::TaskGroup::run is not signal safe #7

jlfwong opened this issue Dec 8, 2017 · 0 comments

Comments

@jlfwong
Copy link
Contributor

jlfwong commented Dec 8, 2017

It's possible for the TaskGroup.run call to terminate before all the tasks in the group have completed.

Repro:

require 'workers'

# test.rb
Signal.trap("USR1") do
  puts "RECEIVED SIGNAL"
end

group = Workers::TaskGroup.new
group.add { sleep(10000000) }
puts group.run
puts group.tasks[0].state

In one shell: ruby test.rb

In another shell, run:

$ ps aux | grep test.rb
jlfwong          42699   0.0  0.1  2511824  14252 s004  S+    2:16PM   0:00.36 ruby test.rb
$ kill -30 

Then switching back to the original shell, I see the following:

RECEIVED SIGNAL
false
running

My expectation in this case would be that run would just not terminate until much much later.

After 10000000 seconds, I'd expect the output to be:

RECEIVED SIGNAL
true
succeeded

From digging around in ruby source, it seems to me that ConditionVariable::wait is not signal safe. http://ruby-doc.org/stdlib-2.0.0/libdoc/thread/rdoc/ConditionVariable.html

::wait calls mutex.sleep, which in turn calls rb_mutex_sleep, which eventually ends up doing this: https://github.com/ruby/ruby/blob/55f93cb63f350c7705733f86923561363a297e00/thread.c#L1084

Which will exit as soon as the native_sleep call terminates, which is I believe upon receiving any handled signal.

I arrived here trying to debug why running my code under stackprof was causing failures (commentary on this issue here: tmm1/stackprof#91 (comment))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant