enthought / traits

Observable typed attributes for Python classes
Other
421 stars 85 forks source link

Add error handling for async observers #1783

Open mdickinson opened 3 months ago

mdickinson commented 3 months ago

For normal (synchronous) Traits observers, if an exception occurs then we execute Traits's exception handler, which by default simply logs the exception. The relevant code (or at least part of it) is here: https://github.com/enthought/traits/blob/53654f9bf8bfe353a63ab3a71fdd0869609e9502/traits/observation/_trait_event_notifier.py#L121-L124

For asynchronous Traits observers as introduced by PR #1770, exceptions occurring in the observer are swallowed entirely, with no easy way to log the exception.

It would be useful to have the option to act on the exception in some way (for example, by logging). This would probably entail adding a Traits-specific wrapper around the coroutine to be executed.

mdickinson commented 3 months ago

A somewhat related discussion thread: https://discuss.python.org/t/asyncio-tasks-and-exception-handling-recommended-idioms/23806