[issue25489] sys.exit() caught in exception handler

R. David Murray report at bugs.python.org
Tue Oct 27 10:59:47 EDT 2015


R. David Murray added the comment:

Ah, I misunderstood your report, because I didn't actually run the example.  The exception is being ignored because it is raised during a __del__ method execution.  This has nothing to do with set_exception_handler.  And in fact if you raise sys.exit in an excepthook handler it is ignored completely, so the cases are parallel in that sense.  Interestingly, if you replace your Exception in boom with sys.exit, your sys.exit in the handler actually works, because in that case the handler isn't getting called from the Task __del__.

I don't think there's a bug in python here, I think the bug is in your program.  You aren't yielding from the task, so the exception from boom only gets dealt with during Task finalization.  I find this to be the most confusing part of asyncio programming, myself.  I don't know why the handler doesn't get called just from creating the Task and running the loop. I often end up writing wrapper functions that yield from a function that does the work inside a try/except, and/or yield from the task in a 'wait_closed' method.

Maybe someone with more asyncio inside knowledge will be able to clear this up, and we can figure out a doc improvement.
I suspect we need a "best practices" document about this...

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25489>
_______________________________________


More information about the Python-bugs-list mailing list