[Python-checkins] [3.8] bpo-39191: Don't spawn a task before failing (GH-17796) (GH-17820)

Andrew Svetlov webhook-mailer at python.org
Sat Jan 4 04:49:23 EST 2020

commit: 867d8333ce6a7f74191ad464acc609d4a04e4acb
branch: 3.8
author: Andrew Svetlov <andrew.svetlov at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-01-04T11:49:11+02:00

[3.8] bpo-39191: Don't spawn a task before failing (GH-17796) (GH-17820)

(cherry picked from commit 3a5de511596f17575de082dcb8d43d63b2bd2da9)

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>

A Misc/NEWS.d/next/Library/2020-01-02-17-28-03.bpo-39191.ur_scy.rst
M Lib/asyncio/base_events.py

diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index bfd40115bed38..aedf0c5e6d3d2 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -547,14 +547,17 @@ def _asyncgen_firstiter_hook(self, agen):
                     'asyncgen': agen
-    def run_forever(self):
-        """Run until stop() is called."""
-        self._check_closed()
+    def _check_runnung(self):
         if self.is_running():
             raise RuntimeError('This event loop is already running')
         if events._get_running_loop() is not None:
             raise RuntimeError(
                 'Cannot run the event loop while another loop is running')
+    def run_forever(self):
+        """Run until stop() is called."""
+        self._check_closed()
+        self._check_runnung()
         self._thread_id = threading.get_ident()
@@ -586,6 +589,7 @@ def run_until_complete(self, future):
         Return the Future's result, or raise its exception.
+        self._check_runnung()
         new_task = not futures.isfuture(future)
         future = tasks.ensure_future(future, loop=self)
diff --git a/Misc/NEWS.d/next/Library/2020-01-02-17-28-03.bpo-39191.ur_scy.rst b/Misc/NEWS.d/next/Library/2020-01-02-17-28-03.bpo-39191.ur_scy.rst
new file mode 100644
index 0000000000000..138c93c2e4877
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-02-17-28-03.bpo-39191.ur_scy.rst
@@ -0,0 +1,3 @@
+Perform a check for running loop before starting a new task in
+``loop.run_until_complete()`` to fail fast; it prevents the side effect of
+new task spawning before exception raising.

More information about the Python-checkins mailing list