From 7fa67d8c02719b67f792eae2f1024c6b64a804ba Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Fri, 7 Jul 2017 16:59:02 -0400 Subject: Stop replacing the environment in Rack::Lock While the flow of environment information is generally downstream only, test frameworks sometimes rely on being able to pull information added to the environment after `.call`. --- lib/rack/lock.rb | 15 ++++++++++++--- test/spec_lock.rb | 12 +++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/rack/lock.rb b/lib/rack/lock.rb index 923dca59..b5a41e8e 100644 --- a/lib/rack/lock.rb +++ b/lib/rack/lock.rb @@ -11,12 +11,21 @@ module Rack def call(env) @mutex.lock + @env = env + @old_rack_multithread = env[RACK_MULTITHREAD] begin - response = @app.call(env.merge(RACK_MULTITHREAD => false)) - returned = response << BodyProxy.new(response.pop) { @mutex.unlock } + response = @app.call(env.merge!(RACK_MULTITHREAD => false)) + returned = response << BodyProxy.new(response.pop) { unlock } ensure - @mutex.unlock unless returned + unlock unless returned end end + + private + + def unlock + @mutex.unlock + @env[RACK_MULTITHREAD] = @old_rack_multithread + end end end diff --git a/test/spec_lock.rb b/test/spec_lock.rb index aa3efa54..c6f7c05e 100644 --- a/test/spec_lock.rb +++ b/test/spec_lock.rb @@ -147,7 +147,8 @@ describe Rack::Lock do }, false) env = Rack::MockRequest.env_for("/") env['rack.multithread'].must_equal true - app.call(env) + _, _, body = app.call(env) + body.close env['rack.multithread'].must_equal true end @@ -191,4 +192,13 @@ describe Rack::Lock do lambda { app.call(env) }.must_raise Exception lock.synchronized.must_equal false end + + it "not replace the environment" do + env = Rack::MockRequest.env_for("/") + app = lock_app(lambda { |inner_env| [200, {"Content-Type" => "text/plain"}, [inner_env.object_id.to_s]] }) + + _, _, body = app.call(env) + + body.to_enum.to_a.must_equal [env.object_id.to_s] + end end -- cgit v1.2.3-24-ge0c7