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 asvetlov
Recipients asvetlov, gvanrossum, iritkatriel, jinty, r.david.murray, vstinner, yselivanov
Date 2022-03-22.01:10:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1647911442.27.0.812344226828.issue25489@roundup.psfhosted.org>
In-reply-to
Content
I can describe what happens with test_sys_exit_in_exception_handler.py 

1. The 'boom' task raises an exception.
2. The task is not awaited, Future.__del__ calls the exception handler with 'Task exception was never retrieved' message.
3. The custom handler raises SystemExit.
4. SystemExit bubbles up and swallowed by __del__, the __del__ method cannot re-raise.

The question is: what is the behavior expected?
a) Now an exception raised by a custom exception handler is swallowed in this particular case (but is propagated if `loop.call_exception_handler()` is called from a function other than __del__).
b) Yuri suggested re-schedule an exception generated by `loop.call_exception_handler` by `loop.call_soon()`.  asyncio.Handle catches it and... calls `call_exception_handler()` with 'Exception in callback ...' message.  At the end, we have an endless recursion.
c) asyncio loop can call `loop.stop()` if an exception is raised by `loop.call_exception_handler()` from __del__.  I think this behavior is terrible: a subtle error can terminate asyncio program.
d) Assume that a custom exception handler should not raise an exception. Catch all exceptions in `call_exception_handler`, call sys.unraisablehook(), and suppress the exception.

I believe that d) is the best thing that we can do here.

I can prepare a fix if we agree on the solution.
History
Date User Action Args
2022-03-22 01:10:42asvetlovsetrecipients: + asvetlov, gvanrossum, vstinner, r.david.murray, yselivanov, jinty, iritkatriel
2022-03-22 01:10:42asvetlovsetmessageid: <1647911442.27.0.812344226828.issue25489@roundup.psfhosted.org>
2022-03-22 01:10:42asvetlovlinkissue25489 messages
2022-03-22 01:10:42asvetlovcreate