1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib
Blake Williams b2283d8f25
Fix rack.after_reply exceptions breaking connections (#2861)
* Fix rack.after_reply exceptions breaking connections

Currently, when a `rack.after_reply` callable raises an exception we
attempt to handle it like other client errors by responding with an HTTP
500 response. This however doesn't work because `rack.after_reply`
callbacks are called after the response body has already been written to
the client.

This can cause issues with re-used connections. This is because 2 HTTP
responses are being returned for a single request. If a second HTTP
request is made before the error handling logic completes the timing can
line up causing the second HTTP response to be served a 500 from the
first HTTP requests `rack.after_reply` callbacks raising.

That may look roughly like:

1. Request 1 starts, opening a reusable TCP connection
2. Request 1 is written to and "completed"
3. Request 1 `rack.after_reply` callables are called
4. Request 2 starts, reusing the same TCP connection as request 1
5. `rack.after_reply` raises, calls `client_error` and serves a 500
   response
6. Request 2 receives the 500 response.

This is somewhat difficult to reproduce using HTTP clients since it's a
race condition whether or not the 500 is written at the "correct" time
or not.

To prevent this issue the `rack.after_reply` callables are now wrapped
in a begin/rescue/end block that rescues from `StandardError` and logs
instead of attempting to serve a 500 response.

* Assert against less specific exception
2022-04-17 10:21:36 -06:00
..
puma Fix rack.after_reply exceptions breaking connections (#2861) 2022-04-17 10:21:36 -06:00
rack/handler [WIP] Refactor: Split out LogWriter from Events (no logic change) (#2798) 2022-02-05 10:06:22 -07:00
puma.rb Rename Puma::JSON to Puma::JSONSerialization (#2640) 2021-06-09 08:55:45 -06:00