Skip to content

Commit

Permalink
All Puma threads are named
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Sep 15, 2019
1 parent 92ec65b commit 0e92533
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 7 deletions.
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
12 changes: 9 additions & 3 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 Expand Up @@ -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
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_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

0 comments on commit 0e92533

Please sign in to comment.