Skip to content

Close TCP sockets on errors in HTTP/1 - #502

Merged
ericmj merged 2 commits into
elixir-mint:mainfrom
xxdavid:close_sockets_on_error
Sep 7, 2026
Merged

Close TCP sockets on errors in HTTP/1#502
ericmj merged 2 commits into
elixir-mint:mainfrom
xxdavid:close_sockets_on_error

Conversation

@xxdavid

@xxdavid xxdavid commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

In some cases, when handling a failure, Mint would only set the conn state to :closed but would not close the underlying socket. These sockets stay open until the calling process that owns them exits, and may therefore accumulate until resources are exhausted.

Every relevant %{conn | state: :closed} inside Mint.HTTP1 is replaced with internal_close that in addition to setting the state also closes the socket.

Tests that asserted closed conn now also assert that the socket is closed. New tests that demonstrate the problem are added. They would fail without the fix and they pass with the fix in place.

Fixes #501.

In some cases, when handling a failure, Mint would only set the conn
state to :closed but would not close the underlying socket. These
sockets stay open until the calling process that owns them exits, and
may therefore accumulate until resources are exhausted.

Every relevant %{conn | state: :closed} inside Mint.HTTP1 is replaced
with internal_close that in addition to setting the state also closes
the socket.

Tests that asserted closed conn now also assert that the socket is
closed. New tests that demonstrate the problem are added. They would
fail without the fix and they pass with the fix in place.

Fixes elixir-mint#501.

@whatyouhide whatyouhide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lovely!

@whatyouhide

Copy link
Copy Markdown
Contributor

@xxdavid can you look at the Dialyzer failure in CI?

The lines shifted a bit by the changes in the previous commit.
@xxdavid

xxdavid commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Ah, running Dialyzer did not cross my mind. Fortunately, it was just a stale line number in the ignore list. Thanks for the review!

@ericmj
ericmj merged commit bfc6454 into elixir-mint:main Sep 7, 2026
3 checks passed
@ericmj

ericmj commented Sep 7, 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.

TCP sockets get leaked on certain failures

3 participants