Skip to content

loop.run_forever can remove running loop if already running #130145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kumaraditya303 opened this issue Feb 15, 2025 · 0 comments
Closed

loop.run_forever can remove running loop if already running #130145

kumaraditya303 opened this issue Feb 15, 2025 · 0 comments
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@kumaraditya303
Copy link
Contributor

kumaraditya303 commented Feb 15, 2025

Reproducer:

import asyncio

async def main():
    loop = asyncio.new_event_loop()
    try:
        loop.run_forever()

    except RuntimeError:
        pass

    print(asyncio.get_running_loop())



asyncio.run(main())

Output:

cpython on  main [$?] via C v13.3.0-gcc via 🐍 v3.12.3 ./python main.py 
Traceback (most recent call last):
  File "/home/realkumaraditya/cpython/main.py", line 15, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "/home/realkumaraditya/cpython/Lib/asyncio/runners.py", line 204, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/home/realkumaraditya/cpython/Lib/asyncio/runners.py", line 127, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/realkumaraditya/cpython/Lib/asyncio/base_events.py", line 719, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/home/realkumaraditya/cpython/main.py", line 11, in main
    print(asyncio.get_running_loop())
          ~~~~~~~~~~~~~~~~~~~~~~~~^^
RuntimeError: no running event loop
/home/realkumaraditya/cpython/Lib/asyncio/base_events.py:758: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=False>

Linked PRs

@github-project-automation github-project-automation bot moved this to Todo in asyncio Feb 15, 2025
@kumaraditya303 kumaraditya303 added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes type-bug An unexpected behavior, bug, or error labels Feb 15, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Feb 15, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 15, 2025
kumaraditya303 added a commit that referenced this issue Feb 15, 2025
…GH-130146) (#130147)

gh-130145: fix `loop.run_forever` when loop is already running (GH-130146)
(cherry picked from commit a545749)

Co-authored-by: Kumar Aditya <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

1 participant