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

thread_pool: fix race condition when shutting down workers #809

Merged
merged 2 commits into from
Nov 6, 2015
Merged

thread_pool: fix race condition when shutting down workers #809

merged 2 commits into from
Nov 6, 2015

Conversation

unleashed
Copy link
Contributor

The following backtrace appeared in my error logs:

Exception handling servers: undefined method `join' for nil:NilClass (NoMethodError)
/var/lib/gems/2.2.0/bundler/gems/puma-2.14.0/lib/puma/thread_pool.rb:250:in `shutdown'
/var/lib/gems/2.2.0/bundler/gems/puma-2.14.0/lib/puma/server.rb:814:in `graceful_shutdown'
/var/lib/gems/2.2.0/bundler/gems/puma-2.14.0/lib/puma/server.rb:338:in `handle_servers'
/var/lib/gems/2.2.0/bundler/gems/puma-2.14.0/lib/puma/server.rb:296:in `block in run'

This signals a problem in thread_pool.rb:250, where the code below lives:

if !@workers.empty?
  @workers.first.join until @workers.empty?
end

Since at that point @mutex is not held, @workers can and does in fact get modified by threads shutting down. There is a race condition where the worker disappears between the time of check and the time of use because of the code below in thread_pool.rb:111, when the worker is exiting:

mutex.synchronize do
  @spawned -= 1
  @workers.delete th
end

I have fixed this by duping the worker threads array inside the critical section and using each to iterate and join all workers.

evanphx added a commit that referenced this pull request Nov 6, 2015
thread_pool: fix race condition when shutting down workers
@evanphx evanphx merged commit cae3fbe into puma:master Nov 6, 2015
@unleashed unleashed deleted the threadpool-fix-race-in-shutdown branch November 6, 2015 18:17
kyrylo added a commit to airbrake/airbrake-ruby that referenced this pull request Mar 10, 2016
The fix ensures that the build passes on newer Rubinius versions, which
fail with "NoMethodError: undefined method `join' on nil:NilClass." due
to (apparently) a race condition.

The fix is copied from puma/puma#809.
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

Successfully merging this pull request may close these issues.

None yet

2 participants