Skip to content

Commit

Permalink
bpo-32610: Fix asyncio.all_tasks() to return only pending tasks. (GH-…
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed May 28, 2018
1 parent fdccfe0 commit 416c1eb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
7 changes: 5 additions & 2 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ Task
Return a set of all tasks for an event loop.

By default all tasks for the current event loop are returned.
If *loop* is ``None``, :func:`get_event_loop` function
is used to get the current loop.

.. classmethod:: current_task(loop=None)

Expand Down Expand Up @@ -567,8 +569,9 @@ Task functions

Return a set of :class:`Task` objects created for the loop.

If *loop* is ``None`` :func:`get_event_loop` is used for getting
current loop.
If *loop* is ``None``, :func:`get_running_loop` is used for getting
current loop (contrary to the deprecated :meth:`Task.all_tasks` method
that uses :func:`get_event_loop`.)

.. versionadded:: 3.7

Expand Down
4 changes: 4 additions & 0 deletions Lib/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from .tasks import *
from .transports import *

# Exposed for _asynciomodule.c to implement now deprecated
# Task.all_tasks() method. This function will be removed in 3.9.
from .tasks import _all_tasks_compat # NoQA

__all__ = (base_events.__all__ +
coroutines.__all__ +
events.__all__ +
Expand Down
3 changes: 1 addition & 2 deletions Lib/asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ async def main():


def _cancel_all_tasks(loop):
to_cancel = [task for task in tasks.all_tasks(loop)
if not task.done()]
to_cancel = tasks.all_tasks(loop)
if not to_cancel:
return

Expand Down
12 changes: 11 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def current_task(loop=None):

def all_tasks(loop=None):
"""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
if futures._get_loop(t) is loop and not t.done()}


def _all_tasks_compat(loop=None):
# Different from "all_task()" by returning *all* Tasks, including
# the completed ones. Used to implement deprecated "Tasks.all_task()"
# method.
if loop is None:
loop = events.get_event_loop()
return {t for t in _all_tasks if futures._get_loop(t) is loop}
Expand Down Expand Up @@ -82,7 +92,7 @@ def all_tasks(cls, loop=None):
"use asyncio.all_tasks() instead",
PendingDeprecationWarning,
stacklevel=2)
return all_tasks(loop)
return _all_tasks_compat(loop)

def __init__(self, coro, *, loop=None):
super().__init__(loop=loop)
Expand Down
29 changes: 27 additions & 2 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1897,8 +1897,10 @@ def kill_me(loop):
# See http://bugs.python.org/issue29271 for details:
asyncio.set_event_loop(self.loop)
try:
self.assertEqual(asyncio.all_tasks(), {task})
self.assertEqual(asyncio.all_tasks(None), {task})
with self.assertWarns(PendingDeprecationWarning):
self.assertEqual(Task.all_tasks(), {task})
with self.assertWarns(PendingDeprecationWarning):
self.assertEqual(Task.all_tasks(None), {task})
finally:
asyncio.set_event_loop(None)

Expand Down Expand Up @@ -2483,6 +2485,9 @@ class TaskLike:
def _loop(self):
return loop

def done(self):
return False

task = TaskLike()
loop = mock.Mock()

Expand All @@ -2496,6 +2501,9 @@ class TaskLike:
def get_loop(self):
return loop

def done(self):
return False

task = TaskLike()
loop = mock.Mock()

Expand All @@ -2504,6 +2512,23 @@ def get_loop(self):
self.assertEqual(asyncio.all_tasks(loop), {task})
self._unregister_task(task)

def test__register_task_3(self):
class TaskLike:
def get_loop(self):
return loop

def done(self):
return True

task = TaskLike()
loop = mock.Mock()

self.assertEqual(asyncio.all_tasks(loop), set())
self._register_task(task)
self.assertEqual(asyncio.all_tasks(loop), set())
self.assertEqual(asyncio.Task.all_tasks(loop), {task})
self._unregister_task(task)

def test__enter_task(self):
task = mock.Mock()
loop = mock.Mock()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make asyncio.all_tasks() return only pending tasks.
4 changes: 2 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module _asyncio
/* identifiers used from some functions */
_Py_IDENTIFIER(__asyncio_running_event_loop__);
_Py_IDENTIFIER(add_done_callback);
_Py_IDENTIFIER(all_tasks);
_Py_IDENTIFIER(_all_tasks_compat);
_Py_IDENTIFIER(call_soon);
_Py_IDENTIFIER(cancel);
_Py_IDENTIFIER(current_task);
Expand Down Expand Up @@ -2125,7 +2125,7 @@ _asyncio_Task_all_tasks_impl(PyTypeObject *type, PyObject *loop)
return NULL;
}

all_tasks_func = _PyObject_GetAttrId(asyncio_mod, &PyId_all_tasks);
all_tasks_func = _PyObject_GetAttrId(asyncio_mod, &PyId__all_tasks_compat);
if (all_tasks_func == NULL) {
return NULL;
}
Expand Down

2 comments on commit 416c1eb

@BearTian
Copy link

@BearTian BearTian commented on 416c1eb Oct 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traceback (most recent call last):
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "D:\Proj\antwork\code\antwork_backend\server\data_handlers\order\playback_hd.py", line 147, in run
    send_to_ws(channel=self.room_name, data=json.loads(msg))
  File "D:\Proj\antwork\code\antwork_backend\server\data_exchange.py", line 26, in send_to_ws
    asyncio.run(channel_layer.group_send(channel, send_data))
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\asyncio\runners.py", line 46, in run
    _cancel_all_tasks(loop)
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\asyncio\runners.py", line 54, in _cancel_all_tasks
    to_cancel = tasks.all_tasks(loop)
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py", line 38, in all_tasks
    return {t for t in _all_tasks
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py", line 38, in <setcomp>
    return {t for t in _all_tasks
  File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\_weakrefset.py", line 61, in __iter__
    for itemref in self.data:
RuntimeError: Set changed size during iteration

@asvetlov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the bug report.
Addressed here: https://bugs.python.org/issue34970

Please sign in to comment.