summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorRomain Picard <romain.picard@oakbits.com>2019-05-07 18:58:24 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-05-07 18:58:24 (GMT)
commitb35acc5b3a0148c5fd4462968b310fb436726d5a (patch)
tree844dd82ca54e012a0416a1abb959d9a7b396e4bc /Lib/asyncio
parentf7bda5c5729a3cc69b32c2a3baf5c64dea666d33 (diff)
downloadcpython-b35acc5b3a0148c5fd4462968b310fb436726d5a.zip
cpython-b35acc5b3a0148c5fd4462968b310fb436726d5a.tar.gz
cpython-b35acc5b3a0148c5fd4462968b310fb436726d5a.tar.bz2
bpo-35125: remove inner callback on outer cancellation in asyncio shield (GH-10340)
When the future returned by shield is cancelled, its completion callback of the inner future is not removed. This makes the callback list of inner inner future grow each time a shield is created and cancelled. This change unregisters the callback from the inner future when the outer future is cancelled. https://bugs.python.org/issue35125
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/tasks.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index b007b74..211b912 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -818,7 +818,7 @@ def shield(arg, *, loop=None):
loop = futures._get_loop(inner)
outer = loop.create_future()
- def _done_callback(inner):
+ def _inner_done_callback(inner):
if outer.cancelled():
if not inner.cancelled():
# Mark inner's result as retrieved.
@@ -834,7 +834,13 @@ def shield(arg, *, loop=None):
else:
outer.set_result(inner.result())
- inner.add_done_callback(_done_callback)
+
+ def _outer_done_callback(outer):
+ if not inner.done():
+ inner.remove_done_callback(_inner_done_callback)
+
+ inner.add_done_callback(_inner_done_callback)
+ outer.add_done_callback(_outer_done_callback)
return outer