Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address comments
  • Loading branch information
asvetlov committed Jun 11, 2019
commit eb3d2a39684a4d6573812c4594f026db9bb3449a
30 changes: 18 additions & 12 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ def all_tasks(loop=None):
"""Return a set of all tasks for the loop."""
if loop is None:
loop = events.get_running_loop()
# NB: list(_all_tasks) is required to protect
# from https://bugs.python.org/issue34970 bug
# NB: Have to repeat on RuntimeError, other thread
# can modify _all_tasks during list(_all_tasks) call
# https://bugs.python.org/issue36607
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
# thread while we do so. Therefore we cast it to list prior to filtering. The list
# 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:
pass
i += 1
if i >= 1000:
raise
else:
break
return {t for t in tasks
Expand All @@ -64,16 +67,19 @@ def _all_tasks_compat(loop=None):
# method.
if loop is None:
loop = events.get_event_loop()
# NB: list(_all_tasks) is required to protect
# from https://bugs.python.org/issue34970 bug
# NB: Have to repeat on RuntimeError, other thread
# can modify _all_tasks during list(_all_tasks) call
# https://bugs.python.org/issue36607
# Looping over a WeakSet (_all_tasks) isn't safe as it can be updated from another
# thread while we do so. Therefore we cast it to list prior to filtering. The list
# 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:
pass
i += 1
if i >= 1000:
raise
else:
break
return {t for t in tasks if futures._get_loop(t) is loop}
Expand Down