Skip to content

Commit

Permalink
Merge pull request rack#425 from SamSaffron/master
Browse files Browse the repository at this point in the history
Fixed Rack::Lock so it correctly releases mutexes on throw
  • Loading branch information
rkh committed Sep 5, 2012
2 parents d084a14 + 5c230ff commit ea7ed1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/rack/lock.rb
Expand Up @@ -13,12 +13,11 @@ def call(env)
old, env[FLAG] = env[FLAG], false
@mutex.lock
response = @app.call(env)
response[2] = BodyProxy.new(response[2]) { @mutex.unlock }
body = BodyProxy.new(response[2]) { @mutex.unlock }
response[2] = body
response
rescue Exception
@mutex.unlock
raise
ensure
@mutex.unlock unless body
env[FLAG] = old
end
end
Expand Down
10 changes: 9 additions & 1 deletion test/spec_lock.rb
Expand Up @@ -134,11 +134,19 @@ def close; @close_called = true; end
should "unlock if the app raises" do
lock = Lock.new
env = Rack::MockRequest.env_for("/")
app = lock_app(lambda { raise Exception })
app = lock_app(lambda { raise Exception }, lock)
lambda { app.call(env) }.should.raise(Exception)
lock.synchronized.should.equal false
end

should "unlock if the app throws" do
lock = Lock.new
env = Rack::MockRequest.env_for("/")
app = lock_app(lambda {|env| throw :bacon }, lock)
lambda { app.call(env) }.should.throw(:bacon)
lock.synchronized.should.equal false
end

should "set multithread flag to false" do
app = lock_app(lambda { |env|
env['rack.multithread'].should.equal false
Expand Down

0 comments on commit ea7ed1a

Please sign in to comment.