Fix background crash in Coach BGTask launch handler - #24
Merged
saksham2001 merged 2 commits intoJun 28, 2026
Merged
Conversation
BGTaskScheduler.register(using: nil) runs its launch handler on a private
background queue, not the main queue. The handler called MainActor.assumeIsolated,
which asserts the current executor is already the main actor and traps
(EXC_BREAKPOINT/SIGTRAP) when it isn't — crashing every time iOS fired the
coach refresh task in the background.
Hop to the main actor with Task { @mainactor in ... } instead of asserting.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
saksham2001
approved these changes
Jun 27, 2026
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
Fixes a periodic background crash (
EXC_BREAKPOINT/SIGTRAP) in the Coach daily check-in background task.CoachNotificationScheduler.register(...)registers the BGTask launch handler withMainActor.assumeIsolated { ... }, on the assumption thatBGTaskScheduler.register(forTaskWithIdentifier:using:)withqueue: nilruns the handler on the main queue.It does not —
nilmeans a private background queue.assumeIsolatedis a precondition assertion (not a thread hop): when the handler runs off the main actor, the assertion fails and Swift traps, crashing the app every time iOS opportunistically fires thecom.pulseloop.coach.refreshtask in the background.Fix
Hop to the main actor explicitly instead of asserting we're already on it:
handle(_:)and the@MainActor-isolated state it touches now genuinely run on the main actor. The async hop is well within the BGTask runtime budget;scheduleNext()/setTaskCompletedbehavior is unchanged.How it was found
Captured in a TestFlight crash report. The crashing thread was a libdispatch background worker:
The symptom matched exactly: crashes only in the background, never during active use.
Testing
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.pulseloop.coach.refresh"]🤖 Generated with Claude Code