Skip to content

gh-162877: unregister selector transport on init failure - #162882

Open
jlu-lujing wants to merge 1 commit into
python:mainfrom
jlu-lujing:fix/asyncio-162877
Open

jlu-lujing wants to merge 1 commit into
python:mainfrom
jlu-lujing:fix/asyncio-162877

Conversation

@jlu-lujing

@jlu-lujing jlu-lujing commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #162877.

_SelectorTransport.__init__ registers the new transport in loop._transports before _SelectorSocketTransport.__init__ performs the remaining setup. If setup then raises, for example when _set_nodelay() gets EINVAL from a reset socket, the caller closes the socket but the transport remains registered and not closing. The kernel can reuse the fd, while _ensure_fd_no_transport() still rejects it. On the accept path this can leave a server resetting every new client while existing connections continue to work.

This change wraps the post-registration initialization. If any exception escapes, the transport removes its own entry if that entry still refers to itself, detaches from its server, clears _server, and re-raises. The regression test makes _set_nodelay() raise OSError and verifies that the transport is detached and the fd is no longer considered in use.

The issue reports this regression in 3.14.8 and later, so a backport to 3.14 may be appropriate.

Validation:

  • The new test fails before the fix because _server._detach() is never called, and passes after the fix.
  • ./python.exe -m test test_asyncio.test_selector_events: 137 tests passed.
  • ./python.exe -m test test_asyncio: 3,089 tests run, 76 skipped, 0 failed across 33 passing test files.
  • make patchcheck: passed.

This patch was prepared with assistance from OpenAI Codex; I reviewed the analysis against the failing test and the complete asyncio test suite.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio: a failed TCP_NODELAY leaves a stale transport registered, blocking its fd for every later connection (regression since 3.14.8)

1 participant