I have a long running task that may raise an exception randomly. This task is an infinite loop: it's not expected to finish or to be awaited. It is tied to an instance of a specific class, I cancel the task when the instance is garbage collected.
I'd like to raise potential errors "outside" of this task but failed to do so.
def start_worker(self):
self.worker = asyncio.create_task(
self._worker()
)
def should_never_be_done(t: asyncio.Task):
if t.exception() is not None:
logger.exception(t.exception())
raise t.exception()
raise Exception(
"_worker task is done, yet it should never finish")
self.worker.add_done_callback(should_never_be_done)
I see the exception in my log, but it doesn't crash the whole app, which I want in this case instead of a silent fail leading to infinite loadings in my UI.
This related question show only how to log the error but do not aim at raising it, hence the separate question.