Skip to content

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

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. johnslavik commented on Nov 8, 2025

    @johnslavik
    MemberAuthor

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

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Nov 8, 2025
  3. 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.

  4. 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.

  5. johnslavik commented on Nov 15, 2025

    @johnslavik
    MemberAuthor

    Thanks. I'll see what I can do.

  6. 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>
    
  7. self-assigned this
    on Jul 20, 2026
  8. added
    3.16new features, bugs and security fixes
    on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.13only security fixes3.14bugs and security fixes3.15bugs and security fixes3.16new features, bugs and security fixesstdlibStandard 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