From 61294479b2f77a838f0d91064ca6d4ed5ba4b9ff Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Thu, 25 Feb 2016 13:35:47 -0800 Subject: [PATCH] Clean thread locals when using keepalive. Fixes #823 --- lib/puma/server.rb | 3 +++ lib/puma/thread_pool.rb | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 2f267190..a42e38a7 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -399,6 +399,7 @@ module Puma # def process_client(client, buffer) begin + clean_thread_locals = @options[:clean_thread_locals] close_socket = true while true @@ -412,6 +413,8 @@ module Puma return unless @queue_requests buffer.reset + ThreadPool.clean_thread_locals if clean_thread_locals + unless client.reset(@status == :run) close_socket = false client.set_timeout @persistent_timeout diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index 2b44325a..bae166f8 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -45,6 +45,12 @@ module Puma attr_reader :spawned, :trim_requested attr_accessor :clean_thread_locals + def self.clean_thread_locals + Thread.current.keys.each do |key| + Thread.current[key] = nil unless key == :__recursive_key__ + end + end + # How many objects have yet to be processed by the pool? # def backlog @@ -98,9 +104,7 @@ module Puma break unless continue if @clean_thread_locals - Thread.current.keys.each do |key| - Thread.current[key] = nil unless key == :__recursive_key__ - end + ThreadPool.clean_thread_locals end begin