From 2f65f36caeb506dd1f67bbdc5815d058f9c36da9 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Tue, 10 Dec 2024 06:47:33 +0800 Subject: [PATCH] DEV: Fix duplicated logging of unicorn worker backtraces (#30178) In 6cafe59c76fd2e1151f2b8483402a6984ccd901b, we added a monkey patch to `Unicorn::HtppServer#murder_lazy_workers` to log a message and send a `USR2` signal to the Unicorn worker process when they Unicorn worker process is 2 seconds away from being timed out by the Unicorn master process. However, we ended up loggging multiple messages and sending multiple USR2 signal during the 2 seconds before the Unicorn worker process hit the time out. To overcome this problem, we will now set an instance variable on the `Unicorn::Worker` instance and use it to ensure that the log message is only logged once and USR2 signal to the Unicorn worker process is only sent one as well. --- config/unicorn.conf.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 8db561ba17e..62f06167e84 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -131,9 +131,12 @@ before_fork do |server, worker| tmp = @timeout - diff # START MONKEY PATCH - if tmp < 2 - logger.error "worker=#{worker.nr} PID:#{wpid} running too long " \ - "(#{diff}s), sending USR2 to dump thread backtraces" + if tmp < 2 && !worker.instance_variable_get(:@timing_out_logged) + logger.error do + "worker=#{worker.nr} PID:#{wpid} running too long (#{diff}s), sending USR2 to dump thread backtraces" + end + + worker.instance_variable_set(:@timing_out_logged, true) kill_worker(:USR2, wpid) end # END MONKEY PATCH