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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All Puma threads are named #1968

Merged
merged 1 commit into from Sep 15, 2019
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: 6 additions & 0 deletions lib/puma.rb
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/puma/cluster.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/puma/launcher.rb
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/puma/plugin.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/puma/reactor.rb
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/puma/server.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/puma/thread_pool.rb
Expand Up @@ -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
Expand Down Expand Up @@ -255,6 +254,7 @@ def start!
@running = true

@thread = Thread.new do
Puma.set_thread_name "threadpool autotrim"
while @running
@pool.trim
sleep @timeout
Expand Down Expand Up @@ -284,6 +284,7 @@ def start!
@running = true

@thread = Thread.new do
Puma.set_thread_name "threadpool reaper"
while @running
@pool.reap
sleep @timeout
Expand Down
2 changes: 1 addition & 1 deletion test/test_integration_cluster.rb
Expand Up @@ -4,7 +4,7 @@
class TestIntegrationCluster < TestIntegration
def setup
super

skip NO_FORK_MSG unless HAS_FORK
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_thread_pool.rb
Expand Up @@ -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

Expand Down