From 0e925338659d9bef7453f3f7fa16b84f2bc3d8dd Mon Sep 17 00:00:00 2001 From: Nate Berkopec Date: Sun, 15 Sep 2019 10:32:14 +0200 Subject: [PATCH] All Puma threads are named --- lib/puma.rb | 6 ++++++ lib/puma/cluster.rb | 2 ++ lib/puma/launcher.rb | 4 ++++ lib/puma/plugin.rb | 5 ++++- lib/puma/reactor.rb | 1 + lib/puma/server.rb | 12 +++++++++--- lib/puma/thread_pool.rb | 5 +++-- test/test_thread_pool.rb | 2 +- 8 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/puma.rb b/lib/puma.rb index c48e352fdf..cb46f8ca35 100644 --- a/lib/puma.rb +++ b/lib/puma.rb @@ -22,4 +22,10 @@ def self.stats_object=(val) def self.stats @get_stats.stats end + + # Thread name is new in Ruby 2.3 + def self.set_thread_name(name) + return unless Thread.current.respond_to?(:name=) + Thread.current.name = "puma #{name}" + end end diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index 86804a4ed8..b09d395124 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -248,6 +248,7 @@ def worker(index, master) @suicide_pipe.close Thread.new do + Puma.set_thread_name "worker check pipe" IO.select [@check_pipe] log "! Detected parent died, dying" exit! 1 @@ -282,6 +283,7 @@ def worker(index, master) end Thread.new(@worker_write) do |io| + Puma.set_thread_name "stat payload" base_payload = "p#{Process.pid}" while true diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index 2078a125b9..de9a08d2de 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -328,6 +328,10 @@ def graceful_stop def log_thread_status Thread.list.each do |thread| @events.log "Thread TID-#{thread.object_id.to_s(36)} #{thread['label']}" + logstr = "Thread: TID-#{thread.object_id.to_s(36)}" + logstr += " #{thread.name}" if thread.respond_to?(:name) + @events.log logstr + if thread.backtrace @events.log thread.backtrace.join("\n") else diff --git a/lib/puma/plugin.rb b/lib/puma/plugin.rb index 571cff098f..91a8541e87 100644 --- a/lib/puma/plugin.rb +++ b/lib/puma/plugin.rb @@ -63,7 +63,10 @@ def add_background(blk) def fire_background @background.each do |b| - Thread.new(&b) + Thread.new do + set_thread_name "plugin background" + b.call + end end end end diff --git a/lib/puma/reactor.rb b/lib/puma/reactor.rb index 5bf10f2c19..57986adea7 100644 --- a/lib/puma/reactor.rb +++ b/lib/puma/reactor.rb @@ -307,6 +307,7 @@ def run def run_in_thread @thread = Thread.new do + Puma.set_thread_name "reactor" begin run_internal rescue StandardError => e diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 089bf7ad56..424f036b4a 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -207,7 +207,10 @@ def run_lopez_mode(background=true) @events.fire :state, :running if background - @thread = Thread.new { handle_servers_lopez_mode } + @thread = Thread.new do + Puma.set_thread_name "server" + handle_servers_lopez_mode + end return @thread else handle_servers_lopez_mode @@ -351,7 +354,10 @@ def run(background=true) @events.fire :state, :running if background - @thread = Thread.new { handle_servers } + @thread = Thread.new do + Puma.set_thread_name "server" + handle_servers + end return @thread else handle_servers @@ -416,7 +422,7 @@ def handle_servers @events.fire :state, @status - graceful_shutdown if @status == :stop || @status == :restart + #graceful_shutdown if @status == :stop || @status == :restart if queue_requests @reactor.clear! @reactor.shutdown diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index 178077986f..2955d16093 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -87,8 +87,7 @@ def spawn_thread @spawned += 1 th = Thread.new(@spawned) do |spawned| - # Thread name is new in Ruby 2.3 - Thread.current.name = 'puma %03i' % spawned if Thread.current.respond_to?(:name=) + Puma.set_thread_name 'threadpool %03i' % spawned todo = @todo block = @block mutex = @mutex @@ -255,6 +254,7 @@ def start! @running = true @thread = Thread.new do + Puma.set_thread_name "threadpool autotrim" while @running @pool.trim sleep @timeout @@ -284,6 +284,7 @@ def start! @running = true @thread = Thread.new do + Puma.set_thread_name "threadpool reaper" while @running @pool.reap sleep @timeout diff --git a/test/test_thread_pool.rb b/test/test_thread_pool.rb index 7f2e65738b..ed51e191f4 100644 --- a/test/test_thread_pool.rb +++ b/test/test_thread_pool.rb @@ -37,7 +37,7 @@ def test_append_spawns @work_done.wait(@work_mutex, 5) assert_equal 1, pool.spawned assert_equal [1], saw - assert_equal('puma 001', thread_name) if Thread.current.respond_to?(:name) + assert_equal('puma threadpool 001', thread_name) if Thread.current.respond_to?(:name) end end