Skip to content

The event loop warns only on Windows 3.14+ if it isn't closed before garbage collection #141262

Open

Description

@johnslavik

Bug report

Bug description:

This program reports ResourceWarning: unclosed event loop <ProactorEventLoop running=False closed=False debug=False> only on Windows and only since 3.14:

import asyncio
asyncio.set_event_loop(loop := asyncio.new_event_loop())
loop.run_until_complete(asyncio.sleep(1))

Other platforms and versions in the 3.9-3.15 Linux-MacOS-Windows matrix don't do that.

See jobs.
I've reproduced this at main too.

Key things I'm investigating:

  • Is it mandatory to call .close() manually, if we don't use a runner?
    • If yes, why does only Windows 3.14+ proactor loop raise this warning, and loops on other platforms and versions do not?
    • If no (since most platforms and versions seemingly do not require this), why does the loop raise the warning?
  • In general, could and should the asyncio REPL use a runner?

I've found this while working on the asyncio REPL. This issue blocks me from completing #140287. Note that the asyncio REPL does call .stop(); it only doesn't call .close(). .stop() is not meant to stop the loop for good (the loop is re-entrant), .close() is irreversible and idempotent.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15, CPython main branch

Operating systems tested on:

Windows, macOS, Linux

Activity

  1. added
    type-bug
    on Nov 8, 2025
  2. johnslavik commented on Nov 8, 2025

    @johnslavik
    MemberAuthor

    CC @ZeroIntensity labels: stdlib, topic-asyncio, 3.13, 3.14, 3.15

  3. added
    stdlib
    3.13
    3.14
    3.15
    on Nov 8, 2025
  4. kumaraditya303 commented on Nov 9, 2025

    @kumaraditya303
    Contributor

    All of them should raise the warning, if not then it is a bug which should be fixed.

  5. vstinner commented on Nov 13, 2025

    @vstinner
    Member

    If yes, why does only Windows 3.14+ proactor loop raise this warning, and loops on other platforms and versions do not?

    The implementation on Windows and on Unix is different. The Unix implementation is based on BaseSelectorEventLoop which uses a "self pipe" which does:

    self._add_reader(self._ssock.fileno(), self._read_from_self)

    This line creates a reference cycle. It prevents the event loop to be deallocated by the GC.

    The Windows implementation also uses a "self pipe" but with a different implementation which doesn't seem to suffer from the same reference cycle issue.

  6. johnslavik commented on Nov 15, 2025

    @johnslavik
    MemberAuthor

    Thanks. I'll see what I can do.

  7. kumaraditya303 commented on Nov 16, 2025

    @kumaraditya303
    Contributor

    This line creates a reference cycle. It prevents the event loop to be deallocated by the GC.

    That's not the actual issue here though. The issue is that the code calls asyncio.set_event_loop which stores the loop inside the policy object which is a module global so that global variable keeps the loop alive until the module is cleared.

    The code raises the warning if the set_event_loop is removed.

    import asyncio
    loop = asyncio.new_event_loop()
    loop.run_until_complete(asyncio.sleep(1))
    ❯ ./python.exe main.py
    /Users/kumaraditya/work/cpython/Lib/asyncio/base_events.py:758: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=False>
    
  8. self-assigned this
    on Jul 20, 2026
  9. added
    3.16
    on Jul 27, 2026
  10. asvetlov commented on Nov 5, 2026

    @asvetlov
    Contributor

    I think we can close the issue.
    After policies removal, both cases (with set_event_loop() and without it) raise a warning.

  11. moved this from Todo to Done in Sprinton Nov 5, 2026
  12. moved this from Todo to Done in asyncioon Nov 5, 2026
  13. johnslavik commented on Nov 5, 2026

    @johnslavik
    MemberAuthor

    No, this still reproduces. Fresh build on my MacOS:

    ❯ ./python.exe -VV
    Python 3.16.0a1+dev (heads/main:2ac2b93ca76, Nov  5 2026, 15:29:47) [Clang 21.0.0 (clang-2100.1.1.101)]
    
    ❯ cat t.py
    import asyncio
    loop = asyncio.new_event_loop()
    # asyncio.set_event_loop(loop)
    loop.run_until_complete(asyncio.sleep(1))
    
    ❯ ./python.exe t.py
    /Users/bartosz.slawecki/py/cpython/Lib/asyncio/base_events.py:769: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=True>
    
    ❯ sed -i '' 's/# //g' t.py
    
    ❯ cat t.py
    import asyncio
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(asyncio.sleep(1))
    
    ❯ ./python.exe t.py
    

    It's because of a refcycle in self-pipe, I'm still thinking how to tackle this. Maybe some sentinels instead of a ref to loop.

  14. moved this from Done to In Progress in Sprinton Nov 5, 2026
  15. moved this from Done to In Progress in asyncioon Nov 5, 2026
  16. vstinner commented on Nov 5, 2026

    @vstinner
    Member

    Use a weakref to self, and add a function calling self._read_from_self() if the weakref is still valid?

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

Metadata

Metadata

Assignees

Labels

3.133.143.153.16sprintstdlibtopic-asynciotype-bug

Projects

  • Status
    In Progress
  • Status
    In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions