From 751720a3db9806829fdd0081310b5990879dcb2b Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Tue, 25 Nov 2014 23:18:30 -0800 Subject: [PATCH] Conditionalize thread local cleaning. Possible perf degradation fix --- lib/puma/configuration.rb | 7 +++++++ lib/puma/server.rb | 2 ++ lib/puma/thread_pool.rb | 9 +++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index 6c5b9210..fe616bc7 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -212,6 +212,13 @@ module Puma @options[:binds] << "tcp://#{Configuration::DefaultTCPHost}:#{port}" end + # Work around leaky apps that leave garbage in Thread locals + # across requests + # + def clean_thread_locals(which=true) + @options[:clean_thread_locals] = which + end + # Daemonize the server into the background. Highly suggest that # this be combined with +pidfile+ and +stdout_redirect+. def daemonize(which=true) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index fd737159..a99daf6c 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -259,6 +259,8 @@ module Puma end end + @thread_pool.clean_thread_locals = @options[:clean_thread_locals] + @reactor = Reactor.new self, @thread_pool @reactor.run_in_thread diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index 47c355c8..7ec36ff9 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -36,9 +36,12 @@ module Puma @mutex.synchronize do @min.times { spawn_thread } end + + @clean_thread_locals = false end attr_reader :spawned, :trim_requested + attr_accessor :clean_thread_locals # How many objects have yet to be processed by the pool? # @@ -89,8 +92,10 @@ module Puma break unless continue - Thread.current.keys.each do |key| - Thread.current[key] = nil unless key == :__recursive_key__ + if @clean_thread_locals + Thread.current.keys.each do |key| + Thread.current[key] = nil unless key == :__recursive_key__ + end end block.call(work, *extra)