Title: shielded task exception never retrieved when outer task cancelled
Type: Stage:
Components: asyncio Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, natano, yselivanov
Priority: normal Keywords: patch

Created on 2020-12-14 10:36 by natano, last changed 2020-12-15 09:44 by natano.

File name Uploaded Description Edit natano, 2020-12-14 10:36 Short script for showing the issue
regression-test.patch natano, 2020-12-14 10:38 Regression test
Messages (4)
msg382975 - (view) Author: Martin Natano (natano) * Date: 2020-12-14 10:36
A task created with asyncio.shield() never retrieves the task exception, which results in a log message being generated. See the attached script for a minimal example. Output looks something like this:

Task exception was never retrieved
future: <Task finished name='Task-2' coro=<some_coroutine() done, defined at /Users/natano/python-bug/> exception=Exception('foo')>
Traceback (most recent call last):
  File "/Users/natano/python-bug/", line 6, in some_coroutine
    raise Exception('foo')
Exception: foo

I believe this behaviour is not intended and is an unintended side-effect of commit b35acc5b3a0148c5fd4462968b310fb436726d5a ( The _inner_done_callback has has code to retrieve the exception when the outer task was cancelled, but since that commit the _inner_done_callback is not called anymore when the outer task was cancelled.

(Tested with 3.8, 3.9 and 3.10. I don't have a python 3.7 version available for testing, but I think this issue also affects 3.7.)
msg382976 - (view) Author: Martin Natano (natano) * Date: 2020-12-14 10:38
I'm not sure how to fix this, but here is a regression test for the issue. (failing on the current master branch)
msg382979 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-12-14 10:49
Thanks for the report.

I didn't test but looks like `outer.exception()` call can suppress the message about not retrieved exception.
msg383039 - (view) Author: Martin Natano (natano) * Date: 2020-12-15 09:44
How would calling `outer.exception()` suppress the message? This happens when the outer task is cancelled while the inner task is still running. In that case the exception is never set on outer.
Date User Action Args
2020-12-15 09:44:41natanosetmessages: + msg383039
2020-12-14 10:49:10asvetlovsetmessages: + msg382979
2020-12-14 10:38:10natanosetfiles: + regression-test.patch
keywords: + patch
messages: + msg382976
2020-12-14 10:36:21natanocreate