Defer forgetting sensor on suspected session end to avoid long reconnection outages - #62
Open
cheets wants to merge 1 commit into
Open
Defer forgetting sensor on suspected session end to avoid long reconnection outages#62cheets wants to merge 1 commit into
cheets wants to merge 1 commit into
Conversation
A remote disconnect before authentication completes is treated as a session end, immediately forgetting the sensor and scanning from scratch. The same disconnect signature occurs on transient BLE handshake failures (auth notification timeouts, encryption failures), where forgetting the tracked peripheral downgrades reconnection from an OS-level pending connect to a throttled background scan, causing 10-40 minute glucose outages. Field logs showed 118 suspected session ends in 7 days of which 1 was a real session end. Keep tracking the sensor on a suspected session end and defer the scan-for-new-sensor by a 15 minute wall-clock grace period, cancelled when any glucose or backfill message arrives. A real session end still switches sensors: the stopped sensor stays silent, the grace period expires, and the new sensor is discovered during its warmup. Immediate switch on sensorFailed/sessionEnded algorithm states is unchanged. Also add DI seams (central manager factory, injectable bluetooth manager, internal manager init) so G7CGMManager is unit-testable, plus a shared scheme with a test action.
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.
Problem
When a G7 disconnects before authentication completes,
G7SensorreportssuspectedEndOfSession=trueandG7CGMManagerimmediately forgets the sensor (sensorID = nil, peripheral forgotten) and starts scanning for a new one.The same disconnect signature is produced by ordinary transient BLE handshake failures — auth notification enable timeouts,
unknownCharacteristicduring incomplete service discovery,CBErrorencryption failures. In a week of device logs I collected, 118 suspected session ends fired, of which exactly 1 was a real session end. Each false positive forgets the tracked peripheral, which downgrades reconnection from an OS-level pending connect (instant wake when the sensor next transmits) to a throttled background scan plus full re-discovery. Result: recurring 10–40 minute glucose outages ("Searching for sensor"), during which Loop cannot dose. Worst observed: 45 minutes.Change
On a suspected session end, keep tracking the current sensor and defer
scanForNewSensor()by a 15-minute wall-clock grace period:sensorFailed/sessionEndedalgorithm states is unchanged — a sensor that announces its own session end still triggers an instant scan.The grace period uses a wall-clock deadline so device sleep cannot postpone detection of a genuinely ended session.
Also adds unit tests for the new behavior, with small DI seams to make
G7CGMManagertestable (central-manager factory to avoid the state-restoration exception in test bundles, injectableG7BluetoothManager, internal manager init), and a shared scheme with a test action.Field results
I have been running this for a month on my own Loop with no issues. From exported device logs:
A real sensor swap during the trial was handled by the unchanged
sessionEndedmessage path: old sensor forgotten immediately, new sensor discovered 3 minutes after activation, during warmup.Notes
This overlaps in intent with the
scanning-fixbranch ("Continue with sensor even after auth without control msg", "Use remote disconnect without auth/data as end-of-session detection again") — same underlying observation that auth-less remote disconnects are unreliable as a session-end signal. This change keeps the existing detection but makes acting on it tolerant to transient failures. Happy to adapt if maintainers prefer thescanning-fixdirection.