Skip to content

Unhook a replaced or forgotten PeripheralManager from BlePodComms - #214

Merged
ps2 merged 3 commits into
devfrom
fix/session-on-configured-manager
Nov 10, 2026
Merged

ps2 merged 3 commits into
devfrom
fix/session-on-configured-manager

Conversation

@ps2

@ps2 ps2 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #132.

Cause

completeConfiguration(for:) runs on the queue of the PeripheralManager whose connect just finished, but it built the login session on self.manager. When those differ, sendMessagePacket's dispatchPrecondition traps, which matches both crash logs in the issue. The difference can arise because BlePodComms.manager's didSet made 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 call completeConfiguration. It can also arise during the callback itself: the O5 write-size poll (up to 2 s) and podStateLock both wait before login, and self.manager can change meanwhile. This is the likely cause; the crash hasn't been reproduced.

Fix

  • Login runs on the manager passed in. establishNewSession(on:) and establishSession(on:…) take the manager, from completeConfiguration, the pairing session and pairPod. The session always runs on the queue that's executing, so the precondition can't trap however the mismatch arises.
  • Only for the current pod. After taking podStateLock, completeConfiguration skips login when the manager is no longer self.manager. It returns rather than throws, so the connect isn't logged as a failed login.
  • Unhook replaced managers. manager.didSet clears the old manager's delegate, and forgetPod sets manager = nil. A connect for the forgotten pod that's already queued can still re-adopt its manager until prepForNewPod clears the pod state; the identity guard covers that.
  • Visible in the device log. A connect that skipped login now shows no delegate, login skipped or not the pod's current manager, login skipped in its [connect] configured … line, instead of setup ok with 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.

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
@marionbarker

marionbarker commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude review

PR #214 review (concise)

PR: loopandlearn/OmnipodKit#214 — "Unhook a replaced or forgotten PeripheralManager from BlePodComms" (head f2e3aa6)
Fixes: #132 — dispatchPrecondition crash in sendMessagePacket, reached from completeConfiguration → establishNewSession → establishSession
Reviewed against: OmnipodKit dev @ adf8773, which includes merged #211, together with open PR #209 (c48aa64)

Verdict

Right direction, but incomplete. Request a small addition before merging.

The PR identifies the cause correctly. completeConfiguration(for: A) runs on A's queue, but establishSession builds the session on self.manager. When self.manager is some other manager, the precondition traps. Clearing the old manager's delegate makes that state much less likely, but it doesn't make it impossible.

The change (4 lines, BlePodComms.swift)

  • manager.didSet sets oldValue?.delegate = nil when the manager changes.
  • forgetPod() sets manager = nil.

Findings

# 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

  1. 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.
  2. Identity guard in completeConfiguration, after taking podStateLock: 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)

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 old didSet, so the fix needs porting there too.

Not done

ps2 added 2 commits November 10, 2026 10:29
…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.
@ps2

ps2 commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

@marionbarker, thanks. a3a51ac takes the two requested changes, on top of a merge of dev with #209:

  • Manager passed through. establishNewSession(on:) and establishSession(on:…). The callers are completeConfiguration, pairPod, and the pair-and-setup session's re-login, which also used self.manager.
  • Identity guard after podStateLock. It returns, with noteSetupStep("not the pod's current manager, login skipped").
  • noteSetupStep("no delegate, login skipped") in configureAndRun.

On the other findings:

  • 2 (unsynchronized manager writes): left as is. The race predates this PR, and with the manager passed in the worst case is a skipped login, which the log line now shows rather than a crash. Locking manager properly is a bigger change than this fix needs.
  • 3 (forgetPod): you're right, a queued didConnect can re-adopt the old manager until prepForNewPod. The description now says so, and the guard makes it harmless.
  • 4 (cause not proven): agreed. The description now calls it the likely cause, and the pass-through makes the trap impossible whichever way the mismatch arises.

OmniTests: 212 run, 0 failures. Not reproduced on a device or the simulator.

@marionbarker marionbarker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude says:
Approve, OK to merge. Every requested change is in, and nothing blocks. The branch already contains current dev, so it merges cleanly.

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.

dispatchPrecondition failure in OmnipodKit

2 participants