fix: don't capture SwiftData models across the notification Task (switch-device crash)#1923
Merged
Merged
Conversation
…extMessageAppPacket
Backtrace-confirmed fix for the "destroyed by ModelContext.reset" crash when
switching devices:
MyInfoEntity.channels.getter()
closure #11 in MeshPackets.textMessageAppPacket(...) MeshPackets.swift:1310
On an incoming text message, both notification branches deferred work to a
`Task { @mainactor in ... }` that captured context-bound model objects
(`newMessage`, and the fetched `MyInfoEntity`) and read them — `.channels`,
`.unreadMessages`, `newMessage.*` — when it ran. A device switch recreates/resets
the MeshPackets context between the capture and the Task, so those objects are
dead by the time the Task executes and any access traps.
Snapshot everything the Task needs before the hop instead:
- Channel branch: do the channel mute-check synchronously (context still alive),
pre-build the Notification, and capture only plain values. The badge recompute
(unreadMessages is @mainactor) re-fetches a fresh, live main-context entity
inside the Task rather than touching the captured one.
- DM branch: build the Notification (and CarPlay intent) before the Task and
capture only the resulting value.
No model object is captured across the Task in either branch now.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a backtrace-confirmed crash that can occur during device switching by preventing SwiftData model instances (bound to a soon-to-be-reset ModelContext) from being captured and later accessed inside a deferred Task { @MainActor in … } in MeshPackets.textMessageAppPacket.
Changes:
- Pre-build DM notifications synchronously while the
MeshPacketscontext is still valid, and pass only value snapshots into the deferred@MainActorTask. - For channel messages, snapshot mute/notification decisions synchronously, and re-fetch
MyInfoEntityfrom a fresh main context inside the@MainActorTask before recomputing the unread badge count. - Remove cross-Task access to
MyInfoEntity.channels,MyInfoEntity.unreadMessages(...), andnewMessage.*that could trap after aModelContext.reset.
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.
Summary
Backtrace-confirmed fix for the recurring crash when switching devices:
Root cause (from the backtrace)
On an incoming text message,
textMessageAppPacketdeferred its notification/badge work to aTask { @MainActor in … }that captured context-bound model objects — the fetchedMyInfoEntityandnewMessage— and read them (.channels,.unreadMessages,newMessage.*) when it ran. A device switch recreates/resets the MeshPacketsModelContextbetween the capture and the Task firing, so by the time the Task executes those objects belong to a torn-down context and any access traps.This is why it was intermittent (depends on a message in flight around the switch) and why the container/context fixes (#1918, #1922) didn't catch it — the bug is the cross-
Taskcapture, not the clear path.Fix
Snapshot everything the Task needs before the hop; capture no model objects:
Notification(and CarPlay intent) is pre-built; only plain values cross into the Task. The badge recompute (unreadMessagesis@MainActor) re-fetches a fresh, live main-contextMyInfoEntityinside the Task instead of touching the captured one.Notificationis built before the Task and only the resulting value is captured.Relationship to the other open PRs
Test plan (on-device — the race is timing-dependent)
ModelContext.resetcrash