Skip to content

Deliver asyncio receive errors to buffered readers - #2169

Merged
zariiii9003 merged 2 commits into
hardbyte:mainfrom
Solarboaters:fix/async-receive-error-delivery
Nov 30, 2026
Merged

zariiii9003 merged 2 commits into
hardbyte:mainfrom
Solarboaters:fix/async-receive-error-delivery

Conversation

@Solarboaters

@Solarboaters Solarboaters commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

Route bus.recv() failures in the asyncio file-descriptor callback to listeners' on_error handlers, recording Notifier.exception as the threaded path does. Stop watching the failing bus descriptor after a receive error so a disconnected device cannot repeatedly enqueue errors. AsyncBufferedReader queues these errors with messages and raises them to the waiting consumer in receive order, instead of leaving it waiting without notification.

Related Issues / Pull Requests

Fixes #1865. The inverter/battery disconnection report motivated the reproduction.

Type of Change

  • Bug fix

Checklist

  • Followed the contribution guide; added regression tests and documentation.
  • Added a towncrier news fragment.
  • All checks and tests pass (tox). Equivalent configured commands were run directly: full Python 3.12 suite, plus Black/Ruff/Pylint, all five mypy targets, Sphinx HTML and doctests on Python 3.14. The full tox interpreter/platform matrix was not run.

Validation

The original three regressions fail on unchanged main. The revised selector test also fails against the previous PR head because it continues delivering data after the receive error; the update stops reception from that bus. Tests now live in the existing notifier/listener test files and exercise production callbacks with a real socketpair.

Current revision: 27 targeted tests passed, one hardware skip; full Python 3.12 suite: 730 passed, 142 skipped, 49 subtests passed. Black, Ruff, Pylint and all five mypy targets pass. The earlier revision also passed Python 3.14 regressions and documentation builds; those were not rerun locally for this follow-up. Hosted CI is pending on the updated head.

The socketpair simulates the CAN backend's receive error; no physical CAN interface, inverter or battery was tested.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: Deliver asyncio receive errors to buffered readers

Findings

  • can/notifier.py, Notifier._on_message_available: the reader stays registered after a receive error. With an event loop, _rx_thread stops after a recv() error; the fd path keeps watching the bus. pyserial raises SerialException on every read of a disconnected device because its fd stays readable, so slcan, serial, robotell and seeedstudio buses re-enter the callback in a busy loop after an unplug. With this PR every pass queues another exception in AsyncBufferedReader (about 28k in 0.2 s with a bus whose recv() always fails), so memory grows without limit. Fix: stop watching the bus in the except branch, matching the thread path:

    except Exception as exc:  # pylint: disable=broad-except
        self._loop.remove_reader(bus.fileno())  # type: ignore[union-attr]
        self.exception = exc
        ...

    Then change test_receive_error_reaches_buffered_reader_in_order to assert that nothing arrives after the error (e.g. await asyncio.sleep(0.05), then self.assertTrue(reader.buffer.empty())) instead of expecting following. This test fails without the fix.

  • test/test_async_receive_errors.py: tests belong in existing files. Move the two Notifier tests (and ReadableBus) into AsyncNotifierTest in test/notifier_test.py, and test_get_message_raises_error_and_stop_rejects_new_errors into test/listener_test.py, which already covers AsyncBufferedReader.

  • self.assertIn(bus.fileno(), notifier._readers): tests a private attribute. Remove it. Both fd tests already fail when the bus falls back to the thread path.

Verdict

request changes

@zariiii9003

Copy link
Copy Markdown
Collaborator

the 2 failing runs are unrelated.

Signed-off-by: Andrew Jessop <354059055+Solarboaters@users.noreply.github.com>
@Solarboaters

Copy link
Copy Markdown
Contributor Author

Updated in a4588ea: receive errors now remove the bus reader, and the regression confirms nothing is delivered afterwards. Moved the tests into the existing notifier/listener files and removed the private-reader assertion; 730 tests pass (142 skipped), plus lint and all five mypy targets. No physical CAN hardware tested.

@zariiii9003
zariiii9003 merged commit b7c83b9 into hardbyte:main Nov 30, 2026
28 checks passed
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.

Async notifier does not handle errors

2 participants