From 1e0dd254d07d078168a548e4362985b8ec2477a7 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sat, 13 Oct 2018 11:21:47 +0300 Subject: [PATCH 1/2] Protect tasks weak set manipulation in asyncio.all_tasks() --- Lib/asyncio/tasks.py | 8 ++++++-- .../next/Library/2018-10-13-11-14-13.bpo-34970.SrJTY7.rst | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-13-11-14-13.bpo-34970.SrJTY7.rst diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 743e82baff7aac..1355ab835be188 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -42,7 +42,9 @@ 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 + # NB: set(_all_tasks) is required to protect + # from https://bugs.python.org/issue34970 bug + return {t for t in set(_all_tasks) if futures._get_loop(t) is loop and not t.done()} @@ -52,7 +54,9 @@ def _all_tasks_compat(loop=None): # method. if loop is None: loop = events.get_event_loop() - return {t for t in _all_tasks if futures._get_loop(t) is loop} + # NB: set(_all_tasks) is required to protect + # from https://bugs.python.org/issue34970 bug + return {t for t in set(_all_tasks) if futures._get_loop(t) is loop} def _set_task_name(task, name): diff --git a/Misc/NEWS.d/next/Library/2018-10-13-11-14-13.bpo-34970.SrJTY7.rst b/Misc/NEWS.d/next/Library/2018-10-13-11-14-13.bpo-34970.SrJTY7.rst new file mode 100644 index 00000000000000..a58b3dd354194a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-13-11-14-13.bpo-34970.SrJTY7.rst @@ -0,0 +1 @@ +Protect tasks weak set manipulation in ``asyncio.all_tasks()`` From d70e998957d4cb90412c6e492c1c548bd09b0998 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sat, 13 Oct 2018 20:48:25 +0300 Subject: [PATCH 2/2] Replace set() with list() --- Lib/asyncio/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 1355ab835be188..15422da1b3b2e0 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -44,7 +44,7 @@ def all_tasks(loop=None): loop = events.get_running_loop() # NB: set(_all_tasks) is required to protect # from https://bugs.python.org/issue34970 bug - return {t for t in set(_all_tasks) + return {t for t in list(_all_tasks) if futures._get_loop(t) is loop and not t.done()} @@ -56,7 +56,7 @@ def _all_tasks_compat(loop=None): loop = events.get_event_loop() # NB: set(_all_tasks) is required to protect # from https://bugs.python.org/issue34970 bug - return {t for t in set(_all_tasks) if futures._get_loop(t) is loop} + return {t for t in list(_all_tasks) if futures._get_loop(t) is loop} def _set_task_name(task, name):