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 e0753de commit c3acff5
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/puma/request.rb
Expand Up @@ -167,11 +167,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 c3acff5

Please sign in to comment.