fix: make auto-reconnect watcher state main-only to close a data race#38
Merged
Conversation
The watcher's state (reconnectWatchlist / reconnectInFlight / intentionalReleases / reconnectTimer) is documented main-only, but the peripheral handoff reaches `unregisterFromPC` on the outgoing-connection queue: `performHandoffToPeer` runs inside an `OutgoingConnection` completion, which fires on `com.magicswitch.outgoing`, not main. So `disarmReconnect` / `noteIntentionalRelease` mutated those Swift collections (and cancelled the main-queue timer) off-main, racing `reconnectTick` and `handlePeripheralDisconnected` — a data race on non-thread-safe collections that can lose the intentional-release flag (causing the watcher to fight a handoff) or corrupt/trap. Hop `armReconnect` / `disarmReconnect` / `noteIntentionalRelease` to main, matching the existing `setConnectionState` pattern; this also guarantees the flag is set before the disconnect handler reads it. Also fixed alongside it: - Clear the intentional-release flag when `closeConnection()` fails, so a release that never happened can't permanently suppress a later genuine drop's auto-reconnect. - Register a disconnect observer when a probe finds the device already reconnected on its own, so its next drop re-arms the watcher (the genuine drop had unregistered the old observer). - Skip the pre-sleep IOBluetooth scan when neither `releaseOnSleep` nor `autoReconnect` needs it, rather than block the held sleep transition to build a `connectedBeforeSleep` snapshot nothing will read.
|
🎉 This PR is included in version 2.11.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
What & why
The auto-reconnect watcher's state (
reconnectWatchlist,reconnectInFlight,intentionalReleases,reconnectTimer) is documented main-only, but the peripheral handoff reachesunregisterFromPCon the outgoing-connection queue:performHandoffToPeerruns inside anOutgoingConnectioncompletion, which fires oncom.magicswitch.outgoing, not main. SodisarmReconnect/noteIntentionalReleasemutated those non-thread-safe Swift collections (and cancelled the main-queue timer) off-main, racingreconnectTickandhandlePeripheralDisconnected.This is a data race on the primary switch path. Beyond the collection corruption/trap risk, it can drop the intentional-release flag — so the watcher fights a just-completed handoff and tries to reclaim a peripheral that was handed to the peer.
Changes
armReconnect/disarmReconnect/noteIntentionalReleasenow hop to main (mirrors the existingsetConnectionStatepattern). This also guarantees the intentional-release flag is set beforehandlePeripheralDisconnectedreads it.closeConnection()fails — no disconnect follows to consume it, so otherwise it would permanently suppress a later genuine drop's auto-reconnect.releaseOnSleepnorautoReconnectneeds it, instead of blocking the held sleep transition to build an unusedconnectedBeforeSleepsnapshot.Testing
xcodebuild -configuration Debug build CODE_SIGNING_ALLOWED=NO-> BUILD SUCCEEDEDswift formatcleanNot in scope (follow-ups)
Silent give-up notification after the 1h window; bounding
bluetoothQueue.syncagainst an in-flight pair at sleep; distinguishingOP_FAILEDfrom transport errors inHOLDS_ONE; multi-Mac (>2) reclaim usingnetworkDevices.first.