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
This commit is contained in:
parent
1f11cf9521
commit
65aa64fae8
|
@ -42,9 +42,22 @@ def all_tasks(loop=None):
|
||||||
"""Return a set of all tasks for the loop."""
|
"""Return a set of all tasks for the loop."""
|
||||||
if loop is None:
|
if loop is None:
|
||||||
loop = events.get_running_loop()
|
loop = events.get_running_loop()
|
||||||
# NB: set(_all_tasks) is required to protect
|
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
|
||||||
# from https://bugs.python.org/issue34970 bug
|
# thread while we do so. Therefore we cast it to list prior to filtering. The list
|
||||||
return {t for t in list(_all_tasks)
|
# cast itself requires iteration, so we repeat it several times ignoring
|
||||||
|
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
|
||||||
|
# details.
|
||||||
|
i = 0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
tasks = list(_all_tasks)
|
||||||
|
except RuntimeError:
|
||||||
|
i += 1
|
||||||
|
if i >= 1000:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return {t for t in tasks
|
||||||
if futures._get_loop(t) is loop and not t.done()}
|
if futures._get_loop(t) is loop and not t.done()}
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,9 +67,22 @@ def _all_tasks_compat(loop=None):
|
||||||
# method.
|
# method.
|
||||||
if loop is None:
|
if loop is None:
|
||||||
loop = events.get_event_loop()
|
loop = events.get_event_loop()
|
||||||
# NB: set(_all_tasks) is required to protect
|
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
|
||||||
# from https://bugs.python.org/issue34970 bug
|
# thread while we do so. Therefore we cast it to list prior to filtering. The list
|
||||||
return {t for t in list(_all_tasks) if futures._get_loop(t) is loop}
|
# cast itself requires iteration, so we repeat it several times ignoring
|
||||||
|
# RuntimeErrors (which are not very likely to occur). See issues 34970 and 36607 for
|
||||||
|
# details.
|
||||||
|
i = 0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
tasks = list(_all_tasks)
|
||||||
|
except RuntimeError:
|
||||||
|
i += 1
|
||||||
|
if i >= 1000:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return {t for t in tasks if futures._get_loop(t) is loop}
|
||||||
|
|
||||||
|
|
||||||
def _set_task_name(task, name):
|
def _set_task_name(task, name):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Eliminate :exc:`RuntimeError` raised by :func:`asyncio.all_tasks()` if
|
||||||
|
internal tasks weak set is changed by another thread during iteration.
|
Loading…
Reference in New Issue