Skip to content

bpo-35125: remove inner callback on outer cancellation in asyncio shield #10340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,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.
Expand All @@ -832,7 +832,13 @@ def _done_callback(inner):
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


Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,14 +1768,23 @@ def test_shield_exception(self):
test_utils.run_briefly(self.loop)
self.assertIs(outer.exception(), exc)

def test_shield_cancel(self):
def test_shield_cancel_inner(self):
inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
inner.cancel()
test_utils.run_briefly(self.loop)
self.assertTrue(outer.cancelled())

def test_shield_cancel_outer(self):
inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
outer.cancel()
test_utils.run_briefly(self.loop)
self.assertTrue(outer.cancelled())
self.assertEqual(0, 0 if outer._callbacks is None else len(outer._callbacks))

def test_shield_shortcut(self):
fut = self.new_future(self.loop)
fut.set_result(42)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Asyncio: Remove inner callback on outer cancellation in shield