Skip to content
Open
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
11 changes: 10 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,19 @@ def _done_callback(fut, cur_task=cur_task):
return outer


def _log_on_exception(fut):
def _log_on_exception(fut, deferred=False):
if fut.cancelled():
return

if not deferred:
# gh-156321: an exception retrieved by an awaiter must not be reported
fut._loop.call_soon(_log_on_exception, fut, True)
return

if not fut._log_traceback:
# The exception was retrieved, nothing to report.
return

exc = fut.exception()
if exc is None:
return
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,21 @@ def test_shield_cancel_outer_exception(self):
test_utils.run_briefly(self.loop)
mock_handler.assert_called_once()

def test_shield_cancel_outer_exception_retrieved(self):
# gh-156321: an exception retrieved by an awaiter must not be reported
mock_handler = mock.Mock()
self.loop.set_exception_handler(mock_handler)
inner = self.new_future(self.loop)
outer = asyncio.shield(inner)
test_utils.run_briefly(self.loop)
outer.cancel()
test_utils.run_briefly(self.loop)
inner.set_exception(Exception('foo'))
self.assertIsNotNone(inner.exception())
test_utils.run_briefly(self.loop)
test_utils.run_briefly(self.loop)
mock_handler.assert_not_called()

def test_shield_cancel_outer_in_task(self):
inner = self.new_future(self.loop)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`asyncio.shield` reporting an exception that was already
retrieved.
Loading