Skip to content

Make BLNETDirect recover from connection errors - #51

Merged
nielstron merged 5 commits into
masterfrom
fix/direct-robustness
Nov 16, 2026
Merged

nielstron merged 5 commits into
masterfrom
fix/direct-robustness

Conversation

@nielstron

@nielstron nielstron commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Cherry-picked from #48 (by Alexander Eisele), without the fork-specific changes (uv migration, Python 3.11 requirement, pyupgrade, PyPI/README fork changes, CLAUDE.md, version bumps).

  • Close the socket after a failed connection attempt (ResourceWarning on 3.14+).
  • Pass BLNET's timeout to BLNETDirect (it always used 60 s).
  • Reset the connection after any read error, so a long-lived BLNET object (e.g. in Home Assistant) recovers instead of reusing a dead socket; malformed replies raise ConnectionError.
  • Wait 2 s when the BL-Net answers GET_LATEST with "no data" (0xAB) instead of retrying instantly.
  • Read GET_LATEST replies completely: a reply split over TCP segments shifted all following replies by a byte, and the weak checksum let ~1/128 of them through as garbage values (e.g. -3807 °C).

Changes vs. the original commits (test-only): tests expect socket.timeout instead of TimeoutError (identical on 3.10+, but CI here still runs 3.8/3.9), and the mock server's drop_connections ignores ENOTCONN, which macOS raises for already-closed sockets.

Each commit passes on its own (3.8 and 3.13), and its new tests fail with its code change reverted.

@nielstron
nielstron force-pushed the fix/direct-robustness branch from 466579a to aded4a7 Compare November 16, 2026 14:27
@nielstron
nielstron added this pull request to stack #56 November 16, 2026 14:31
@nielstron
nielstron removed this pull request from stack #56 November 16, 2026 14:34
@nielstron
nielstron changed the base branch from fix/direct-dl-mode to master November 16, 2026 14:34
@nielstron
nielstron force-pushed the fix/direct-robustness branch from aded4a7 to abd0a19 Compare November 16, 2026 14:35
dereisele and others added 5 commits November 16, 2026 15:57
When connecting to one of the resolved addresses failed, the socket was
dropped without closing it, which Python 3.14+ reports as
ResourceWarning. Only catch OSError instead of everything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BLNETDirect always used its default of 60 seconds, so a BL-NET that
stopped answering blocked fetch() for a minute regardless of the
timeout given to BLNET.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A timeout or other error while reading left the socket open, and since
_connect() only connects without a socket, every later call reused the
dead connection. A BLNET object, as kept by Home Assistant, never
recovered until it was recreated.

- Close the socket and reset the read state on any error in get_latest,
  get_count, _get_data and the mode check in the constructor, which also
  leaked its socket on timeout
- Raise ConnectionError when the BL-Net closes the connection or sends a
  malformed reply instead of IndexError or struct.error
- Use sendall instead of send
- _get_data no longer swallows errors with a return in finally

Errors from the direct connection are now always OSError subclasses:
ConnectionError or TimeoutError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A real BL-Net answers GET_LATEST with a single 0xAB byte ("no data
available", see TA's interface spec) for up to about 5 seconds after a
read. The retries ran without any pause, so every read shortly after
the previous one failed with ConnectionError. Wait 2 seconds after such
a reply before the next try.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BL-NETs sometimes send the first byte of a GET_LATEST reply in a TCP
segment of its own. _query() stopped after that byte, so the rest of
the reply was read as the start of the next one and all following
replies were shifted by one byte. The sum checksum accepts about one in
128 of those shifted datasets, which showed up as occasional garbage
values like -3807 °C or jumps of the energy meter.

Derive the reply length from its first byte and read it completely,
and reconnect after a reply with a bad checksum so that stray bytes
can't end up in the next reply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KJJTUv5YaVyYB197zeuqBp
@nielstron
nielstron force-pushed the fix/direct-robustness branch from abd0a19 to 592b639 Compare November 16, 2026 14:58
@nielstron
nielstron merged commit 592b639 into master Nov 16, 2026
14 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.

3 participants