BLE: a connect that fails must put the link back down

MeshCore.create_ble builds its client, calls connect() on it, and hands
it back only on success. A connect that *raises* therefore left that
client — and the bleak link it had already opened — orphaned inside the
library with no reference we could close. Our _mc was never assigned, so
disconnect() was a no-op and the probe's _safe_disconnect closed nothing.

That is not a corner: a PIN-protected companion answers the unbonded
notify-subscribe with a GATT authentication error, and the picker's first
attempt is always PIN-less. So selecting an unbonded companion leaked the
link, Windows held it for the life of the process, and the peripheral —
still believing it had a peer — stopped advertising. The PIN dialog then
asked for a code it could no longer deliver: the retry paired at the OS
level (the bond really is created) but could not form a GATT session, and
the user was told their correct PIN belonged to something that "isn't a
MeshCore companion". Since Unpair & quit is what leaves you unbonded, the
whole thing read as unpairing having changed the PIN.

Assemble the two objects here instead. _connect_owned_ble keeps the
handle, so both failure shapes close it: a raise (closed, then re-raised)
and a None handshake (closed, then reported as "not a companion"). The
teardown is shielded — the other way in is a probe whose wait_for expired
and cancelled the handshake, where a plain await would be cancelled the
moment it suspended and abandon the very close it was called to perform.

The picker's accepted PIN now also rides on the context, so a mid-session
reconnect doesn't re-run the whole "needs a PIN" dance against a companion
we already authenticated to. Session-lifetime only; nothing reaches disk.

Tests stop naming a real companion: the addresses and pairing code are
stand-ins now, and the fakes model the class we construct rather than the
create_ble we no longer call.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5 committed 2026-08-24 01:32:42 -04:00
1 parent 602bceea3b
commit d3735c8f7b
6 files changed
+457 -100

No files matched your search

+10 -3
View File
@@ -594,14 +594,19 @@ async def _startup(ctx: AppContext) -> bool:
async def verify(device: DiscoveredDevice, pin: Optional[str] = None):
# ``pin`` is what the picker's PIN dialog collected on a retry; fall back to any
# ``--ble-pin`` supplied on the CLI for the first attempt.
result = await probe_device(
device, baudrate=baudrate, pin=pin if pin is not None else ctx.ble_pin
)
used = pin if pin is not None else ctx.ble_pin
result = await probe_device(device, baudrate=baudrate, pin=used)
if result is None:
return None
connection, info = result
probed["device_id"] = device.stable_id
probed["device"] = connection
# Remember the PIN that actually worked. The adopted connection carries its own
# copy, but a mid-session reconnect builds a *fresh* device from the context's
# resolved endpoint — which, without this, would go back unarmed and re-run the
# whole "needs a PIN" dance against a companion we already authenticated to.
# Session-lifetime only: nothing writes a pairing code to disk.
probed["pin"] = used
return info
chosen = await prompt_device(
@@ -624,6 +629,8 @@ async def _startup(ctx: AppContext) -> bool:
ctx.tcp_override = None
if probed.get("device_id") == chosen.stable_id:
ctx.adopt_device(probed["device"])
if probed.get("pin"):
ctx.ble_pin = probed["pin"]
# Between the device splash and the first menu paint, resuming background listening opens
# the radio — a slow, silent step that would otherwise leave the screen blank for a beat.
# Float the skeleton card across that gap on any real link (see _busy_over_link).