bpo-34970: Protect tasks weak set manipulation in asyncio.all_tasks() (GH-9837) (GH-9849)
https://bugs.python.org/issue34970
(cherry picked from commit 97cf0828727ac2a269c89c5aa09570a69a22c83c)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 72792a2..416c346 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -35,7 +35,9 @@
"""Return a set of all tasks for the loop."""
if loop is None:
loop = events.get_running_loop()
- return {t for t in _all_tasks
+ # NB: set(_all_tasks) is required to protect
+ # from https://bugs.python.org/issue34970 bug
+ return {t for t in list(_all_tasks)
if futures._get_loop(t) is loop and not t.done()}
@@ -45,7 +47,9 @@
# method.
if loop is None:
loop = events.get_event_loop()
- return {t for t in _all_tasks if futures._get_loop(t) is loop}
+ # NB: set(_all_tasks) is required to protect
+ # from https://bugs.python.org/issue34970 bug
+ return {t for t in list(_all_tasks) if futures._get_loop(t) is loop}
class Task(futures._PyFuture): # Inherit Python Task implementation