From 0b1fb255a930cf072684a78412b1c9434efd514d Mon Sep 17 00:00:00 2001 From: Charles Baynham Date: Mon, 9 Sep 2019 12:30:13 +0100 Subject: [PATCH] tools: Wrap Task _do() calls in a generic exception handler Signed-off-by: Charles Baynham --- artiq/tools.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/artiq/tools.py b/artiq/tools.py index 192658661..d7d20bae1 100644 --- a/artiq/tools.py +++ b/artiq/tools.py @@ -240,7 +240,16 @@ async def asyncio_wait_or_cancel(fs, **kwargs): class TaskObject: def start(self): - self.task = asyncio.ensure_future(self._do()) + async def log_exceptions(awaitable): + try: + return await awaitable() + except asyncio.CancelledError: + raise + except Exception: + logger.error("Unhandled exception in TaskObject task body", exc_info=True) + raise + + self.task = asyncio.ensure_future(log_exceptions(self._do)) async def stop(self): self.task.cancel()