https://github.com/python/cpython/commit/1a7b8c0f4d7aaa4868ec601e324b7df580f24b69
commit: 1a7b8c0f4d7aaa4868ec601e324b7df580f24b69
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: kumaraditya303 <kumaradi...@python.org>
date: 2025-02-15T09:55:08Z
summary:

[3.13] gh-130145: fix `loop.run_forever` when loop is already running 
(GH-130146) (#130147)

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

Co-authored-by: Kumar Aditya <kumaradi...@python.org>

files:
A Misc/NEWS.d/next/Library/2025-02-15-07-50-37.gh-issue-130145.I0CkV0.rst
M Lib/asyncio/base_events.py
M Lib/test/test_asyncio/test_events.py

diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index b4a654c2dc2c66..e19a364886ab5f 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -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:
diff --git a/Lib/test/test_asyncio/test_events.py 
b/Lib/test/test_asyncio/test_events.py
index c92bc81618c989..06a72aa798092b 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -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
diff --git 
a/Misc/NEWS.d/next/Library/2025-02-15-07-50-37.gh-issue-130145.I0CkV0.rst 
b/Misc/NEWS.d/next/Library/2025-02-15-07-50-37.gh-issue-130145.I0CkV0.rst
new file mode 100644
index 00000000000000..9c8c469bff5663
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-02-15-07-50-37.gh-issue-130145.I0CkV0.rst
@@ -0,0 +1 @@
+Fix :meth:`!asyncio.AbstractEventloop.run_forever` when another loop is 
already running.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to