Skip to content

gh-158903: Avoid AttributeError in BaseEventLoop.__del__ and close() during premature teardown - #158954

Closed
JohnnyWilson16 wants to merge 1 commit into
python:mainfrom
JohnnyWilson16:fix-gh-158903-eventloop-del-attributeerror
Closed

JohnnyWilson16 wants to merge 1 commit into
python:mainfrom
JohnnyWilson16:fix-gh-158903-eventloop-del-attributeerror

Conversation

@JohnnyWilson16

@JohnnyWilson16 JohnnyWilson16 commented Oct 7, 2026 •

Copy link
Copy Markdown

An interrupted event loop initialization (e.g. keyboard interrupt or signal received before _make_self_pipe() or proactor setup completes) leaves attributes like _ssock, _csock, _selector, _proactor, and _signal_handlers uninitialized or partially assigned. When garbage collected, BaseEventLoop.__del__ calls self.close() -> _close_self_pipe(), unconditionally dereferencing self._ssock.fileno() and raising an AttributeError via sys.unraisablehook.

This patch guards internal attribute dereferences across BaseSelectorEventLoop, _UnixSelectorEventLoop, and BaseProactorEventLoop during close() and _close_self_pipe(), ensuring partially initialized loops tear down cleanly.

  • Added regression test test_close_partially_initialized_loop in Lib/test/test_asyncio/test_base_events.py verifying clean close and zero unraisable exceptions under support.catch_unraisable_exception().
  • Verified via ./python -m test test_asyncio (33/33 suites passed).
  • Includes NEWS blurb in Misc/NEWS.d/next/Library/.

Fixes #158903.

@python-cla-bot

python-cla-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@JohnnyWilson16
JohnnyWilson16 force-pushed the fix-gh-158903-eventloop-del-attributeerror branch from 1776568 to 48c92ce Compare October 7, 2026 07:51
@picnixz

picnixz commented Oct 7, 2026

Copy link
Copy Markdown
Member

This adds un-necessary defensive guards for a very rare event. And I don't think it's necessary to have it. I'm closing it because it's premature to do so and I'll Kumar decide on whether he wants to accept the issue.

@picnixz picnixz closed this Oct 7, 2026
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.

Race condition leads to AttributeError from BaseEventLoop.__del__

2 participants