Skip to content

Race condition leads to AttributeError from BaseEventLoop.__del__ #158903

Description

@bmerry

Bug report

The following program will rarely (maybe 0.1% of the time) print a stack trace:

#!/usr/bin/env python3

import asyncio
import os
import signal
import sys
import threading

async def main():
    await asyncio.sleep(1)

sys.setswitchinterval(1e-6)
try:
    t = threading.Thread(target=os.kill, args=(0, signal.SIGINT))
    t.start()
    asyncio.run(main())
except KeyboardInterrupt:
    pass
finally:
    t.join()

Output:

Exception ignored while calling deallocator <function BaseEventLoop.__del__ at 0x721e011f2140>:
Traceback (most recent call last):
  File "/usr/lib/python3.15/asyncio/base_events.py", line 765, in __del__
    self.close()
    ~~~~~~~~~~^^
  File "/usr/lib/python3.15/asyncio/unix_events.py", line 71, in close
    super().close()
    ~~~~~~~~~~~~~^^
  File "/usr/lib/python3.15/asyncio/selector_events.py", line 105, in close
    self._close_self_pipe()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.15/asyncio/selector_events.py", line 112, in _close_self_pipe
    self._remove_reader(self._ssock.fileno())
                        ^^^^^^^^^^^
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_ssock'
/home/bmerry/work/presentations/2026-10-pycon-asyncio-patterns/tests/./pause.py:19: RuntimeWarning: coroutine 'main' was never awaited
  pass

Tested on Ubuntu 24.04 with CPython 3.15.0rc3 from deadsnakes. I've also observed this with 3.14.6.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions