Skip to content

gh-109564: asyncio: drop connections accepted by a closed server - #160851

Merged
asvetlov merged 2 commits into
python:mainfrom
asvetlov:claude/asyncio-accept-closed-server-350cac
Oct 29, 2026
Merged

asvetlov merged 2 commits into
python:mainfrom
asvetlov:claude/asyncio-accept-closed-server-350cac

Conversation

@asvetlov

@asvetlov asvetlov commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

If Server.close() runs after a connection was accepted but before its transport is created, Server._attach() hits assert self._sockets is not None. The selector loop reports the AssertionError to the exception handler, and the half-built transport later raises TypeError from __del__ (Server._wakeup() with _waiters already None). On the proactor loop the exception escapes the accept callback and the accepted socket leaks.

Both accept paths now close such a connection quietly before the transport is created. SSL connections go through the same path.

The regression test closes the server from the protocol factory, which forces this ordering deterministically on both selector and proactor loops. It fails without the fix on Linux and Windows.

This overlaps with #154861, which guards only the selector path.

🤖 Generated with Claude Code

If Server.close() ran after a connection was accepted but before its
transport was created, Server._attach() hit an assertion, the error was
reported to the exception handler, and the half-built transport later
raised TypeError from __del__.  Close such connections quietly instead,
in both the selector and proactor accept paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@asvetlov
asvetlov merged commit 5364bdc into python:main Oct 29, 2026
53 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @asvetlov for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

GH-160878 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 29, 2026
@bedevere-app

bedevere-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

GH-160879 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 29, 2026
asvetlov added a commit that referenced this pull request Oct 29, 2026
…er (GH-160851) (#160879)

gh-109564: asyncio: drop connections accepted by a closed server (GH-160851)

If Server.close() ran after a connection was accepted but before its
transport was created, Server._attach() hit an assertion, the error was
reported to the exception handler, and the half-built transport later
raised TypeError from __del__.  Close such connections quietly instead,
in both the selector and proactor accept paths.
(cherry picked from commit 5364bdc)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
asvetlov added a commit that referenced this pull request Oct 29, 2026
…er (GH-160851) (#160878)

gh-109564: asyncio: drop connections accepted by a closed server (GH-160851)

If Server.close() ran after a connection was accepted but before its
transport was created, Server._attach() hit an assertion, the error was
reported to the exception handler, and the half-built transport later
raised TypeError from __del__.  Close such connections quietly instead,
in both the selector and proactor accept paths.
(cherry picked from commit 5364bdc)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant