Skip to content

Reconnect: never strand the state machine without a pending connect intent - #5

Open
loopkitdev wants to merge 2 commits into
mainfrom
reconnect-standing-connect-intent
Open

Reconnect: never strand the state machine without a pending connect intent#5
loopkitdev wants to merge 2 commits into
mainfrom
reconnect-standing-connect-intent

Conversation

@loopkitdev

Copy link
Copy Markdown
Contributor

Summary

Two recent multi-hour CGM outages traced to the reconnect state machine stranding itself "disconnected with nothing pending" — no monitor, no in-flight attempt, and no CoreBluetooth connect intent to wake the app when the sensor returns — recovering only on a manual foreground. Both are races around the peripheral's transient .connecting/.disconnecting states.

Fixes

1. scheduleReconnect — only requestConnect from .disconnected (was: anything except .connected/.connecting).

Issuing central.connect while a cancelConnection is still in flight (e.g. the close-on-CCCD-failure path drops the link and immediately reschedules) races the pending teardown. iOS leaves the peripheral in a phantom .connecting state that never fires didConnect, and that phantom state then suppresses every legitimate re-arm. Wait for the didDisconnect; its scheduleReconnect re-arms cleanly from .disconnected.
Field log 2026-07-25 15:53 — ~17-hour outage.

2. Failed-attempt continuation — don't cancel a .connecting peripheral (was: cancel on .connected or .connecting).

.connecting is normally the intent the preceding didDisconnect just re-armed. Cancelling a .connecting peripheral fires no terminal callback, so it silently drops that intent and strands us. Now it only drops a fully .connected stale link (which does fire didDisconnect), leaves .connecting/.disconnecting alone (a CB callback is guaranteed to come and route back to scheduleReconnect), and re-arms only from .disconnected.
Field log 2026-07-26 16:40 — the didDisconnect re-armed the connect (requesting connect (peripheral state=0)), then this continuation cancelled it 1ms later; ~6-min outage.

3. Sensor monitor bails if superseded during the CCCD refresh.

refreshPostAuthNotifications isn't cancellation-aware, so a monitor stopped mid-refresh runs to completion and then executes its close path — cancelConnection on session.peripheral (same identifier as a newer attempt's peripheral) plus firing the disconnect handler — derailing the live reconnect. A Task.isCancelled check after the refresh returns before touching the link.
Field log 2026-07-25 15:53 — a refresh started at :37, monitor stopped at :47, refresh failed at :53 and dropped the fresh attempt mid-connect.

Testing

Builds clean (LoopWorkspace scheme, device); installed and field-testing. The invariant to watch on recovery: a requesting connect … (peripheral state=0) after didDisconnect, with no cancelConnection immediately following it.

ps2 added 2 commits July 26, 2026 12:44
…races

Two ways the reconnect state machine could strand itself "disconnected with
nothing pending" (no monitor, no in-flight attempt, no CB connect intent to wake
the app when the sensor returns), causing outages until a manual foreground:

1. scheduleReconnect re-armed the connect intent whenever the peripheral wasn't
   .connected/.connecting -- which included .disconnecting. Issuing
   central.connect while a cancelConnection is still in flight (e.g. the
   close-on-CCCD-failure path) leaves the peripheral in a phantom .connecting
   state that never fires didConnect and suppresses every later re-arm. Only
   request connect from .disconnected; wait for the didDisconnect otherwise.

2. After a failed attempt, the continuation cancelled the peripheral whenever it
   was .connected OR .connecting. But .connecting is normally the intent the
   preceding didDisconnect just re-armed, and cancelling a .connecting peripheral
   fires no terminal callback, silently dropping it. Only drop a fully .connected
   stale link (which does fire didDisconnect); leave .connecting/.disconnecting
   alone (a CB callback is coming) and re-arm from .disconnected.

Field logs: 2026-07-25 15:53 (phantom .connecting, ~17-hour outage) and
2026-07-26 16:40 (cancelled the re-armed intent, ~6-min outage).
refreshPostAuthNotifications isn't cancellation-aware, so a monitor stopped
mid-refresh runs to completion and then executes its close path --
cancelConnection on session.peripheral (same identifier as a newer attempt's
peripheral) plus firing the disconnect handler -- derailing the live reconnect.
Check Task.isCancelled after the refresh and return before touching the link.

Field log 2026-07-25 15:53: a refresh started at :37, the monitor was stopped at
:47, the refresh failed at :53 and dropped the fresh attempt mid-connect.
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.

2 participants