Skip to content

fix(ble): keep macOS App Nap from stalling a sync while the strap is connected - #2919

Open
matthiasthomas wants to merge 1 commit into
ryanbr:mainfrom
matthiasthomas:fix/macos-app-nap
Open

matthiasthomas wants to merge 1 commit into
ryanbr:mainfrom
matthiasthomas:fix/macos-app-nap

Conversation

@matthiasthomas

@matthiasthomas matthiasthomas commented Nov 17, 2026 •

Copy link
Copy Markdown

What this PR does

On macOS, a history sync stalls whenever NOOP's window is hidden or covered, even with the app open all day. The Devices card then sits on "Syncing…" with the frontier frozen until NOOP is brought back to the front.

Cause: App Nap. For an app with no visible window, macOS coalesces its timers and clamps its work to background priority. The drain depends on both:

  • Decoding: each history chunk is decoded on a .utility task before its ack is sent.
  • Timers: the keep-alive, the backfill watchdog and the periodic-sync timer all run on the main queue.

NOOP never opted out, so a napped app drained slowly or not at all.

Fix: BLEManager holds a ProcessInfo activity for as long as a strap link is up, using .userInitiatedAllowingIdleSystemSleep (BLEManager.appNapExemptionOptions).

  • Coverage: it's driven by LiveState.connected through Combine, so every connect and teardown path begins or ends it, including the radio-off teardown in fix(ble): end the link when the Mac's Bluetooth powers off or resets #2787, with no per-call-site bookkeeping.
  • Scope: it only exempts the app from App Nap. The Mac can still idle-sleep, and the display can still sleep.
  • Platform: macOS only (#if os(macOS)); App Nap does not exist on iOS.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Documentation
  • CI / tooling

How it was tested

  • Hardware: WHOOP 4.0, firmware 41.17.6.0, macOS 27. The strap had a large backlog (Strap backlog pages behind: 9647). Before this change, the drain stopped whenever NOOP was not the front window. With this change, I hid NOOP (⌘H) and counted Historical Data Result acks per minute from the strap log:

    19:16 27 · 19:17 34 · 19:18 32 · 19:19 34 · 19:20 32 · 19:21 31 · 19:22 33 · 19:23 32 · 19:24 33 · 19:25 31 · 19:26 32 · 19:27 33 · 19:28 29
    

    The drain held a steady ~32 chunks a minute for 12 minutes hidden, with no disconnects and no session restarts.

  • Unit tests: AppNapExemptionTests pins the options: user-initiated, and never .idleSystemSleepDisabled or .idleDisplaySleepDisabled, so syncing can never keep the Mac awake.

  • Full StrandTests (macOS): 2,304 tests ran. Two failures are timezone-dependent and unrelated to this change: SkinTempAbsoluteDisplayTests and SleepCarriedStampTests fail in America/New_York (fixed by fix(dates): show day-keyed dates on their own day in every time zone #2723). After marking appNapExemptionOptions nonisolated to clear a Swift 6 isolation warning in the test, I re-ran AppNapExemptionTests: pass, with no warnings.

  • Build and checks: xcodegen generate plus xcodebuild (macOS Strand scheme) builds. Tools/doc_comment_lint.py and Tools/i18n_audit.py --ci upstream/main pass.

Checklist

  • Swift package tests pass for any package I touched (swift test in Packages/<name>): n/a, no package touched
  • Android unit tests pass if I touched android/: n/a. App Nap is macOS-only, so there is no Android equivalent
  • No new build warnings introduced
  • UI changes use only StrandDesign tokens: n/a, no UI change
  • No hardcoded hex frame bytes; protocol facts live in the schema / decoders
  • Follows the conventions in docs/CONTRIBUTING.md
  • I did not commit generated output (Strand.xcodeproj/) or any secrets/keystores

Related issues

Refs #2787

…connected

When NOOP's window is hidden or covered, macOS App Nap coalesces the app's
timers and clamps its work to background priority. A history drain decodes
each chunk on a utility task before acking it, and the keep-alive, backfill
watchdog and periodic-sync timers all run on the main queue, so a drain
slowed to a stop whenever NOOP was not the front window, even with the app
open all day.

BLEManager now holds a ProcessInfo activity
(.userInitiatedAllowingIdleSystemSleep) for as long as a strap link is up,
driven by LiveState.connected so every connect and teardown path is covered.
It only exempts the app from App Nap; the Mac can still idle-sleep. macOS
only (#if os(macOS)); App Nap does not exist on iOS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jvascopf added a commit to jvascopf/noop that referenced this pull request Nov 21, 2026
…p from stalling a sync while the strap is connected

ryanbr#2919

Conflict: both sides were additive; kept ryanbr#2912's battery rated-life init call and
ryanbr#2787's LinkIntent table alongside this PR's App Nap exemption.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant