1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

resolve waiting counter problem

if a thread is in the wait state, but transitions into
a killed state the waiting counter will not be decremented
leaving the pool to believe there is a thread available
and waiting to complete work when that is not the case.
Introduce a begin..ensure block so that any thread exception
raised while in the wait state properly balances the
waiting counter.
This commit is contained in:
Adam Markowitz 2017-09-01 12:31:47 -07:00 committed by John W. Phillips
parent 22002acc9d
commit 111874d8b5
2 changed files with 25 additions and 2 deletions

View file

@ -122,8 +122,11 @@ module Puma
@out_of_band_pending = false
end
not_full.signal
not_empty.wait mutex
@waiting -= 1
begin
not_empty.wait mutex
ensure
@waiting -= 1
end
end
work = todo.shift

View file

@ -266,4 +266,24 @@ class TestThreadPool < Minitest::Test
assert_equal 2, rescued.length
refute rescued.compact.any?(&:alive?)
end
def test_correct_waiting_count_for_killed_threads
pool = new_pool(1, 1) { |_| }
pause
# simulate our waiting worker thread getting killed for whatever reason
pool.instance_eval { @workers[0].kill }
pause
pool.reap
pause
pool << 0
pause
assert_equal 0, pool.backlog
end
end