Skip to content

Commit a545749

Browse files
gh-130145: fix loop.run_forever when loop is already running (#130146)
1 parent a05433f commit a545749

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/asyncio/base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ def _run_forever_cleanup(self):
671671

672672
def run_forever(self):
673673
"""Run until stop() is called."""
674+
self._run_forever_setup()
674675
try:
675-
self._run_forever_setup()
676676
while True:
677677
self._run_once()
678678
if self._stopping:

Lib/test/test_asyncio/test_events.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,22 @@ async def main():
30043004
self.loop.run_until_complete(main()),
30053005
'hello')
30063006

3007+
def test_get_running_loop_already_running(self):
3008+
async def main():
3009+
running_loop = asyncio.get_running_loop()
3010+
loop = asyncio.new_event_loop()
3011+
try:
3012+
loop.run_forever()
3013+
except RuntimeError:
3014+
pass
3015+
else:
3016+
self.fail("RuntimeError not raised")
3017+
3018+
self.assertIs(asyncio.get_running_loop(), running_loop)
3019+
3020+
self.loop.run_until_complete(main())
3021+
3022+
30073023
def test_get_event_loop_returns_running_loop(self):
30083024
class TestError(Exception):
30093025
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :meth:`!asyncio.AbstractEventloop.run_forever` when another loop is already running.

0 commit comments

Comments
 (0)