lib/tobox/pool/threaded_pool.rb in tobox-0.5.0 vs lib/tobox/pool/threaded_pool.rb in tobox-0.5.1

- old
+ new

@@ -20,28 +20,39 @@ end end def stop shutdown_timeout = @configuration[:shutdown_timeout] + grace_shutdown_timeout = @configuration[:grace_shutdown_timeout] - deadline = Process.clock_gettime(::Process::CLOCK_MONOTONIC) - super Thread.pass # let workers finish # soft exit - while Process.clock_gettime(::Process::CLOCK_MONOTONIC) - deadline < shutdown_timeout - return if @threads.empty? + join = lambda do |timeout| + start = Process.clock_gettime(::Process::CLOCK_MONOTONIC) - sleep 0.5 + loop do + terminating_th = @threads.synchronize { @threads.first } + + return unless terminating_th + + elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start + + break if elapsed > timeout + + terminating_th.join(timeout - elapsed) + end end + join.call(shutdown_timeout) + # hard exit - @threads.each { |th| th.raise(KillError) } - while (th = @threads.pop) - th.value # waits - end + @threads.synchronize { @threads.each { |th| th.raise(KillError) } } + join.call(grace_shutdown_timeout) + @threads.synchronize { @threads.each(&:kill) } + join.call(1) end private def start_thread_worker(wrk) @@ -61,11 +72,9 @@ subst_worker = Worker.new(worker.label, @configuration) @workers[idx] = subst_worker subst_thread = start_thread_worker(subst_worker) @threads << subst_thread end - # all workers went down abruply, we need to kill the process. - # @parent_thread.raise(Interrupt) if wk.finished? && @threads.empty? && @running end end end end end