catch exceptions in callbacks

This commit is contained in:
əlemi 2021-11-22 13:08:31 +01:00
parent f47d82733c
commit ce22e26458

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import uuid import uuid
import logging
from typing import Dict, List, Any, Callable from typing import Dict, List, Any, Callable
@ -8,6 +9,8 @@ class CallbacksHolder:
_callbacks : Dict[Any, List[Callable]] _callbacks : Dict[Any, List[Callable]]
_tasks : Dict[uuid.UUID, asyncio.Event] _tasks : Dict[uuid.UUID, asyncio.Event]
_logger : logging.Logger
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._callbacks = {} self._callbacks = {}
@ -25,7 +28,11 @@ class CallbacksHolder:
def _wrap(self, cb:Callable, uid:uuid.UUID) -> Callable: def _wrap(self, cb:Callable, uid:uuid.UUID) -> Callable:
async def wrapper(*args): async def wrapper(*args):
ret = await cb(*args) try:
ret = await cb(*args)
except Exception:
logging.exception("Exception processing callback")
ret = None
self._tasks[uid].set() self._tasks[uid].set()
self._tasks.pop(uid) self._tasks.pop(uid)
return ret return ret