SwiftData crashes after updating to MacOS 15.2

After updating to 15.2 I am seeing frequent crashes in my in-development app related to SwiftData.

For instance, I have a 100% reproducible crash when I make the app lose and regain focus.

There is also a crash that seem to be triggered by a modelContext.save() call in one of my ModelActors.

With both of these crashes, the issue seems to be around keeping SwiftData models up to date. The first item in the stacktrace that is not machinecode is always some getter on a SwiftData collection or object.

In the console, these crashes are accompanied by output along the lines of:

=== AttributeGraph: cycle detected through attribute 820680 ===
precondition failure: setting value during update: 930592
error: the replacement path doesn't exist: "/var/folders/b7/0dw7ztp13fgfxlj19by851tw0000gn/T/swift-generated-sources/@__swiftmacro_10SpaceDebug8TodoListV5todos33_5575DE008494C519BB9FA49C405133E1LL5QueryfMa_.swift"
error: the replacement path doesn't exist: "/var/folders/b7/0dw7ztp13fgfxlj19by851tw0000gn/T/swift-generated-sources/@__swiftmacro_10SpaceDebug8TodoListV5todos33_5575DE008494C519BB9FA49C405133E1LL5QueryfMa_.swift"
Can't show file for stack frame : <DBGLLDBStackFrame: 0x35a57c4e0> - stackNumber:27 - name:TodoList.todos.getter. The file path does not exist on the file system: /var/folders/b7/0dw7ztp13fgfxlj19by851tw0000gn/T/swift-generated-sources/@__swiftmacro_10SpaceDebug8TodoListV5todos33_5575DE008494C519BB9FA49C405133E1LL5QueryfMa_.swiftCan't show file for stack frame : <DBGLLDBStackFrame: 0x35a57c4e0> - stackNumber:27 - name:TodoList.todos.getter. The file path does not exist on the file system: /var/folders/b7/0dw7ztp13fgfxlj19by851tw0000gn/T/swift-generated-sources/@__swiftmacro_10SpaceDebug8TodoListV5todos33_5575DE008494C519BB9FA49C405133E1LL5QueryfMa_.swiftCan't show file for stack frame : <DBGLLDBStackFrame: 0x35a5a82f0> - stackNumber:62 - name:TodoList.todos.getter. The file path does not exist on the file system: /var/folders/b7/0dw7ztp13fgfxlj19by851tw0000gn/T/swift-generated-sources/@__swiftmacro_10SpaceDebug8TodoListV5todos33_5575DE008494C519BB9FA49C405133E1LL5QueryfMa_.swift

Has anyone run into something similar? I'm looking for suggestions on how to debug this.

Cheers, Bastiaan

Answered by BTBTBT in 822182022

Apologies for the delay. It took me some digging, but I have resolved the issue. In the end it had nothing to do with SwiftData, but rather with Actors and illegally crossing actor boundaries.

I have a singleton that holds a reference to my main SwiftData Container, that I can access using SwiftDataCoordinator.shared.container This shared instance lives on the MainActor.

My issue arose because I was instantiating my ModelActors like this:

Task.detached {
                        let modelActor = SortableDataService(modelContainer: SwiftDataCoordinator.shared.container)
etc...

The fix was to do this:

let container = SwiftDataCoordinator.shared.container
Task.detached {
                        let modelActor = SortableDataService(modelContainer: container)
etc...

In the end quite obvious, but it took me a while to see it. Leaving this here in case others run into this.

For instance, I have a 100% reproducible crash when I make the app lose and regain focus.

Would you mind to provide a minimal project that contains only the code relevant to the issue, with detailed steps to reproduce the issue? I may be able to take a look and hopefully figure out why.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Apologies for the delay. It took me some digging, but I have resolved the issue. In the end it had nothing to do with SwiftData, but rather with Actors and illegally crossing actor boundaries.

I have a singleton that holds a reference to my main SwiftData Container, that I can access using SwiftDataCoordinator.shared.container This shared instance lives on the MainActor.

My issue arose because I was instantiating my ModelActors like this:

Task.detached {
                        let modelActor = SortableDataService(modelContainer: SwiftDataCoordinator.shared.container)
etc...

The fix was to do this:

let container = SwiftDataCoordinator.shared.container
Task.detached {
                        let modelActor = SortableDataService(modelContainer: container)
etc...

In the end quite obvious, but it took me a while to see it. Leaving this here in case others run into this.

SwiftData crashes after updating to MacOS 15.2
 
 
Q