Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashed: Realm Notification Listener (BACKGROUND) #8498

Closed
sergeiromanchuk opened this issue Mar 2, 2024 · 9 comments
Closed

Crashed: Realm Notification Listener (BACKGROUND) #8498

sergeiromanchuk opened this issue Mar 2, 2024 · 9 comments

Comments

@sergeiromanchuk
Copy link

sergeiromanchuk commented Mar 2, 2024

How frequently does the bug occur?

Sometimes

Description

Firebase title: realm::Transaction::Transaction(std::__1::shared_ptrrealm::DB, realm::SlabAlloc*, realm::DB::ReadLockInfo&, realm::DB::TransactStage)
EXC_BAD_ACCESS (KERN_INVALID_ADDRESS)

This bug happens in production and it's a big problem for us, but we can't figure out what's wrong. The bug occurs 100% in BACKGROUND.

There are suspicions that NotificationToken should be invalidated when you go to BACKGROUND. We don't do this, only when the class is deinit only in this case we invalidate the token.

Stacktrace & log output

Crashed: Realm notification listener
0  Example                          0x123d66c realm::Transaction::Transaction(std::__1::shared_ptr<realm::DB>, realm::SlabAlloc*, realm::DB::ReadLockInfo&, realm::DB::TransactStage) + 88568
1  Example                          0x11186f0 std::__1::shared_ptr<realm::Transaction> (anonymous namespace)::make_transaction_ref<std::__1::shared_ptr<realm::DB>, realm::SlabAlloc*, realm::DB::ReadLockInfo&, realm::DB::TransactStage>(std::__1::shared_ptr<realm::DB>&&, realm::SlabAlloc*&&, realm::DB::ReadLockInfo&, realm::DB::TransactStage&&) + 189196
2  Example                          0x1112dac realm::DB::start_read(realm::VersionID) + 166344
3  Example                          0x13749fc realm::_impl::RealmCoordinator::run_async_notifiers() + 1363336
4  Example                          0x137492c realm::_impl::RealmCoordinator::on_change() + 1363128
5  Example                          0x135924c realm::_impl::ExternalCommitHelper::listen() + 1250776
6  Example                          0x1359378 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&, realm::RealmConfig const&)::$_0>>(void*) + 1251076
7  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
8  libsystem_pthread.dylib        0xb220 _pthread_start + 44
9  libsystem_pthread.dylib        0xecdc thread_start + 4



com.apple.main-thread
0  libc++.1.dylib                 0xf190 std::__1::basic_ostream<wchar_t, std::__1::char_traits<wchar_t> >::flush() + 486
1  libsystem_c.dylib              0x5bbb4 __cxa_finalize_ranges + 384
2  libsystem_c.dylib              0x5bec4 exit + 24
3  UIKitCore                      0x8c3fec -[UIApplication terminateWithSuccess] + 466
4  UIKitCore                      0x17fd7c __98-[__UICanvasLifecycleMonitor_Compatability deactivateEventsOnly:withContext:forceExit:completion:]_block_invoke.261 + 344
5  UIKitCore                      0x8c80ac _runAfterCACommitDeferredBlocks + 296
6  UIKitCore                      0x8b6bfc _cleanUpAfterCAFlushAndRunDeferredBlocks + 352
7  UIKitCore                      0x8e3a6c _afterCACommitHandler + 116
8  CoreFoundation                 0xaa4fc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
9  CoreFoundation                 0xa5224 __CFRunLoopDoObservers + 412
10 CoreFoundation                 0xa57a0 __CFRunLoopRun + 1228
11 CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
12 GraphicsServices               0xa79c GSEventRunModal + 104
13 UIKitCore                      0x8bcc38 UIApplicationMain + 212
14 Example                          0x19015c main + 22 (AppDelegate.swift:22)
15 libdyld.dylib                  0x18e0 start + 4



com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x180f4 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x175a0 mach_msg + 72
2  CoreFoundation                 0xaa914 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0xa5824 __CFRunLoopRun + 1360
4  CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
5  Foundation                     0x795c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300
6  Foundation                     0x77ec -[NSRunLoop(NSRunLoop) runUntilDate:] + 96
7  UIKitCore                      0x9a2754 -[UIEventFetcher threadMain] + 136
8  Foundation                     0x1344a0 __NSThread__start__ + 984
9  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
10 libsystem_pthread.dylib        0xb220 _pthread_start + 44
11 libsystem_pthread.dylib        0xecdc thread_start + 4



com.google.firebase.crashlytics.MachExceptionServer
0  Example                          0x14e6954 FIRCLSProcessRecordAllThreads + 393 (FIRCLSProcess.c:393)
1  Example                          0x14e6d34 FIRCLSProcessRecordAllThreads + 424 (FIRCLSProcess.c:424)
2  Example                          0x14de180 FIRCLSHandler + 34 (FIRCLSHandler.m:34)
3  Example                          0x14e09e0 FIRCLSMachExceptionServer + 521 (FIRCLSMachException.c:521)
4  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
5  libsystem_pthread.dylib        0xb220 _pthread_start + 44
6  libsystem_pthread.dylib        0xecdc thread_start + 4



com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x180f4 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x175a0 mach_msg + 72
2  CoreFoundation                 0xaa914 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0xa5824 __CFRunLoopRun + 1360
4  CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
5  CFNetwork                      0x174c -[__CoreSchedulingSetRunnable runForever] + 216
6  Foundation                     0x1344a0 __NSThread__start__ + 984
7  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
8  libsystem_pthread.dylib        0xb220 _pthread_start + 44
9  libsystem_pthread.dylib        0xecdc thread_start + 4
AVAudioSession Notify Thread
0  libsystem_kernel.dylib         0x180f4 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x175a0 mach_msg + 72
2  CoreFoundation                 0xaa914 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0xa5824 __CFRunLoopRun + 1360
4  CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
5  AVFAudio                       0x8c334 GenericRunLoopThread::Entry(void*) + 156
6  AVFAudio                       0xb6c60 CAPThread::Entry(CAPThread*) + 88
7  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
8  libsystem_pthread.dylib        0xb220 _pthread_start + 44
9  libsystem_pthread.dylib        0xecdc thread_start + 4



com.apple.CFSocket.private
0  libsystem_kernel.dylib         0x23328 __select + 8
1  CoreFoundation                 0xb35f8 __CFSocketManager + 620
2  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
3  libsystem_pthread.dylib        0xb220 _pthread_start + 44
4  libsystem_pthread.dylib        0xecdc thread_start + 4



JavaScriptCore bmalloc scavenger
0  libsystem_kernel.dylib         0x22ee4 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x3cf8 <redacted> + 636
2  libc++.1.dylib                 0x8090 std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 24
3  JavaScriptCore                 0x8aaa0 <redacted> + 108
4  JavaScriptCore                 0x8ea94 <redacted> + 176
5  JavaScriptCore                 0x8e20c <redacted> + 10
6  JavaScriptCore                 0x8fc4c <redacted> + 38
7  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
8  libsystem_pthread.dylib        0xb220 _pthread_start + 44
9  libsystem_pthread.dylib        0xecdc thread_start + 4



com.apple.coreaudio.AQClient
0  libsystem_kernel.dylib         0x180f4 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x175a0 mach_msg + 72
2  CoreFoundation                 0xaa914 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0xa5824 __CFRunLoopRun + 1360
4  CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
5  AudioToolbox                   0x2317bc <redacted> + 156
6  AudioToolbox                   0x4c04f8 <redacted> + 88
7  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
8  libsystem_pthread.dylib        0xb220 _pthread_start + 44
9  libsystem_pthread.dylib        0xecdc thread_start + 4



com.apple.CFStream.LegacyThread
0  libsystem_kernel.dylib         0x180f4 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x175a0 mach_msg + 72
2  CoreFoundation                 0xaa914 __CFRunLoopServiceMachPort + 236
3  CoreFoundation                 0xa5824 __CFRunLoopRun + 1360
4  CoreFoundation                 0xa4fb4 CFRunLoopRunSpecific + 436
5  CoreFoundation                 0xbdcf4 _legacyStreamRunLoop_workThread + 260
6  libsystem_pthread.dylib        0xb2c0 <redacted> + 128
7  libsystem_pthread.dylib        0xb220 _pthread_start + 44
8  libsystem_pthread.dylib        0xecdc thread_start + 4


com.example.RealmQueue
0  libsystem_kernel.dylib         0x22f2c __psynch_mutexwait + 8
1  libsystem_pthread.dylib        0x4a84 <redacted> + 92
2  libsystem_pthread.dylib        0x49f4 <redacted> + 272
3  libc++.1.dylib                 0x3c610 std::__1::mutex::lock() + 12
4  Example                          0x137432c realm::_impl::RealmCoordinator::commit_write(realm::Realm&, bool) + 1361592
5  Example                          0x13bdcd4 realm::Realm::commit_transaction() + 22180
6  Example                          0x17382d0 -[RLMRealm commitWriteTransactionWithoutNotifying:error:] + 648 (RLMRealm.mm:648)
7  Example                          0x17ca298 Realm.commitWrite(withoutNotifying:) + 332 (Realm.swift:332)
8  Example                          0x17ca1b4 Realm.write<A>(withoutNotifying:_:) + 270 (Realm.swift:270)
9  Example                          0x12c88c specialized Realm.safeWrite(file:function:line:_:) + 188 (Realm+Helpers.swift:188)
10 Example                          0x38ca3c closure #1 in RoomGameTableRepo.removeRoomGameTables(using:) + 407 (RoomGameTableRepo.swift:407)
11 Example                          0x38e394 partial apply for closure #1 in RoomGameTableRepo.removeRoomGameTables(using:) + 4301038484 (<compiler-generated>:4301038484)
12 Example                          0x1402c4 closure #1 in static Realm.async(on:configuration:file:function:line:action:) + 206 (Realm+Helpers.swift:206)
13 Example                          0x3e4b1c thunk for @escaping @callee_guaranteed () -> () + 4301392668 (<compiler-generated>:4301392668)
14 Example                          0x13e5dc partial apply for thunk for @escaping @callee_guaranteed () -> (@unowned ()?) + 4298614236 (<compiler-generated>:4298614236)
15 Example                          0x140cb8 partial apply for specialized closure #1 in static Realm.performTaskOnRealmQueue<A>(method:file:function:line:_:) + 236 (Realm+Helpers.swift:236)
16 Example                          0x2dc27c thunk for @escaping @callee_guaranteed @Sendable () -> () + 4300309116 (<compiler-generated>:4300309116)
17 libdispatch.dylib              0x5fa38 _dispatch_call_block_and_release + 24
18 libdispatch.dylib              0x607d4 _dispatch_client_callout + 16
19 libdispatch.dylib              0x9324 _dispatch_lane_serial_drain$VARIANT$mp + 592
20 libdispatch.dylib              0x9e40 _dispatch_lane_invoke$VARIANT$mp + 428
21 libdispatch.dylib              0x124ac _dispatch_workloop_worker_thread + 596
22 libsystem_pthread.dylib        0xc114 _pthread_wqthread + 304
23 libsystem_pthread.dylib        0xecd4 start_wqthread + 4



notificationQueue
0  libsystem_kernel.dylib         0x22ee4 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x3cf8 <redacted> + 636
2  Foundation                     0x753b8 -[__NSOperationInternal _waitUntilFinished:] + 772
3  Foundation                     0x2fea4 -[__NSObserver _doit:] + 240
4  CoreFoundation                 0x8a21c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
5  CoreFoundation                 0x8a1e8 ___CFXRegistrationPost_block_invoke + 64
6  CoreFoundation                 0x896dc _CFXRegistrationPost + 392
7  CoreFoundation                 0x89388 ___CFXNotificationPost_block_invoke + 96
8  CoreFoundation                 0x2c54 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
9  CoreFoundation                 0x88e38 _CFXNotificationPost + 696
10 Foundation                     0x61a4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
11 Example                          0x3df618 closure #1 in postNotification(name:object:userInfo:) + 246 (Util.swift:246)
12 Example                          0x3df870 partial apply for closure #1 in closure #1 in runInMainQueue(_:) + 56 (Util.swift:56)
13 Example                          0x2dc27c thunk for @escaping @callee_guaranteed @Sendable () -> () + 4300309116 (<compiler-generated>:4300309116)
14 libdispatch.dylib              0x5fa38 _dispatch_call_block_and_release + 24
15 libdispatch.dylib              0x607d4 _dispatch_client_callout + 16
16 libdispatch.dylib              0x9324 _dispatch_lane_serial_drain$VARIANT$mp + 592
17 libdispatch.dylib              0x9e40 _dispatch_lane_invoke$VARIANT$mp + 428
18 libdispatch.dylib              0x124ac _dispatch_workloop_worker_thread + 596
19 libsystem_pthread.dylib        0xc114 _pthread_wqthread + 304
20 libsystem_pthread.dylib        0xecd4 start_wqthread + 4

Can you reproduce the bug?

No

Reproduction Steps

No response

Version

10.47.0

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 12-17

Build environment

Xcode version: 14.2-15.2
Dependency manager and version: Cocoapods 1.15.2

Copy link

sync-by-unito bot commented Mar 2, 2024

➤ PM Bot commented:

Jira ticket: RCOCOA-2298

@sergeiromanchuk
Copy link
Author

@nirinchev

Could it have any effect that we create notificationToken on our serial queue? (Of course if it was wrong I think the application would always crash, but anyway).

@nirinchev
Copy link
Member

It looks like something is going wrong just trying to create a read transaction in the database. I'm not super familiar with the backgrounding aspects of iOS, but will bring it to the attention of the team on our weekly review meeting.

(for context, I've assigned myself temporarily until we've discussed it and will hand it off to someone who's more knowledgeable)

@tgoyne
Copy link
Member

tgoyne commented Mar 8, 2024

This may be a file protection-related crash. If NSFileProtectionComplete is set on the Realm file or any of our helper files then access to the file is revoked while the device is locked, and attempting to use the files will crash.

@sergeiromanchuk
Copy link
Author

sergeiromanchuk commented Mar 8, 2024

This may be a file protection-related crash. If NSFileProtectionComplete is set on the Realm file or any of our helper files then access to the file is revoked while the device is locked, and attempting to use the files will crash.

The attribute of the file itself is NSFileProtectionCompleteUntilFirstUserAuthentication

How to remove file protection for Realm files? I use this code in willFinishLaunchingWithOptions

let realmConfiguration = Realm.Configuration()
guard let realmFilesDirectoryURL = realmConfiguration.fileURL?.deletingLastPathComponent() else { return }
let realmFilesDirectoryPath: String = if #available(iOS 16.0, *) {
    realmFilesDirectoryURL.path()
} else {
    realmFilesDirectoryURL.path
}
do {
    let attributes = try FileManager.default.attributesOfItem(atPath: realmFilesDirectoryPath)
    guard let protectionType = attributes[.protectionKey] as? FileProtectionType, protectionType != .none else { return }
    try FileManager.default.setAttributes(
        [.protectionKey: FileProtectionType.none],
        ofItemAtPath: realmFilesDirectoryPath
    )
} catch {
    PLog.local(
        message: "Unable to set protection attributes for realm folder: \(error)",
        level: .error
    )
}

@sergeiromanchuk
Copy link
Author

@tgoyne @nirinchev

Doesn't this line indicate that this happens when the user closes the application? As far as I understand this line is not called when the application crashes, is it?

3 UIKitCore 0x8c3fec -[UIApplication terminateWithSuccess] + 466

@nirinchev
Copy link
Member

See @tgoyne's comment on the other issue as it's also applicable here: realm/realm-core#7420 (comment)

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Mar 11, 2024
@sergeiromanchuk
Copy link
Author

See @tgoyne's comment on the other issue as it's also applicable here: realm/realm-core#7420 (comment)

@nirinchev 👉👉👉 realm/realm-core#7420 (comment)

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Mar 12, 2024
@nirinchev
Copy link
Member

I'll close this issue as it's the same root cause as realm/realm-core#7420 and we can keep the discussion in a single place.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2024
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Mar 12, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants