Skip to content

Commit b70f451

Browse files
byrootnateberkopec
authored andcommitted
Ensure close is called on the response body no matter what
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.
1 parent 15dd116 commit b70f451

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/puma/request.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ def handle_request(client, lines, requests)
171171
end
172172

173173
ensure
174-
uncork_socket io
175-
176-
body.close
177-
client.tempfile.unlink if client.tempfile
178-
res_body.close if res_body.respond_to? :close
174+
begin
175+
uncork_socket io
176+
177+
body.close
178+
client.tempfile.unlink if client.tempfile
179+
ensure
180+
# Whatever happens, we MUST call `close` on the response body.
181+
# Otherwise Rack::BodyProxy callbacks may not fire and lead to various state leaks
182+
res_body.close if res_body.respond_to? :close
183+
end
179184

180185
after_reply.each { |o| o.call }
181186
end

0 commit comments

Comments
 (0)