Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep thread names under 15 characters #2733

Merged
merged 2 commits into from Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: lets not change any thread names that are always less than 15 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that this thread name is also used as a prefix for its accompanying threadpool threads. So if we don't shorten it, we end up with names like puma control tp 001 which is longer than 15 characters.

@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 @@ -159,7 +159,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