diff --git a/History.md b/History.md index 2bbc15c3..1238bd8e 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +### Master +* Bugfixes + * Resolve issue with threadpool waiting counter decrement when thread is killed + ## 5.0.0 * Features diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index c0bbedb1..bb6a73c7 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -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 diff --git a/test/test_thread_pool.rb b/test/test_thread_pool.rb index 697f5c46..0e6f7645 100644 --- a/test/test_thread_pool.rb +++ b/test/test_thread_pool.rb @@ -266,4 +266,19 @@ 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) { |_| } + sleep 1 + + # simulate our waiting worker thread getting killed for whatever reason + pool.instance_eval { @workers[0].kill } + sleep 1 + pool.reap + sleep 1 + + pool << 0 + sleep 1 + assert_equal 0, pool.backlog + end end