This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author chris.jerdonek
Recipients asvetlov, chris.jerdonek, cjrh, rshakh, yselivanov
Date 2020-05-19.00:58:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1589849916.32.0.245439177086.issue39839@roundup.psfhosted.org>
In-reply-to
Content
I took a look at this.

Basically, the reason the exception handler isn't firing when the task is still in scope is that the exception handler is only a handler of "last resort." You can see that the exception handler is called inside Task.__del__ here:
https://github.com/python/cpython/blob/3764069f3ba2a7e932837ae19265059339dc86e3/Lib/asyncio/tasks.py#L167-L176

The reason to do it this way is that if the Task is still in scope, there's still a chance that the caller might want to handle the exception themselves, e.g. by awaiting on the Task themselves. It's only when all references to the task go away that you know that the Task's exceptions can no longer be handled manually.

Maybe this would be worth clarifying somewhere in the Error Handling API docs:
https://docs.python.org/3/library/asyncio-eventloop.html#error-handling-api
If you agree, we can change this issue to a documentation issue.
History
Date User Action Args
2020-05-19 00:58:36chris.jerdoneksetrecipients: + chris.jerdonek, asvetlov, cjrh, yselivanov, rshakh
2020-05-19 00:58:36chris.jerdoneksetmessageid: <1589849916.32.0.245439177086.issue39839@roundup.psfhosted.org>
2020-05-19 00:58:36chris.jerdoneklinkissue39839 messages
2020-05-19 00:58:35chris.jerdonekcreate