Skip to content

Fix launch crash: start telemetry after Core Data initialization - #1519

Merged
t1dude merged 1 commit into
nightscout:devfrom
trioneer-dev:fix/telemetry-launch-order
Sep 15, 2026
Merged

t1dude merged 1 commit into
nightscout:devfrom
trioneer-dev:fix/telemetry-launch-order

Conversation

@trioneer-dev

@trioneer-dev trioneer-dev commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Problem

Debug/simulator launches of current dev crash within seconds:

NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.

Crashing queues are NSManagedObjectContext: storeGlucose and storeBatteryStatus.

Cause

Since #1367, TelemetryClient.shared resolves through the DI container with eager injectServices. AppDelegate.didFinishLaunchingWithOptions calls it at launch, which resolves APSManager + FetchGlucoseManager and thereby constructs BaseDeviceDataManager — the whole APS/device graph — before CoreDataStack.initializeStack() has finished loading the persistent stores.

The CGM and pump simulators deliver status/readings immediately on construction, so their first Core Data save races the store load and aborts the app. Real hardware usually wins the race (BLE reconnect latency), which is why reports cluster on simulator-configured dev setups; a Debug/TSAN build widens the window further. The launch-order log signature: a healthy launch logs Successfully loaded persistent store before PumpManagerState is persisted; a crashing one persists PumpManagerState immediately and never reaches the store-load line.

Fix

AppDelegate no longer resolves services at launch (only the Crashlytics gate remains, which touches plists only). Telemetry starts at the end of TrioApp.loadServices() — i.e. strictly after Core Data initialization — with the identical cadence sequence as before (install ID, cold-launch recording, SHA-change ping, recurring timer, overdue check). The foreground-transition hook receives its TelemetryClient reference from loadServices() and no-ops before that.

This deliberately does not reintroduce the old injectIfNeeded deferral; eager injection stays, only the call site moves behind the initialization barrier.

Notes

  • Cold-launch timestamps are now recorded ~1 s later (post store load) — immaterial to the 7-day sliding window.
  • A latent same-class race remains on the APNS path (TrioRemoteControl.shared in didReceiveRemoteNotification when a push cold-launches the app). It predates Refactor: Dependency Injection Hygiene #1367 and is left for a follow-up.

Since nightscout#1367, AppDelegate.didFinishLaunching resolved TelemetryClient
eagerly, constructing the APS/device graph before the persistent
stores were loaded. Pump and CGM simulators save immediately, so
launches crashed with "NSPersistentStoreCoordinator has no
persistent stores" (storeGlucose / storeBatteryStatus queues).

Telemetry now starts at the end of loadServices(), after the Core
Data stack is initialized; the foreground-transition hook receives
its reference from there and no-ops before that.

@jwoglom jwoglom 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.

Cherry-picked this onto latest dev and validated it doesn't crash on startup

@LiroyvH LiroyvH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can confirm this works in a build to iPhone 16 Pro and iPhone 12 Mini, both running iOS 27 stable and built from XCode 27. Appears to build as expected and the app now launches instead of crashing. This is not a full test of the dev branch latest state, just confirming that the app no longer fatally crashes when building this branch as compared to the current dev branch where it does fatally crash.

@bjornoleh bjornoleh 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.

Confirmed that the app on my phone no longer crashes when running this update. Did previously crash with current dev with sim pump/CGM.

Approving based on a quick, successful test.

@t1dude
t1dude merged commit 2865101 into nightscout:dev Sep 15, 2026
2 checks passed
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.

5 participants