realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

Crash when access Realm on background

Open mmar001 opened this issue 2 years ago • 8 comments

How frequently does the bug occur?

Sometimes

Description

I face the same problem here in my app, but set protection attribute to .none does not solve the issue. Here the code sample:

    let  folderPath = config.fileURL!.deletingLastPathComponent().path
    do {
        let attributes = try FileManager.default.attributesOfItem(atPath: folderPath)
        let protectionType = attributes[.protectionKey] as? FileProtectionType
        if protectionType != .some(FileProtectionType.none) {
            try FileManager.default.setAttributes([.protectionKey: FileProtectionType.none], ofItemAtPath: folderPath)
        }
    } catch {
        log("Unable to set protection attributes for realm folder: \(error)")
    }`

below the crash reported only in crashlytics on live app

Can someone help?

Stacktrace & log output

0  Realm                          0x198d10 realm::DB::grab_read_lock(realm::DB::ReadLockInfo&, realm::VersionID) + 424
1  Realm                          0x193be8 realm::DB::start_read(realm::VersionID) + 200
2  Realm                          0x44dae4 realm::Realm::begin_read(realm::VersionID) + 44
3  Realm                          0x44da04 realm::Realm::transaction() + 112
4  Realm                          0x646a0 realm::Object realm::Object::get_for_primary_key<objc_object* __strong, RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm> const&, realm::ObjectSchema const&, objc_object* __strong) + 148
5  Realm                          0x64484 RLMGetObject + 280
6  RealmSwift                     0x6ec14 $s10RealmSwift0A0V6object6ofType13forPrimaryKeyxSgxm_q_tSo0aB6ObjectCRbzr0_lF + 240

Can you reproduce the bug?

Not yet

Reproduction Steps

No response

Version

10.28.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

iOS 15.4.1 ~ 15.6.1 - multiple iPhone

Build environment

Xcode version: .Xcode 13.3

mmar001 avatar Aug 25 '22 13:08 mmar001

Hi @mmar001 Are you using AppGroups? Could you paste the entire stack trace to help us to investigate further?

leemaguire avatar Aug 26 '22 08:08 leemaguire

I had a look at your other issue.

let db = try Realm(configuration: appConf)
fixRealmBackgroundCrash(config: appConf)

setting the file protection attributes must come before opening the database. Can you change it to

fixRealmBackgroundCrash(config: appConf)
let db = try Realm(configuration: appConf)

leemaguire avatar Aug 26 '22 08:08 leemaguire

Actually, that shouldn't matter, we will need to investigate further.

leemaguire avatar Aug 26 '22 08:08 leemaguire

We don't use AppGroups

Crashed: com.apple.main-thread
0  Realm                          0x198d10 realm::DB::grab_read_lock(realm::DB::ReadLockInfo&, realm::VersionID) + 424
1  Realm                          0x193be8 realm::DB::start_read(realm::VersionID) + 200
2  Realm                          0x44dae4 realm::Realm::begin_read(realm::VersionID) + 44
3  Realm                          0x44da04 realm::Realm::transaction() + 112
4  Realm                          0x646a0 realm::Object realm::Object::get_for_primary_key<objc_object* __strong, RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm> const&, realm::ObjectSchema const&, objc_object* __strong) + 148
5  Realm                          0x64484 RLMGetObject + 280
6  RealmSwift                     0x6ec14 $s10RealmSwift0A0V6object6ofType13forPrimaryKeyxSgxm_q_tSo0aB6ObjectCRbzr0_lF + 240
7  App                             0x8b5d28 getFromDb<A>(withPrimaryKey:) + 296 (DatabaseManager.swift:296)
…
11 RxSwift                        0x531f8 $s7RxSwift14ObservableTypePAAE9subscribe6onNext0F5Error0F9Completed0F8DisposedAA10Disposable_py7ElementQzcSg_ys0H0_pcSgyycSgAOtFyAA5EventOyAKGcfU_ + 272
12 RxSwift                        0x53574 $s7RxSwift14ObservableTypePAAE9subscribe6onNext0F5Error0F9Completed0F8DisposedAA10Disposable_py7ElementQzcSg_ys0H0_pcSgyycSgAOtFyAA5EventOyAKGcfU_TA + 48
13 RxSwift                        0x73dc $s7RxSwift17AnonymousObserverC6onCoreyyAA5EventOyxGF + 20
14 RxSwift                        0x5595c $s7RxSwift12ObserverBaseC2onyyAA5EventOyxGF + 276
15 RxSwift                        0x55aa0 $s7RxSwift12ObserverBaseCyxGAA0C4TypeA2aEP2onyyAA5EventOy7ElementQzGFTW + 20
16 RxSwift                        0x54d28 $s7RxSwift32ObserveOnSerialDispatchQueueSink33_277A93ABA8477198C125F3F26B2D4B62LLC9scheduler8observer6cancelADyxGAA0efG9SchedulerC_xAA10Cancelable_ptcfcAA10Disposable_pAH4sink_AA5EventOy7ElementQzG5eventt_tcfU_ + 556
17 RxSwift                        0x556a4 $s7RxSwift32ObserveOnSerialDispatchQueueSink33_277A93ABA8477198C125F3F26B2D4B62LLCyxGAA5EventOy7ElementAA12ObserverTypePQzGAA10Disposable_pIeggnr_AE_ALtAaM_pIegnr_AaIRzlTRTA + 148
18 RxSwift                        0x46d0c $s7RxSwift13MainSchedulerC16scheduleInternal_6actionAA10Disposable_px_AaF_pxctlFyycfU_ + 116
19 RxSwift                        0x2b0d0 $sIeg_IeyB_TR + 28
20 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
21 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
22 libdispatch.dylib              0x11f48 _dispatch_main_queue_drain + 928
23 libdispatch.dylib              0x11b98 _dispatch_main_queue_callback_4CF + 44
24 CoreFoundation                 0x51800 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
25 CoreFoundation                 0xb704 __CFRunLoopRun + 2532
26 CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific + 600
27 GraphicsServices               0x1374 GSEventRunModal + 164
28 UIKitCore                      0x514648 -[UIApplication _run] + 1100
29 UIKitCore                      0x295d90 UIApplicationMain + 364
…
31 ???                            0x1034d9ce4 (Missing)

com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x14a0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1ae4 mach_msg + 76
2  CoreFoundation                 0x6d30 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb1bc __CFRunLoopRun + 1180
4  CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific + 600
5  Foundation                     0x19464 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 236
6  Foundation                     0x5ae2c -[NSRunLoop(NSRunLoop) runUntilDate:] + 92
7  UIKitCore                      0x48e770 -[UIEventFetcher threadMain] + 524
8  Foundation                     0x6943c __NSThread__start__ + 808
9  libsystem_pthread.dylib        0x19ac _pthread_start + 148
10 libsystem_pthread.dylib        0xe68 thread_start + 8

com.google.firebase.crashlytics.MachExceptionServer
0  FirebaseCrashlytics            0x1cf2c FIRCLSProcessRecordAllThreads + 184
1  FirebaseCrashlytics            0x1d30c FIRCLSProcessRecordAllThreads + 1176
2  FirebaseCrashlytics            0x1617c FIRCLSHandler + 48
3  FirebaseCrashlytics            0x18f44 FIRCLSMachExceptionServer + 1236
4  libsystem_pthread.dylib        0x19ac _pthread_start + 148
5  libsystem_pthread.dylib        0xe68 thread_start + 8

com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x14a0 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1ae4 mach_msg + 76
2  CoreFoundation                 0x6d30 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb1bc __CFRunLoopRun + 1180
4  CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific + 600
5  CFNetwork                      0x27823c _CFURLStorageSessionDisableCache + 60032
6  Foundation                     0x6943c __NSThread__start__ + 808
7  libsystem_pthread.dylib        0x19ac _pthread_start + 148
8  libsystem_pthread.dylib        0xe68 thread_start + 8

queue.devices
0  libsystem_kernel.dylib         0x4ba4 poll + 8
…
16 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
17 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
18 libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
19 libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
20 libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
21 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
22 libsystem_pthread.dylib        0xe5c start_wqthread + 8

queue.wifiDet
0  libsystem_kernel.dylib         0x4ba4 poll + 8
…
16 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
17 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
18 libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
19 libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
20 libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
21 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
22 libsystem_pthread.dylib        0xe5c start_wqthread + 8

queue.NetworkSummary
0  libsystem_kernel.dylib         0x242c read + 8
…
27 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
28 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
29 libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
30 libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
31 libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
32 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
33 libsystem_pthread.dylib        0xe5c start_wqthread + 8

Realm notification listener
0  libsystem_kernel.dylib         0x2e18 kevent + 8
1  Realm                          0x3aaf04 realm::_impl::ExternalCommitHelper::listen() + 156
2  Realm                          0x3ab054 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, realm::_impl::ExternalCommitHelper::ExternalCommitHelper(realm::_impl::RealmCoordinator&)::$_0> >(void*) + 52
3  libsystem_pthread.dylib        0x19ac _pthread_start + 148
4  libsystem_pthread.dylib        0xe68 thread_start + 8

queue.DMASuperWifiProvider
0  libsystem_kernel.dylib         0x2600 __psynch_mutexwait + 8
1  libsystem_pthread.dylib        0x23dc _pthread_mutex_firstfit_lock_wait + 84
2  libsystem_pthread.dylib        0x952c _pthread_mutex_firstfit_lock_slow + 248
3  libc++.1.dylib                 0xf344 std::__1::recursive_mutex::lock() + 16
4  Realm                          0x198ba0 realm::DB::grab_read_lock(realm::DB::ReadLockInfo&, realm::VersionID) + 56
5  Realm                          0x193be8 realm::DB::start_read(realm::VersionID) + 200
6  Realm                          0x44dae4 realm::Realm::begin_read(realm::VersionID) + 44
7  Realm                          0x44da04 realm::Realm::transaction() + 112
8  Realm                          0x646a0 realm::Object realm::Object::get_for_primary_key<objc_object* __strong, RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm> const&, realm::ObjectSchema const&, objc_object* __strong) + 148
9  Realm                          0x64484 RLMGetObject + 280
10 RealmSwift                     0x6ec14 $s10RealmSwift0A0V6object6ofType13forPrimaryKeyxSgxm_q_tSo0aB6ObjectCRbzr0_lF + 240
…
15 RxSwift                        0x2b8e8 $s7RxSwift19AnonymousObservable33_95EBF5692819D58425EC2DD0512D115ALLC3run_6cancelAA10Disposable_p4sink_AaG_p12subscriptiontqd___AA10Cancelable_pt7ElementQyd__RszAA12ObserverTypeRd__lF + 240
16 RxSwift                        0x5d264 $s7RxSwift8ProducerC9subscribeyAA10Disposable_pqd__7ElementQyd__RszAA12ObserverTypeRd__lFAaE_pyt_tcfU_ + 228
17 RxSwift                        0x5c950 $s7RxSwift22CurrentThreadSchedulerC8schedule_6actionAA10Disposable_px_AaF_pxctlFyt_Tg5 + 168
18 RxSwift                        0x5c850 $s7RxSwift8ProducerC9subscribeyAA10Disposable_pqd__7ElementQyd__RszAA12ObserverTypeRd__lF + 496
19 RxSwift                        0x50b2c $s7RxSwift10ObservableCyxGAA0C4TypeA2aEP9subscribeyAA10Disposable_pqd__AA08ObserverD0Rd__7ElementQyd__AJRtzlFTW + 28
20 RxSwift                        0x77828 $s7RxSwift15SubscribeOnSink33_B44C3DD7F62EF81799E6347E59A266A2LLC3runAA10Disposable_pyFAaF_pyt_tcfU_ + 152
21 RxSwift                        0x35ccc $s7RxSwift26DispatchQueueConfigurationV8schedule_6actionAA10Disposable_px_AaF_pxctlFyycfU_TA + 120
22 RxSwift                        0x2b0d0 $sIeg_IeyB_TR + 28
23 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
24 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
25 libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
26 libdispatch.dylib              0xbcb4 _dispatch_lane_invoke + 444
27 libdispatch.dylib              0xb000 _dispatch_lane_serial_drain + 376
28 libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
29 libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
30 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
31 libsystem_pthread.dylib        0xe5c start_wqthread + 8

queue.discovery
0  libsystem_kernel.dylib         0x4ba4 poll + 8
…
16 libswiftDispatch.dylib         0x2598 partial apply for thunk for @callee_guaranteed () -> (@out A, @error @owned Error) + 28
17 libswiftDispatch.dylib         0x9af8 thunk for @callee_guaranteed () -> (@out A, @error @owned Error)partial apply + 16
18 libswiftDispatch.dylib         0x3958 closure #1 in closure #1 in OS_dispatch_queue._syncHelper<A>(fn:execute:rescue:) + 188
19 libswiftDispatch.dylib         0x3a6c partial apply for thunk for @callee_guaranteed () -> () + 28
20 libswiftDispatch.dylib         0x3384 thunk for @escaping @callee_guaranteed () -> () + 28
21 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
22 libdispatch.dylib              0x12c90 _dispatch_lane_barrier_sync_invoke_and_complete + 56
23 libswiftDispatch.dylib         0x2cac implicit closure #2 in implicit closure #1 in OS_dispatch_queue.sync<A>(execute:) + 180
24 libswiftDispatch.dylib         0x1d0c partial apply for implicit closure #2 in implicit closure #1 in OS_dispatch_queue.sync<A>(execute:) + 56
25 libswiftDispatch.dylib         0x29ec OS_dispatch_queue._syncHelper<A>(fn:execute:rescue:) + 396
26 libswiftDispatch.dylib         0x1dbc OS_dispatch_queue.sync<A>(execute:) + 168
…
30 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release + 32
31 libdispatch.dylib              0x3a30 _dispatch_client_callout + 20
32 libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
33 libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
34 libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
35 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
36 libsystem_pthread.dylib        0xe5c start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x1a74 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1108 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe5c start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x1a74 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1108 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe5c start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x1a74 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1108 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe5c start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x1a74 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1108 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe5c start_wqthread + 8

mmar001 avatar Aug 26 '22 09:08 mmar001

This doesn't look related to file locking, but something else has the read lock on the database. Do you have realm running in multiple threads? Is this only happening when the app is in the background?

leemaguire avatar Aug 26 '22 09:08 leemaguire

If its happening in the background only, you probably need to extend the background execution time. https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time

leemaguire avatar Aug 26 '22 09:08 leemaguire

Yes it only happens in background, then I'll try to extend the background execution time. And yes we use Realm in multiple thread.

mmar001 avatar Aug 26 '22 12:08 mmar001

Hi @mmar001 did the change to the execution time fixed the issue?, please let us know if the problem continues or nor

dianaafanador3 avatar Sep 12 '22 09:09 dianaafanador3

@mmar001 do you have any updates on this issue?

leemaguire avatar Sep 26 '22 13:09 leemaguire

Closing this as there is no response from the user.

dianaafanador3 avatar Feb 08 '23 20:02 dianaafanador3