Tolerate BLE write failures during the encrypted handshake - #81
Open
antoinevalentinHA wants to merge 2 commits into
Open
Tolerate BLE write failures during the encrypted handshake#81antoinevalentinHA wants to merge 2 commits into
antoinevalentinHA wants to merge 2 commits into
Conversation
iamburny
added a commit
to iamburny/bluetti-hmi-screen
that referenced
this pull request
Aug 5, 2026
…eout Inspired by Patrick762/bluetti-bt-lib#81 (mid-handshake writes can legitimately fail if the peripheral drops the link between polls). We don't have that PR's exact bug -- our handshake loop is synchronous, not an async callback that can let an exception escape -- but we had the same underlying gap: writeValue()'s return value was ignored, so a failed write meant sitting through the rest of the 12s handshake window waiting on a reply to a message that was never sent. Now we detect the failure and bail immediately so the outer poll loop retries after RETRY_MS (4s) instead.
Both handshake replies -- the challenge response and the peer pubkey -- are written with an unguarded `write_gatt_char`. Those calls run inside the notification handler, so a BleakError raised there does not propagate to the caller awaiting the read: it surfaces as an unhandled exception from bleak's callback dispatch, with a traceback that does not name the read that stalled. A write can legitimately fail mid-handshake when the peripheral drops the link -- common with battery-powered stations that disconnect between polls. Catching BleakError turns that into a warning and lets the read time out and retry normally, which is already how the surrounding code handles a lost connection. Behaviour on success is unchanged; only the failure path is affected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Drives _notification_handler with a well-formed CHALLENGE frame against a client whose write_gatt_char always raises BleakError. Fails with `bleak.exc.BleakError: Not connected` escaping the handler before the fix, passes after. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
antoinevalentinHA
force-pushed
the
pr/ble-write-error-handling
branch
from
September 1, 2026 13:40
a44c2eb to
0c59d63
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Both handshake replies — the challenge response and the peer pubkey — are written with an unguarded
write_gatt_charinside_notification_handler.Because the handler runs as a bleak notification callback, a
BleakErrorraised there does not propagate to the caller awaitingread(). It surfaces as an unhandled exception from bleak's callback dispatch, with a traceback that never names the read that then stalls until timeout. The failure is real but the log points at the wrong place.A write can legitimately fail mid-handshake when the peripheral drops the link — common with battery-powered stations that disconnect between polls. Catching
BleakErrorturns that into a warning and lets the read time out and retry normally, which is already how the surrounding code handles a lost connection.Behaviour on success is unchanged; only the failure path is affected.
Test
Adds a regression test driving
_notification_handlerwith a well-formedCHALLENGEframe against a client whosewrite_gatt_charalways raises. It fails withbleak.exc.BleakError: Not connectedescaping the handler before the change, and passes after. Full suite green.Relation to other open PRs
Not overlapping with #78, which guards the tail of the same handler (already-completed future) — the two are complementary. #67 reworks connection error handling far more broadly; this PR is deliberately minimal and can be dropped if that direction is preferred.
🤖 Generated with Claude Code