Skip to content

Commit

Permalink
Keep thread names under 15 characters (puma#2733)
Browse files Browse the repository at this point in the history
* Keep thread names under 15 characters

* Add test

Co-authored-by: Olivier Bellone <olivier@bellone.fr>
  • Loading branch information
2 people authored and JuanitoFatas committed Sep 9, 2022
1 parent 73a30cb commit cd0988e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/puma/cluster/worker.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/plugin.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/runner.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/puma/server.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/thread_pool.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_integration_cluster.rb
Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions test/test_thread_pool.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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/<pid>/task/<tid>/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
Expand Down

0 comments on commit cd0988e

Please sign in to comment.