SwiftData Crash: Incorrect actor executor assumption.

I have this actor

actor ConcurrentDatabase: ModelActor {
    nonisolated let modelExecutor: any ModelExecutor
    nonisolated let modelContainer: ModelContainer

    init(modelContainer: ModelContainer) {
        self.modelExecutor = DefaultSerialModelExecutor(modelContext: ModelContext(modelContainer))
        self.modelContainer = modelContainer
    }

/// Save pending changes in the model context.
    private func save() {
        if self.modelContext.hasChanges {
            do {
                try self.modelContext.save()
            } catch {
                ...
            }
        }
    }
}

I am getting a runtime crash on:

try self.modelContext.save()

when trying to insert something into the database and save

Thread 1: Fatal error: Incorrect actor executor assumption; Expected same executor as MainActor.

Can anyone explain why this is happening?

Why don't you use the @ModelActor macro instead?

@ModelActor actor ConcurrentDatabase {
    private func save() {
        if self.modelContext.hasChanges {
            do {
                try self.modelContext.save()
            } catch {
                ...
            }
        }
    }
}

It seems to be a manifestation of the bug outlined at https://forums.developer.apple.com/forums/thread/736226. In my tests, I use an implementation like yours, except I omit the ModelActor protocol conformance, and it seems to work fine.

SwiftData Crash: Incorrect actor executor assumption.
 
 
Q