Skip to content

Commit 9907a10

Browse files
kumaraditya303miss-islington
authored andcommitted
pythongh-130145: fix loop.run_forever when loop is already running (pythonGH-130146)
(cherry picked from commit a545749) Co-authored-by: Kumar Aditya <[email protected]>
1 parent 692d36f commit 9907a10

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
@@ -2889,6 +2889,22 @@ async def main():
28892889
self.loop.run_until_complete(main()),
28902890
'hello')
28912891

2892+
def test_get_running_loop_already_running(self):
2893+
async def main():
2894+
running_loop = asyncio.get_running_loop()
2895+
loop = asyncio.new_event_loop()
2896+
try:
2897+
loop.run_forever()
2898+
except RuntimeError:
2899+
pass
2900+
else:
2901+
self.fail("RuntimeError not raised")
2902+
2903+
self.assertIs(asyncio.get_running_loop(), running_loop)
2904+
2905+
self.loop.run_until_complete(main())
2906+
2907+
28922908
def test_get_event_loop_returns_running_loop(self):
28932909
class TestError(Exception):
28942910
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)