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 #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 nateberkopec committed Feb 11, 2022
1 parent 15dd116 commit b70f451
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/puma/request.rb
Original file line number Diff line number Diff line change
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 b70f451

Please sign in to comment.