Skip to content

Commit

Permalink
pythongh-130145: fix loop.run_forever when loop is already running (p…
Browse files Browse the repository at this point in the history
…ythonGH-130146)

(cherry picked from commit a545749)

Co-authored-by: Kumar Aditya <[email protected]>
  • Loading branch information
kumaraditya303 authored and miss-islington committed Feb 15, 2025
1 parent 692d36f commit 9907a10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ def _run_forever_cleanup(self):

def run_forever(self):
"""Run until stop() is called."""
self._run_forever_setup()
try:
self._run_forever_setup()
while True:
self._run_once()
if self._stopping:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,22 @@ async def main():
self.loop.run_until_complete(main()),
'hello')

def test_get_running_loop_already_running(self):
async def main():
running_loop = asyncio.get_running_loop()
loop = asyncio.new_event_loop()
try:
loop.run_forever()
except RuntimeError:
pass
else:
self.fail("RuntimeError not raised")

self.assertIs(asyncio.get_running_loop(), running_loop)

self.loop.run_until_complete(main())


def test_get_event_loop_returns_running_loop(self):
class TestError(Exception):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :meth:`!asyncio.AbstractEventloop.run_forever` when another loop is already running.

0 comments on commit 9907a10

Please sign in to comment.