Repository navigation
Add :closed_reason to Mint.HTTPError for closed connections - #555
Conversation
After an HTTP/2 connection is closed, request/5 and stream_request_body/3
returned %Mint.HTTPError{reason: :closed} with no way to tell why the
connection closed. The connection now remembers the cause and the error
carries it in a new :closed_reason field (:client_closed, :remote_closed,
:recv_failed, :send_failed, :goaway, :invalid_frame), which is also
included in the message. :reason stays :closed.
ericmj
left a comment
There was a problem hiding this comment.
The closed_reason field should be set for HTTP/1 as well, in this PR. HTTP/1 closes the connection without returning an error after a response with Connection: close, or an HTTP/1.0 response without keep-alive. The caller then sees nothing until a later request/5 fails with :closed, so that's where the field helps the most. Without HTTP/1, Mint.HTTP callers get a reason or nil depending on which protocol ALPN picked.
The close paths in Mint.HTTP1 map onto the existing values like this:
close/1::client_closed- send returning
:closedinrequest/5andstream_request_body/3::send_failed handle_close/1(tcp_closed/ssl_closed, orrecv/3returning:closed)::remote_closedhandle_transport_error/2and thesetoptsfailure instream/2::recv_failedhandle_data/2closing on unexpected data or a parse error: this means the same as:invalid_frame, but that name only fits HTTP/2.:protocol_violationwould work for both protocols.close_after_response/3afterConnection: closeor HTTP/1.0 without keep-alive: needs a new value, for example:server_requested_close.
With that in place, the "Currently only set by Mint.HTTP2" sentence in the Mint.HTTPError docs can go.
|
|
||
| defp handle_closed(conn) do | ||
| conn = put_in(conn.state, :closed) | ||
| conn = %{conn | state: :closed, closed_reason: :remote_closed} |
There was a problem hiding this comment.
This overwrites a reason that's already set. In active mode, the {:ssl_closed, socket} message still arrives after send! fails with :closed. stream/2 passes it here, and :send_failed turns into :remote_closed. The same happens to :goaway, because the handshake GOAWAY path doesn't close the socket. I think :recv_failed is affected too, since gen_tcp sends tcp_closed after tcp_error. closed_reason: conn.closed_reason || :remote_closed fixes it. Can you add a test that sends {:ssl_closed, socket} after the send failure?
|
|
||
| {:error, %TransportError{reason: :closed} = error} -> | ||
| throw({:mint, %{conn | state: :closed}, error}) | ||
| throw({:mint, %{conn | state: :closed, closed_reason: :send_failed}, error}) |
There was a problem hiding this comment.
send! only sets this when the transport returns :closed, and that means the server closed the socket, the same event as :remote_closed. The only difference is whether Mint noticed while sending or while receiving. Is that split useful to callers, or should both be :remote_closed?
Mint.HTTP1 now records why the connection was closed, so a later request/5 or stream_request_body/3 returns the same :closed_reason as in HTTP/2. :invalid_frame is renamed to :protocol_violation, which fits both protocols, and :server_requested_close is added for responses that don't keep the connection open (connection: close, or HTTP/1.0 without keep-alive).
|
added http/1 in fd30e35 with your mapping, and dropped the "currently only set by went with two
both have tests. happy to switch either one if you'd rather keep them under |
…ssages
A connection that Mint closed without closing the socket, such as after a
failed send, a GOAWAY during the HTTP/2 handshake or a failed setopts call,
still receives {:tcp_closed, socket} or {:tcp_error, socket, reason}.
Passing those to stream/2 replaced the reason with :remote_closed or
:recv_failed.
…_size A response line or header section over the :max_header_list_size option closed the connection with :protocol_violation, although the server sent valid HTTP.
The other error reason types end in term() or are term(), so new reasons can be added without changing the type's contract.
Buffered header block fragments over the bound derived from the max_header_list_size client setting closed the connection with :protocol_violation, the same as the HTTP/1 limit errors did.
A send only set :send_failed when the transport returned :closed, which means the server closed the socket, the same event :remote_closed describes. The two values only differed in which call noticed it.
|
Thank you! 💜 |
Closes #422
Mint.HTTPErrorgets a:closed_reasonfield, so callers can tell why a connection was closed when a later request fails with:closed.Problem
Once a connection is closed,
request/5andstream_request_body/3return the same error whatever closed it.Fix
Mint.HTTP1andMint.HTTP2record the cause wherever the connection moves to:closed::client_closed(close/1),:remote_closed(socket closed by the server),:recv_failed,:send_failed,:protocol_violation(Mint closed the connection after the server broke the protocol: an invalid HTTP/2 frame, unexpected HTTP/1 data or a parse error, or an HTTP/1.0 message withtransfer-encoding),:goaway(GOAWAY during the HTTP/2 handshake) and:server_requested_close(HTTP/1 response withconnection: close, or HTTP/1.0 without keep-alive).:closederrors fromrequest/5andstream_request_body/3carry it, andException.message/1appends a short description.:reasonis still:closed, so existing matches keep working. The field isnilfor every other error.Testing
describe "closed connection"for each protocol, checkingclosed_reasonon the error from a laterrequest/5(andstream_request_body/3forclose/1) and the message.:goaway.