Repository navigation
Unhook a replaced or forgotten PeripheralManager from BlePodComms - #214
Conversation
BlePodComms set itself as the delegate of each PeripheralManager it adopted but never cleared it, so a pod's old manager (a pairing candidate that lost out, or a pod that was forgotten) could still finish a queued connect and call completeConfiguration(for:) on its own queue while self.manager pointed at another manager. Session setup then ran against self.manager and tripped its dispatchPrecondition. Fixes #132
Claude reviewPR #214 review (concise)PR: loopandlearn/OmnipodKit#214 — "Unhook a replaced or forgotten PeripheralManager from BlePodComms" (head VerdictRight direction, but incomplete. Request a small addition before merging. The PR identifies the cause correctly. The change (4 lines,
|
| # | Severity | Finding |
|---|---|---|
| 1 | Medium | The crash is still possible. The delegate is read in configureAndRun before two waits: the O5 write-size poll (up to 2 s) and podStateLock, which is held for whole pod sessions, including pairing. If self.manager changes during either wait, establishSession still traps. |
| 2 | Low | manager is written from several threads with no synchronization (main, managerQueue, the caller's thread). Interleaved writes could leave the current manager with no delegate. Login would then be silently skipped on reconnect, and commands would fail until the next connect. |
| 3 | Low | forgetPod doesn't fully unhook. podState stays set until prepForNewPod, and the device-list prune runs asynchronously. A didConnect that is already queued can hook the old manager back up. It's harmless for the crash, but contrary to the description. |
| 4 | Info | The cause isn't proven. Within one BluetoothManager there is one manager per pod. A mismatch therefore needs an earlier pod change or pairing retry. The second crash (background, 35 min after launch) looks more like a reconnect. The PR adds no test, and the crash wasn't reproduced. |
Requested addition
- Pass the manager through:
establishNewSession(on:)/establishSession(on:…). That's 3 call sites. With it, the session always uses the manager whose queue is running, so this crash can't happen. - Identity guard in
completeConfiguration, after takingpodStateLock:guard manager === self.manager else { note; return }. This avoids running login against a pod that isn't the current one.
Interaction with #209 (still open)
- Both PRs merge cleanly onto current
dev, together and in either order. - Log each connect's setup and every failed exchange and disconnect; don't mark unsent commands unacknowledged #209 rewrites the same login line (
setupStep("login") { try establishNewSession() }). Merge Log each connect's setup and every failed exchange and disconnect; don't mark unsent commands unacknowledged #209 first, then add the requested change on top. - Log each connect's setup and every failed exchange and disconnect; don't mark unsent commands unacknowledged #209 now rethrows from
completeConfiguration. The identity guard should return, not throw; otherwise it would be logged as a failed login. - With Log each connect's setup and every failed exchange and disconnect; don't mark unsent commands unacknowledged #209, a skipped
completeConfiguration(no delegate) is logged asconfigured … setup okwithout a login step. AddnoteSetupStep("no delegate — login skipped")inconfigureAndRun'sif let delegatebranch. This makes Unhook a replaced or forgotten PeripheralManager from BlePodComms #214's effect and Finding 2 visible in device logs.
Shipping notes
- The crashes came from Trio builds that ship OmnipodKit
792dc19, so the fix must be cherry-picked to the branch Trio pins. - The LoopKit/OmnipodKit fork (
next-dev) still has the olddidSet, so the fix needs porting there too.
Not done
- No build or test run of the combined code; only static review and
git merge-treechecks. - The raw crash-log attachments on dispatchPrecondition failure in OmnipodKit #132 were not read; only the stacks quoted in the issue.
…t pod Clearing a replaced manager's delegate stops new callbacks, but one that has already started can still wait out the O5 write-size poll or podStateLock while self.manager changes, and establishSession then built the session on self.manager and trapped in sendMessagePacket. The session is now established on the manager passed in, so it always runs on that manager's queue, and completeConfiguration skips login when that manager is no longer the pod's. Both skips are noted in the connect's setup log, so a connect without a login no longer reads as setup ok with no login step.
|
@marionbarker, thanks. a3a51ac takes the two requested changes, on top of a merge of
On the other findings:
OmniTests: 212 run, 0 failures. Not reproduced on a device or the simulator. |
marionbarker
left a comment
There was a problem hiding this comment.
Claude says:
Approve, OK to merge. Every requested change is in, and nothing blocks. The branch already contains current dev, so it merges cleanly.
Fixes #132.
Cause
completeConfiguration(for:)runs on the queue of the PeripheralManager whose connect just finished, but it built the login session onself.manager. When those differ,sendMessagePacket'sdispatchPreconditiontraps, which matches both crash logs in the issue. The difference can arise becauseBlePodComms.manager'sdidSetmade BlePodComms the delegate of each manager it adopted and never cleared the previous one. A pod's old manager, such as a pairing candidate that lost out or a pod that was forgotten, could still finish a queued connect and callcompleteConfiguration. It can also arise during the callback itself: the O5 write-size poll (up to 2 s) andpodStateLockboth wait before login, andself.managercan change meanwhile. This is the likely cause; the crash hasn't been reproduced.Fix
establishNewSession(on:)andestablishSession(on:…)take the manager, fromcompleteConfiguration, the pairing session andpairPod. The session always runs on the queue that's executing, so the precondition can't trap however the mismatch arises.podStateLock,completeConfigurationskips login when the manager is no longerself.manager. It returns rather than throws, so the connect isn't logged as a failed login.manager.didSetclears the old manager's delegate, andforgetPodsetsmanager = nil. A connect for the forgotten pod that's already queued can still re-adopt its manager untilprepForNewPodclears the pod state; the identity guard covers that.no delegate, login skippedornot the pod's current manager, login skippedin its[connect] configured …line, instead ofsetup okwith no login step.Testing
OmniTests: 212 run, 3 skipped, 0 failures. There's no unit test for the mismatch; it needs a second manager's connect to land at the wrong moment, and it wasn't reproduced on a device or the simulator.