Skip to content

Add :closed_reason to Mint.HTTPError for closed connections - #555

Merged
ericmj merged 7 commits into
elixir-mint:mainfrom
agustin2606:closed-reason-in-http-error
Dec 2, 2026
Merged

ericmj merged 7 commits into
elixir-mint:mainfrom
agustin2606:closed-reason-in-http-error

Conversation

@agustin2606

@agustin2606 agustin2606 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #422

Mint.HTTPError gets a :closed_reason field, so callers can tell why a connection was closed when a later request fails with :closed.

Problem

Once a connection is closed, request/5 and stream_request_body/3 return the same error whatever closed it.

{:ok, conn} = Mint.HTTP2.close(conn)
{:error, _conn, error} = Mint.HTTP2.request(conn, "GET", "/", [], nil)
# before: %Mint.HTTPError{reason: :closed}, "the connection is closed"
#         (same as after the server dropped the socket, a failed send, a GOAWAY...)
# after:  %Mint.HTTPError{reason: :closed, closed_reason: :client_closed},
#         "the connection is closed (closed with close/1)"

Fix

  • Both Mint.HTTP1 and Mint.HTTP2 record 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 with transfer-encoding), :goaway (GOAWAY during the HTTP/2 handshake) and :server_requested_close (HTTP/1 response with connection: close, or HTTP/1.0 without keep-alive).
  • The :closed errors from request/5 and stream_request_body/3 carry it, and Exception.message/1 appends a short description.
  • Following the discussion in the issue, this is an extra field rather than a tuple reason: :reason is still :closed, so existing matches keep working. The field is nil for every other error.

Testing

  • One test per cause in describe "closed connection" for each protocol, checking closed_reason on the error from a later request/5 (and stream_request_body/3 for close/1) and the message.
  • The existing GOAWAY-during-handshake test also checks :goaway.

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 ericmj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :closed in request/5 and stream_request_body/3: :send_failed
  • handle_close/1 (tcp_closed/ssl_closed, or recv/3 returning :closed): :remote_closed
  • handle_transport_error/2 and the setopts failure in stream/2: :recv_failed
  • handle_data/2 closing on unexpected data or a parse error: this means the same as :invalid_frame, but that name only fits HTTP/2. :protocol_violation would work for both protocols.
  • close_after_response/3 after Connection: close or 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.

Comment thread lib/mint/http2.ex Outdated

defp handle_closed(conn) do
conn = put_in(conn.state, :closed)
conn = %{conn | state: :closed, closed_reason: :remote_closed}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread lib/mint/http2.ex Outdated

{:error, %TransportError{reason: :closed} = error} ->
throw({:mint, %{conn | state: :closed}, error})
throw({:mint, %{conn | state: :closed, closed_reason: :send_failed}, error})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@agustin2606 agustin2606 changed the title Add :closed_reason to Mint.HTTPError for closed HTTP/2 connections Add :closed_reason to Mint.HTTPError for closed connections Dec 2, 2026
@agustin2606

Copy link
Copy Markdown
Contributor Author

added http/1 in fd30e35 with your mapping, and dropped the "currently only set by Mint.HTTP2" line from the docs.

went with :protocol_violation for both protocols, renaming :invalid_frame since it's unreleased, so http/1 and http/2 share the value.

two close_after_response/3 paths don't follow the mapping literally:

  • a body read until close goes through handle_close/1 and close_after_response/3 at once. i went with :remote_closed since the server closed the socket.
  • an http/1.0 response with transfer-encoding (final or 1xx) gets :protocol_violation, not :server_requested_close. mint closes because rfc 9112 6.1 treats that framing as faulty, not because the server asked, and it can even come with keep-alive.

both have tests. happy to switch either one if you'd rather keep them under :server_requested_close.

…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.
@ericmj
ericmj merged commit e4ccc06 into elixir-mint:main Dec 2, 2026
@ericmj

ericmj commented Dec 2, 2026

Copy link
Copy Markdown
Member

Thank you! 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error messaging context

2 participants