Skip to content

Commit 5dbb1b7

Browse files
bpo-34970: Protect tasks weak set manipulation in asyncio.all_tasks() (GH-9837) (GH-9849)
https://bugs.python.org/issue34970 (cherry picked from commit 97cf082) Co-authored-by: Andrew Svetlov <[email protected]>
1 parent d075642 commit 5dbb1b7

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Lib/asyncio/tasks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def all_tasks(loop=None):
3535
"""Return a set of all tasks for the loop."""
3636
if loop is None:
3737
loop = events.get_running_loop()
38-
return {t for t in _all_tasks
38+
# NB: set(_all_tasks) is required to protect
39+
# from https://bugs.python.org/issue34970 bug
40+
return {t for t in list(_all_tasks)
3941
if futures._get_loop(t) is loop and not t.done()}
4042

4143

@@ -45,7 +47,9 @@ def _all_tasks_compat(loop=None):
4547
# method.
4648
if loop is None:
4749
loop = events.get_event_loop()
48-
return {t for t in _all_tasks if futures._get_loop(t) is loop}
50+
# NB: set(_all_tasks) is required to protect
51+
# from https://bugs.python.org/issue34970 bug
52+
return {t for t in list(_all_tasks) if futures._get_loop(t) is loop}
4953

5054

5155
class Task(futures._PyFuture): # Inherit Python Task implementation
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Protect tasks weak set manipulation in ``asyncio.all_tasks()``

0 commit comments

Comments
 (0)