summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/runners.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-01-21 19:56:59 (GMT)
committerGitHub <noreply@github.com>2018-01-21 19:56:59 (GMT)
commita4afcdfa55ddffa4b9ae3b0cf101628c7bff4102 (patch)
tree61649205a7b3d95fed03f643b66c568aab79be3a /Lib/asyncio/runners.py
parentfc2f407829d9817ddacccae6944dd0879cfaca24 (diff)
downloadcpython-a4afcdfa55ddffa4b9ae3b0cf101628c7bff4102.zip
cpython-a4afcdfa55ddffa4b9ae3b0cf101628c7bff4102.tar.gz
cpython-a4afcdfa55ddffa4b9ae3b0cf101628c7bff4102.tar.bz2
bpo-32314: Fix asyncio.run() to cancel runinng tasks on shutdown (#5262)
Diffstat (limited to 'Lib/asyncio/runners.py')
-rw-r--r--Lib/asyncio/runners.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py
index 94d9409..bb54b72 100644
--- a/Lib/asyncio/runners.py
+++ b/Lib/asyncio/runners.py
@@ -2,6 +2,7 @@ __all__ = 'run',
from . import coroutines
from . import events
+from . import tasks
def run(main, *, debug=False):
@@ -42,7 +43,31 @@ def run(main, *, debug=False):
return loop.run_until_complete(main)
finally:
try:
+ _cancel_all_tasks(loop)
loop.run_until_complete(loop.shutdown_asyncgens())
finally:
events.set_event_loop(None)
loop.close()
+
+
+def _cancel_all_tasks(loop):
+ to_cancel = [task for task in tasks.all_tasks(loop)
+ if not task.done()]
+ if not to_cancel:
+ return
+
+ for task in to_cancel:
+ task.cancel()
+
+ loop.run_until_complete(
+ tasks.gather(*to_cancel, loop=loop, return_exceptions=True))
+
+ for task in to_cancel:
+ if task.cancelled():
+ continue
+ if task.exception() is not None:
+ loop.call_exception_handler({
+ 'message': 'unhandled exception during asyncio.run() shutdown',
+ 'exception': task.exception(),
+ 'task': task,
+ })