Search code examples
pythonpython-asyncio

Bubble up error from asyncio "create_task" in Python


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.


Solution

  • well, the add_done_callback is as good as it gets: if you see the exception in your logs, you had catch the exception - now, just do whatever you want with it.

    Either just relaunch your worker, or create an event or queue with which you can sinalize to the rest of the application they should be done, or, to just crash everything, just close the event loop right there:

            ...
            def should_never_be_done(t: asyncio.Task):
                if t.exception() is not None:
                    logger.exception(t.exception())
                    # Trainwreck: 
                    asyncio.get_running_loop().close()