realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.19k stars 2.14k forks source link

App Crashes with 0xdead10cc on Vision Pro When Using @ObservedResults under App Group #8530

Open gongzhang opened 3 months ago

gongzhang commented 3 months ago

How frequently does the bug occur?

Always

Description

Hello everyone!

Although I'm aware that the 0xdead10cc issue has been extensively discussed (e.g., #8017, #7863, #7053, #6861, etc.), I believe the problem I'm encountering is slightly different and can be reproduced 100% of the time, which is why I've decided to bring it up.

I've noticed that on the Vision Pro, if my Realm database is stored within an App Group, then holding any observation to Realm in the app will lead to a 100% crash when entering the background. This means that if your database is within an App Group, even the most basic SwiftUI examples cannot function properly on visionOS:

// official SwiftUI example
struct ContactsView: View {
    @ObservedResults(Person.self) var persons // ←💥 causes a 0xdead10cc crash on visionOS in App Group

    var body: some View {
        List {
            ForEach(persons) { person in
                Text(person.name)
            }
            .onMove(perform: $persons.move)
            .onDelete(perform: $persons.remove)
        }.navigationBarItems(trailing:
            Button("Add") {
                $persons.append(Person())
            }
        )
    }
}

More specifically, the app will 100% crash with either a 0xdead10cc error or a RUNNINGBOARD 3735883980 error upon entering the background, provided the following conditions are met:

  1. The app utilizes @ObservedResults or any other Realm observation object.
  2. The realm file is located in an App Group container.
  3. The app is running on a physical Vision Pro device, not on a simulator.
  4. The app is a native visionOS app, rather than an compatible iOS app.
  5. The Xcode debugger is not attached to the app.

Stacktrace & log output

Incident Identifier: A1FA1A77-0CC6-409B-B555-FFDDD2F26544
CrashReporter Key:   1b2668797ebb0d3ce434ac760fbcd8168635b1bb
Hardware Model:      RealityDevice14,1
Process:             RealmFileLockTest [756]
Path:                /private/var/containers/Bundle/Application/FA8A9582-4DAA-4600-BEB4-D24CC7740D31/RealmFileLockTest.app/RealmFileLockTest
Identifier:          io.gongzhang.RealmFileLockTest
Version:             1.0 (1)
Code Type:           ARM-64 (Native)
Role:                unknown
Parent Process:      launchd [1]
Coalition:           io.gongzhang.RealmFileLockTest [784]

Date/Time:           2024-03-27 15:37:11.8581 +0800
Launch Time:         2024-03-27 15:37:03.0253 +0800
OS Version:          xrOS 1.1.1 (21O224)
Release Type:        Beta
Report Version:      104

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: RUNNINGBOARD 3735883980 

Triggered by Thread:  0

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib                 0x24457a5c8 mach_msg2_trap + 8
1   libsystem_kernel.dylib                 0x2445826b4 mach_msg2_internal + 80
2   libsystem_kernel.dylib                 0x244582948 mach_msg_overwrite + 436
3   libsystem_kernel.dylib                 0x244582788 mach_msg + 24
4   CoreFoundation                         0x1a10e09b8 __CFRunLoopServiceMachPort + 160
5   CoreFoundation                         0x1a10dac70 __CFRunLoopRun + 1208
6   CoreFoundation                         0x1a10da394 CFRunLoopRunSpecific + 608
7   GraphicsServices                       0x1dc9da4f8 GSEventRunModal + 164
8   UIKitCore                              0x22c67c720 -[UIApplication _run] + 888
9   UIKitCore                              0x22c680f80 UIApplicationMain + 340
10  SwiftUI                                0x1b1c57ca8 0x1b0b28000 + 18021544
11  SwiftUI                                0x1b1c57b04 0x1b0b28000 + 18021124
12  SwiftUI                                0x1b18084f4 0x1b0b28000 + 13501684
13  RealmFileLockTest                      0x1022bf060 static RealmFileLockTestApp.$main() + 40
14  RealmFileLockTest                      0x1022bf10c main + 12
15  dyld                                   0x23da918b8 start + 2204

Can you reproduce the bug?

Always

Reproduction Steps

I have provided a sample project along with the steps to reproduce the issue. The sample project is quite straightforward—it uses @ObservedResults and stores the database in an App Group.

Steps to reproduce can be found here: https://github.com/gongzhang/RealmFileLockTest/tree/main?tab=readme-ov-file#steps-to-reproduce

Version

v10.49.1

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

visionOS 1.0-1.1.1 are confirmed

Build environment

Xcode version: 15.3 Dependency manager and version: XCFramework and SPM

sync-by-unito[bot] commented 3 months ago

➤ PM Bot commented:

Jira ticket: RCOCOA-2323

aehlke commented 3 weeks ago

I also run into this. Ever solve it? Seems like Realm considers it working or user error

@tgoyne does the guidance in https://github.com/realm/realm-swift/issues/8017#issuecomment-1540455131 still apply? Thanks

gongzhang commented 3 weeks ago

@aehlke I resolved the issue by moving the Realm database out of the shared group. I still use the shared group to exchange data between processes, but I never open a Realm database file within the shared group. Additionally, I utilize UserDefaults to implement observation.

Overall, any flock() in the shared group will lead to a background crash.

tgoyne commented 3 weeks ago

Looks like we aren't using the emulated file lock that we wrote for iOS on visionOS and need to be. This doesn't fix all of the problems, but does make it so that merely reading from an already-open Realm won't kill the process.