Skip to content

Tolerate BLE write failures during the encrypted handshake - #81

Open
antoinevalentinHA wants to merge 2 commits into
Patrick762:mainfrom
antoinevalentinHA:pr/ble-write-error-handling
Open

Tolerate BLE write failures during the encrypted handshake#81
antoinevalentinHA wants to merge 2 commits into
Patrick762:mainfrom
antoinevalentinHA:pr/ble-write-error-handling

Conversation

@antoinevalentinHA

Copy link
Copy Markdown
Contributor

Both handshake replies — the challenge response and the peer pubkey — are written with an unguarded write_gatt_char inside _notification_handler.

Because the handler runs as a bleak notification callback, a BleakError raised there does not propagate to the caller awaiting read(). 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 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.

Test

Adds a regression test driving _notification_handler with a well-formed CHALLENGE frame against a client whose write_gatt_char always raises. It fails with bleak.exc.BleakError: Not connected escaping 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

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.
antoinevalentinHA and others added 2 commits September 1, 2026 15:39
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
antoinevalentinHA force-pushed the pr/ble-write-error-handling branch from a44c2eb to 0c59d63 Compare September 1, 2026 13:40
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