ModelContext autosaveEnabled leading to crashes when SwiftUI App moves to background?

Hi! I'm investigating some crashes that seem to be related to ModelContext.autosaveEnabled^1. I don't have a very clean repro test case at this time… but I seem to be seeing crashes when a SwiftUI app moves to the background. I am testing an app built for macOS 14.6.1. I am building from Xcode_16_beta_5.

My app is not using a mainContext. My app is building a ModelActor that is running on a background thread (off main). The modelContext inside my ModelActor is set with autosaveEnabled equal to true. The mutations on my state are not being explicitly saved (I am waiting for the system to save automatically).

My guess (so far) is that transitioning into the background from SwiftUI is kicking off some kind of logic that is specifically being tied to the main thread… but this is causing problems when my modelContext is created from a background thread. My understanding was that ModelActor could help to defend against threading problems… but this might be a different problem that I did not expect.

I am unblocked for now by turning off autosaveEnabled (and manually saving from my ModelActor). That fixes the crashes. Any more thoughts or insight about what could be causing these crashes when my app transitions into the background? Thanks!

Thread 1 Queue : com.apple.main-thread (serial)
#0	0x000000023108beb8 in ___lldb_unnamed_symbol2827 ()
#1	0x000000023108ef30 in ___lldb_unnamed_symbol2847 ()
#2	0x000000023108eca8 in ___lldb_unnamed_symbol2845 ()
#3	0x000000019bac6144 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#4	0x000000019bb5a3d8 in ___CFXRegistrationPost_block_invoke ()
#5	0x000000019bb5a320 in _CFXRegistrationPost ()
#6	0x000000019ba94678 in _CFXNotificationPost ()
#7	0x000000019cbb12c4 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#8	0x000000019f489408 in -[NSApplication _handleDeactivateEvent:] ()
#9	0x000000019fb24380 in -[NSApplication(NSEventRouting) sendEvent:] ()
#10	0x000000019f771d9c in -[NSApplication _handleEvent:] ()
#11	0x000000019f322020 in -[NSApplication run] ()
#12	0x000000019f2f9240 in NSApplicationMain ()
#13	0x00000001c74c73b8 in ___lldb_unnamed_symbol83060 ()
#14	0x00000001c7c30ddc in ___lldb_unnamed_symbol132917 ()
#15	0x00000001c802be0c in static SwiftUI.App.main() -> () ()

Thread 5 Queue : NSManagedObjectContext 0x6000009c38e0 (serial)
#0	0x000000019ba66f94 in constructBuffers ()
#1	0x000000019ba65e30 in _CFURLCreateWithURLString ()
#2	0x000000019bad6e7c in _CFURLComponentsCopyURLRelativeToURL ()
#3	0x000000019cbde864 in -[__NSConcreteURLComponents URL] ()
#4	0x000000019d3782f8 in -[NSURL(NSURL) initWithString:relativeToURL:encodingInvalidCharacters:] ()
#5	0x000000019cbdd4d4 in +[NSURL(NSURL) URLWithString:relativeToURL:] ()
#6	0x00000001a23feef0 in -[NSTemporaryObjectID URIRepresentation] ()
#7	0x00000002310e0878 in ___lldb_unnamed_symbol4176 ()
#8	0x00000002310ef480 in ___lldb_unnamed_symbol4401 ()
#9	0x00000002310eb6e0 in ___lldb_unnamed_symbol4385 ()
#10	0x00000002310a22b4 in ___lldb_unnamed_symbol3130 ()
#11	0x00000002310ed4e8 in ___lldb_unnamed_symbol4390 ()
#12	0x00000002310883dc in ___lldb_unnamed_symbol2799 ()
#13	0x0000000231087edc in ___lldb_unnamed_symbol2798 ()
#14	0x000000023109fd24 in ___lldb_unnamed_symbol3021 ()
#15	0x0000000231086acc in ___lldb_unnamed_symbol2784 ()
#16	0x00000001a2392144 in developerSubmittedBlockToNSManagedObjectContextPerform ()
#17	0x00000001a2392004 in -[NSManagedObjectContext performBlockAndWait:] ()
#18	0x00000002310879ac in ___lldb_unnamed_symbol2797 ()

This seems a lot like a bug that was introduced to SwiftData around the 17.4 timeframe. The modelContext environment variable becomes corrupted as the app becomes de-active, then when in the background some SwiftData/SwiftUI activity, relying on that variable, crashes.

With autosave turned off, that background activity doesn't happen, but you even when that's turned off you'll probably be bitten eventually.

The solution is to avoid relying on the modelContext environment variable, instead setting the modelContainer manually on a root view.

See these threads for a deeper discussion :

https://developer.apple.com/forums/thread/750084?answerId=52256025#52256025

https://developer.apple.com/forums/thread/744194?answerId=788019022#788019022

ModelContext autosaveEnabled leading to crashes when SwiftUI App moves to background?
 
 
Q