From 5b94b150edd783d3208326f0ad5e63e972fe4e0b Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sun, 12 Dec 2021 12:11:19 -0800 Subject: [PATCH] Keep thread names under 15 characters (#2733) * Keep thread names under 15 characters * Add test Co-authored-by: Olivier Bellone --- lib/puma/cluster/worker.rb | 6 +++--- lib/puma/plugin.rb | 2 +- lib/puma/runner.rb | 2 +- lib/puma/server.rb | 2 +- lib/puma/thread_pool.rb | 2 +- test/test_integration_cluster.rb | 2 +- test/test_thread_pool.rb | 31 ++++++++++++++++++++++++++++--- 7 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/puma/cluster/worker.rb b/lib/puma/cluster/worker.rb index 9ac3901e7e..7ac46883db 100644 --- a/lib/puma/cluster/worker.rb +++ b/lib/puma/cluster/worker.rb @@ -34,7 +34,7 @@ def run Signal.trap "SIGCHLD", "DEFAULT" Thread.new do - Puma.set_thread_name "worker check pipe" + Puma.set_thread_name "wrkr check" @check_pipe.wait_readable log "! Detected parent died, dying" exit! 1 @@ -76,7 +76,7 @@ def run end Thread.new do - Puma.set_thread_name "worker fork pipe" + Puma.set_thread_name "wrkr fork" while (idx = @fork_pipe.gets) idx = idx.to_i if idx == -1 # stop server @@ -114,7 +114,7 @@ def run while restart_server.pop server_thread = server.run stat_thread ||= Thread.new(@worker_write) do |io| - Puma.set_thread_name "stat payload" + Puma.set_thread_name "stat pld" base_payload = "p#{Process.pid}" while true diff --git a/lib/puma/plugin.rb b/lib/puma/plugin.rb index eb32f31265..8a943b594e 100644 --- a/lib/puma/plugin.rb +++ b/lib/puma/plugin.rb @@ -64,7 +64,7 @@ def add_background(blk) def fire_background @background.each_with_index do |b, i| Thread.new do - Puma.set_thread_name "plugin background #{i}" + Puma.set_thread_name "plgn bg #{i}" b.call end end diff --git a/lib/puma/runner.rb b/lib/puma/runner.rb index 013d175d77..3dd598aeed 100644 --- a/lib/puma/runner.rb +++ b/lib/puma/runner.rb @@ -69,7 +69,7 @@ def start_control control.binder.parse [str], self, 'Starting control server' - control.run thread_name: 'control' + control.run thread_name: 'ctl' @control = control end diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 4e3255e78d..759d913a5d 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -220,7 +220,7 @@ def pool_capacity # up in the background to handle requests. Otherwise requests # are handled synchronously. # - def run(background=true, thread_name: 'server') + def run(background=true, thread_name: 'srv') BasicSocket.do_not_reverse_lookup = true @events.fire :state, :booting diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index f711aee5cd..9454564f91 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -102,7 +102,7 @@ def spawn_thread @spawned += 1 th = Thread.new(@spawned) do |spawned| - Puma.set_thread_name '%s threadpool %03i' % [@name, spawned] + Puma.set_thread_name '%s tp %03i' % [@name, spawned] todo = @todo block = @block mutex = @mutex diff --git a/test/test_integration_cluster.rb b/test/test_integration_cluster.rb index b5d8abacfb..0db66612b8 100644 --- a/test/test_integration_cluster.rb +++ b/test/test_integration_cluster.rb @@ -177,7 +177,7 @@ def test_worker_timeout on_worker_boot do Thread.new do sleep 1 - Thread.list.find {|t| t.name == 'puma stat payload'}.kill + Thread.list.find {|t| t.name == 'puma stat pld'}.kill end end RUBY diff --git a/test/test_thread_pool.rb b/test/test_thread_pool.rb index 6d6befec3e..c6ffc0a68f 100644 --- a/test/test_thread_pool.rb +++ b/test/test_thread_pool.rb @@ -10,12 +10,12 @@ def teardown def new_pool(min, max, &block) block = proc { } unless block - @pool = Puma::ThreadPool.new('test', min, max, &block) + @pool = Puma::ThreadPool.new('tst', min, max, &block) end def mutex_pool(min, max, &block) block = proc { } unless block - @pool = MutexPool.new('test', min, max, &block) + @pool = MutexPool.new('tst', min, max, &block) end # Wraps ThreadPool work in mutex for better concurrency control. @@ -59,7 +59,32 @@ def test_thread_name thread_name = nil pool = mutex_pool(0, 1) {thread_name = Thread.current.name} pool << 1 - assert_equal('puma test threadpool 001', thread_name) + assert_equal('puma tst tp 001', thread_name) + end + + def test_thread_name_linux + skip 'Thread.name not supported' unless Thread.current.respond_to?(:name) + + task_dir = File.join('', 'proc', Process.pid.to_s, 'task') + skip 'This test only works under Linux with appropriate permissions' if !(File.directory?(task_dir) && File.readable?(task_dir)) + + expected_thread_name = 'puma tst tp 001' + found_thread = false + pool = mutex_pool(0, 1) do + # Read every /proc//task//comm file to find the thread name + Dir.entries(task_dir).select {|tid| File.directory?(File.join(task_dir, tid))}.each do |tid| + comm_file = File.join(task_dir, tid, 'comm') + next unless File.file?(comm_file) && File.readable?(comm_file) + + if File.read(comm_file).strip == expected_thread_name + found_thread = true + break + end + end + end + pool << 1 + + assert(found_thread, "Did not find thread with name '#{expected_thread_name}'") end def test_converts_pool_sizes