Skip to content

Commit 65aa64f

Browse files
asvetlovmiss-islington
authored andcommitted
bpo-36607: Eliminate RuntimeError raised by asyncio.all_tasks() (GH-13971)
If internal tasks weak set is changed by another thread during iteration. https://bugs.python.org/issue36607
1 parent 1f11cf9 commit 65aa64f

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Lib/asyncio/tasks.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,22 @@ def all_tasks(loop=None):
4242
"""Return a set of all tasks for the loop."""
4343
if loop is None:
4444
loop = events.get_running_loop()
45-
# NB: set(_all_tasks) is required to protect
46-
# from https://bugs.python.org/issue34970 bug
47-
return {t for t in list(_all_tasks)
45+
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
46+
# thread while we do so. Therefore we cast it to list prior to filtering. The list
47+
# cast itself requires iteration, so we repeat it several times ignoring
48+
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
49+
# details.
50+
i = 0
51+
while True:
52+
try:
53+
tasks = list(_all_tasks)
54+
except RuntimeError:
55+
i += 1
56+
if i >= 1000:
57+
raise
58+
else:
59+
break
60+
return {t for t in tasks
4861
if futures._get_loop(t) is loop and not t.done()}
4962

5063

@@ -54,9 +67,22 @@ def _all_tasks_compat(loop=None):
5467
# method.
5568
if loop is None:
5669
loop = events.get_event_loop()
57-
# NB: set(_all_tasks) is required to protect
58-
# from https://bugs.python.org/issue34970 bug
59-
return {t for t in list(_all_tasks) if futures._get_loop(t) is loop}
70+
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
71+
# thread while we do so. Therefore we cast it to list prior to filtering. The list
72+
# cast itself requires iteration, so we repeat it several times ignoring
73+
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
74+
# details.
75+
i = 0
76+
while True:
77+
try:
78+
tasks = list(_all_tasks)
79+
except RuntimeError:
80+
i += 1
81+
if i >= 1000:
82+
raise
83+
else:
84+
break
85+
return {t for t in tasks if futures._get_loop(t) is loop}
6086

6187

6288
def _set_task_name(task, name):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Eliminate :exc:`RuntimeError` raised by :func:`asyncio.all_tasks()` if
2+
internal tasks weak set is changed by another thread during iteration.

0 commit comments

Comments
 (0)