Skip to content

Notifier: route listener exceptions only to the failing listener's on_error - #2183

Merged
zariiii9003 merged 3 commits into
hardbyte:mainfrom
zariiii9003:feature/notifier-isolate-listener-errors
Dec 5, 2026
Merged

zariiii9003 merged 3 commits into
hardbyte:mainfrom
zariiii9003:feature/notifier-isolate-listener-errors

Conversation

@zariiii9003

Copy link
Copy Markdown
Collaborator

Summary of Changes

  • When a listener (or the coroutine it returns) raises, can.Notifier now calls on_error() of that listener only and keeps delivering the message to all other listeners.
  • If the failing listener does not handle the exception, the notifier logs it, removes the listener and calls its stop(). It no longer kills the receive thread (threaded mode) or reports to the event loop's exception handler (asyncio mode).
  • An exception raised by on_error() itself is logged and counts as "not handled", also for bus receive errors.
  • Documented in Notifier.__init__, Listener.on_error and Notifier.exception.
 _on_message_received(msg)
   for callback in listeners:
-    callback(msg)                      # exception aborts delivery to remaining listeners
+    try: callback(msg)
+    except exc: _on_listener_error(callback, exc); continue

-on listener/task exception
-  _on_error(exc)                       # every listener's on_error()
-  if nobody handled: raise             # rx thread dies / loop exception handler
+_on_listener_error(callback, exc)
+  if callback.on_error(exc) returns:   # handled
+    log debug
+  elif notifier running:
+    remove callback, callback.stop(), log error
+  else:
+    log error

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe): behaviour change in Listener.on_error semantics, see Additional Notes

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Evidence

Behaviour on main vs this PR (virtual bus, one listener that raises ValueError on every message):

Scenario main This PR
Threaded, raising callable + BufferedReader receive thread dies, reader gets [] callable removed, reader gets [0, 1, 2]
asyncio, raising callable + AsyncBufferedReader get_message() raises the callable's ValueError instead of returning messages reader returns messages [0, 1]
Threaded, raising callable + another listener with on_error other listener absorbs the errors, callable stays registered other listener sees no errors, callable is removed

Tests in test/notifier_test.py cover per-listener routing, continued delivery, removal and stop() of the failed listener, failing on_error()/stop(), and no second stop() when a coroutine fails after Notifier.stop(). They wait on events instead of fixed sleeps, so they don't depend on CI scheduling.

Merge Danger

Door: two-way. Code-only change, revertible without data or API migration.

Blast Radius: error-handling consumers.

The last row of the table changes v4.6.1 behaviour: Listener.on_error was documented as handling "any exception in the receive thread", so a listener with on_error could keep a raising sibling alive. That no longer works. A listener must implement on_error() itself to survive its own exceptions. Keeping the old routing would preserve the AsyncBufferedReader misrouting (row 2), so the change is intended. The news fragment describes the migration.

🤖 Generated with AI

@zariiii9003
zariiii9003 merged commit 737fbf2 into hardbyte:main Dec 5, 2026
26 checks passed
@zariiii9003
zariiii9003 deleted the feature/notifier-isolate-listener-errors branch December 5, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant