Skip to content

Commit

Permalink
Ensure close is called on the response body no matter what
Browse files Browse the repository at this point in the history
Another fallout from puma#2809 is that
in some cases the `res_body.close` wasn't called because some previous code
raised.

For Rails apps it means CurrentAttributes and a few other important
states aren't reset properly.

This is being improved on the Rails side too, but I believe it would
be good to harden this on the puma side as well.
  • Loading branch information
byroot authored and JuanitoFatas committed Sep 9, 2022
1 parent 96a7c9b commit 30176fe
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/puma/request.rb
Expand Up @@ -171,11 +171,16 @@ def handle_request(client, lines, requests)
end

ensure
uncork_socket io

body.close
client.tempfile.unlink if client.tempfile
res_body.close if res_body.respond_to? :close
begin
uncork_socket io

body.close
client.tempfile.unlink if client.tempfile
ensure
# Whatever happens, we MUST call `close` on the response body.
# Otherwise Rack::BodyProxy callbacks may not fire and lead to various state leaks
res_body.close if res_body.respond_to? :close
end

after_reply.each { |o| o.call }
end
Expand Down

0 comments on commit 30176fe

Please sign in to comment.