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

Crash when Realm is querying objects in a process while another process is committing transactions #7111

Open
alinradut opened this issue Nov 2, 2023 · 12 comments
Assignees

Comments

@alinradut
Copy link

alinradut commented Nov 2, 2023

SDK and version

SDK : RealmSwift
Version: 10.44.0

Observations

  • How frequent do the crash occur?
    Reproducible almost always.
  • Does it happen in production or during dev/test?
    Both
  • Can the crash be reproduced by you?
    Yes
  • Can you provide instructions for how we can reproduce it?
    Yes, a test project is available at https://github.com/alinradut/realm-has_refs-crash

Crash log / stacktrace

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: FFF93C70-F621-4A71-98D2-380B969B8E2C
CrashReporter Key:   3398b348cc29922d801c4b9772f7dfced750e88f
Hardware Model:      iPhone8,1
Process:             realmcrash share [559]
Path:                /private/var/containers/Bundle/Application/D4CB2D33-0B28-46CF-9253-08B21263E3D3/realmcrash.app/PlugIns/realmcrash share.appex/realmcrash share
Identifier:          com.example.realmcrash.realmcrash-share
Version:             1.0 (1)
Code Type:           ARM-64 (Native)
Role:                Background
Parent Process:      launchd [1]
Coalition:           com.example.realmcrash.realmcrash-share [886]

Date/Time:           2023-11-02 23:32:21.9368 +0200
Launch Time:         2023-11-02 23:32:05.5630 +0200
OS Version:          iPhone OS 15.8 (19H370)
Release Type:        User
Baseband Version:    9.61.00
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  5

Application Specific Information:
abort() called


Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib        	       0x1bb7c5aac mach_msg_trap + 8
1   libsystem_kernel.dylib        	       0x1bb7c607c mach_msg + 72
2   CoreFoundation                	       0x180e7fc88 __CFRunLoopServiceMachPort + 368
3   CoreFoundation                	       0x180e83f90 __CFRunLoopRun + 1160
4   CoreFoundation                	       0x180e97174 CFRunLoopRunSpecific + 572
5   GraphicsServices              	       0x1a19b2988 GSEventRunModal + 160
6   UIKitCore                     	       0x183699a88 -[UIApplication _run] + 1080
7   UIKitCore                     	       0x183432f78 UIApplicationMain + 336
8   libxpc.dylib                  	       0x1dc35fdc4 _xpc_objc_main + 440
9   libxpc.dylib                  	       0x1dc361e30 xpc_main + 152
10  Foundation                    	       0x1825ea11c -[NSXPCListener resume] + 292
11  PlugInKit                     	       0x1be8f04e4 0x1be8d3000 + 120036
12  PlugInKit                     	       0x1be8da8b4 0x1be8d3000 + 30900
13  PlugInKit                     	       0x1be8d9910 0x1be8d3000 + 26896
14  ExtensionFoundation           	       0x1ca6883bc EXExtensionMain + 296
15  Foundation                    	       0x18264958c NSExtensionMain + 216
16  dyld                          	       0x1034c44d0 start + 444

Thread 1:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0

Thread 2 name:   Dispatch queue: com.apple.root.default-qos
Thread 2:
0   libsystem_kernel.dylib        	       0x1bb7c6af0 __psynch_mutexwait + 8
1   libsystem_pthread.dylib       	       0x1dc32f530 _pthread_mutex_firstfit_lock_wait + 80
2   libsystem_pthread.dylib       	       0x1dc335950 _pthread_mutex_firstfit_lock_slow$VARIANT$mp + 304
3   realmcrash share              	       0x100998bd0 realm::util::Mutex::lock() + 24
4   realmcrash share              	       0x100998ba0 realm::util::UniqueLock::UniqueLock(realm::util::Mutex&) + 44
5   realmcrash share              	       0x100998b38 realm::util::UniqueLock::UniqueLock(realm::util::Mutex&) + 36
6   realmcrash share              	       0x100998ac0 realm::util::do_encryption_read_barrier(void const*, unsigned long, unsigned long (*)(char const*), realm::util::EncryptedFileMapping*, bool) + 60
7   realmcrash share              	       0x100998244 realm::util::encryption_read_barrier(void const*, unsigned long, realm::util::EncryptedFileMapping*, unsigned long (*)(char const*), bool) + 88
8   realmcrash share              	       0x1009a62d0 realm::Allocator::translate_critical(realm::Allocator::RefTranslation*, unsigned long) const + 244
9   realmcrash share              	       0x10099b3e0 realm::Allocator::translate(unsigned long) const + 80
10  realmcrash share              	       0x100a41268 realm::ClusterNodeInner::get_leaf(realm::ObjKey, realm::ClusterNode::IteratorState&) const + 660
11  realmcrash share              	       0x100a43fa0 realm::ClusterTree::get_leaf(realm::ObjKey, realm::ClusterNode::IteratorState&) const + 428
12  realmcrash share              	       0x100a44584 realm::ClusterTree::Iterator::load_leaf(realm::ObjKey) const + 72
13  realmcrash share              	       0x100a44434 realm::ClusterTree::Iterator::Iterator(realm::ClusterTree const&, unsigned long) + 316
14  realmcrash share              	       0x100a44600 realm::ClusterTree::Iterator::Iterator(realm::ClusterTree const&, unsigned long) + 44
15  realmcrash share              	       0x1011855fc realm::Table::begin() const + 52
16  realmcrash share              	       0x100cc1268 realm::Results::IteratorWrapper::get(realm::Table const&, unsigned long) + 140
17  realmcrash share              	       0x100cbc790 std::__1::optional<realm::Obj> realm::Results::try_get<realm::Obj>(unsigned long) + 244
18  realmcrash share              	       0x100cbc5a8 realm::Obj realm::Results::get<realm::Obj>(unsigned long) + 60
19  realmcrash share              	       0x1005a97b8 RLMAccessorContext auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)::operator()<realm::Obj*>(RLMAccessorContext&) const + 56
20  realmcrash share              	       0x1005a91c4 RLMAccessorContext realm::switch_on_type<realm::Obj, auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)>(realm::PropertyType, auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)&&) + 1412
21  realmcrash share              	       0x1005a8c34 RLMAccessorContext realm::Results::dispatch<auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)>(RLMAccessorContext&) const + 40
22  realmcrash share              	       0x1005a37c8 auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long) + 56
23  realmcrash share              	       0x1005a35d0 -[RLMFastEnumerator countByEnumeratingWithState:count:] + 296
24  realmcrash share              	       0x1005a3980 RLMFastEnumerate(NSFastEnumerationState*, unsigned long, id<RLMCollectionPrivate>) + 220
25  realmcrash share              	       0x1007cd4f8 -[RLMResults countByEnumeratingWithState:objects:count:] + 140
26  libswiftFoundation.dylib      	       0x1853205c0 NSFastEnumerationIterator.next() + 180
27  realmcrash share              	       0x100916168 RLMIterator.next() + 52
28  realmcrash share              	       0x100916214 protocol witness for IteratorProtocol.next() in conformance RLMIterator<A> + 12
29  libswiftCore.dylib            	       0x18579bfac Sequence._copySequenceContents(initializing:) + 404
30  realmcrash share              	       0x100931ff8 protocol witness for Sequence._copyContents(initializing:) in conformance Results<A> + 12
31  libswiftCore.dylib            	       0x1856584d0 _copyCollectionToContiguousArray<A>(_:) + 624
32  libswiftCore.dylib            	       0x185646c58 Sequence._copyToContiguousArray() + 40
33  realmcrash share              	       0x100931fe0 protocol witness for Sequence._copyToContiguousArray() in conformance Results<A> + 44
34  libswiftCore.dylib            	       0x18564e2a4 Array.init<A>(_:) + 32
35  realmcrash share              	       0x10052f040 closure realm/realm-swift#1 in static RealmHelper.startReadTest() + 304
36  realmcrash share              	       0x10052ebb4 thunk for @escaping @callee_guaranteed @Sendable () -> () + 48
37  libdispatch.dylib             	       0x180bcb094 _dispatch_call_block_and_release + 24
38  libdispatch.dylib             	       0x180bcc094 _dispatch_client_callout + 16
39  libdispatch.dylib             	       0x180b6e858 _dispatch_queue_override_invoke + 720
40  libdispatch.dylib             	       0x180b7bb94 _dispatch_root_queue_drain + 340
41  libdispatch.dylib             	       0x180b7c39c _dispatch_worker_thread2 + 172
42  libsystem_pthread.dylib       	       0x1dc32cdc4 _pthread_wqthread + 224
43  libsystem_pthread.dylib       	       0x1dc32c92c start_wqthread + 8

Thread 3 name:  com.apple.uikit.eventfetch-thread
Thread 3:
0   libsystem_kernel.dylib        	       0x1bb7c5aac mach_msg_trap + 8
1   libsystem_kernel.dylib        	       0x1bb7c607c mach_msg + 72
2   CoreFoundation                	       0x180e7fc88 __CFRunLoopServiceMachPort + 368
3   CoreFoundation                	       0x180e83f90 __CFRunLoopRun + 1160
4   CoreFoundation                	       0x180e97174 CFRunLoopRunSpecific + 572
5   Foundation                    	       0x1825a3eac -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 232
6   Foundation                    	       0x1825e2fd0 -[NSRunLoop(NSRunLoop) runUntilDate:] + 88
7   UIKitCore                     	       0x183618ef4 -[UIEventFetcher threadMain] + 512
8   Foundation                    	       0x1825f0bdc __NSThread__start__ + 792
9   libsystem_pthread.dylib       	       0x1dc32e338 _pthread_start + 116
10  libsystem_pthread.dylib       	       0x1dc32c938 thread_start + 8

Thread 4:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0

Thread 5 name:   Dispatch queue: com.apple.root.default-qos
Thread 5 Crashed:
0   libsystem_kernel.dylib        	       0x1bb7cbbbc __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x1dc338844 pthread_kill + 208
2   libsystem_c.dylib             	       0x18b5f86ac abort + 124
3   realmcrash share              	       0x101236320 please_report_this_issue_in_github_realm_realm_core_v_13_23_1 + 12
4   realmcrash share              	       0x1012365b0 realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 292
5   realmcrash share              	       0x101236488 realm::util::terminate_with_info(char const*, char const*, long, char const*, std::initializer_list<realm::util::Printable>&&) + 308
6   realmcrash share              	       0x1011eca54 void realm::util::terminate_with_info<unsigned long&, unsigned long, unsigned long, unsigned long, unsigned long const&>(char const*, int, char const*, char const*, unsigned long&, unsigned long&&, unsigned long&&, unsigned long&&, unsigned long const&) + 244
7   realmcrash share              	       0x1011eaee8 realm::util::EncryptedFileMapping::get_local_index_of_address(void const*, unsigned long) const + 276
8   realmcrash share              	       0x1011eb19c realm::util::EncryptedFileMapping::read_barrier(void const*, unsigned long, unsigned long (*)(char const*), bool) + 548
9   realmcrash share              	       0x100998adc realm::util::do_encryption_read_barrier(void const*, unsigned long, unsigned long (*)(char const*), realm::util::EncryptedFileMapping*, bool) + 88
10  realmcrash share              	       0x100998244 realm::util::encryption_read_barrier(void const*, unsigned long, realm::util::EncryptedFileMapping*, unsigned long (*)(char const*), bool) + 88
11  realmcrash share              	       0x1009a62d0 realm::Allocator::translate_critical(realm::Allocator::RefTranslation*, unsigned long) const + 244
12  realmcrash share              	       0x10099b3e0 realm::Allocator::translate(unsigned long) const + 80
13  realmcrash share              	       0x1009bdcc8 realm::Array::init_from_ref(unsigned long) + 108
14  realmcrash share              	       0x100ab327c realm::Group::attach(unsigned long, bool, bool, unsigned long, unsigned long long) + 268
15  realmcrash share              	       0x100ab6434 realm::Group::attach_shared(unsigned long, unsigned long, bool, realm::VersionID) + 264
16  realmcrash share              	       0x1011cd660 realm::Transaction::Transaction(std::__1::shared_ptr<realm::DB>, realm::SlabAlloc*, realm::DB::ReadLockInfo&, realm::DB::TransactStage) + 400
17  realmcrash share              	       0x1011cd938 realm::Transaction::Transaction(std::__1::shared_ptr<realm::DB>, realm::SlabAlloc*, realm::DB::ReadLockInfo&, realm::DB::TransactStage) + 60
18  realmcrash share              	       0x100a5a328 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&&) + 112
19  realmcrash share              	       0x100a52bc4 realm::DB::start_read(realm::VersionID) + 312
20  realmcrash share              	       0x100c525a8 realm::_impl::RealmCoordinator::begin_read(realm::VersionID, bool) + 200
21  realmcrash share              	       0x100d8361c realm::Realm::begin_read(realm::VersionID) + 160
22  realmcrash share              	       0x100d834c0 realm::Realm::transaction() + 120
23  realmcrash share              	       0x100d8343c realm::Realm::read_group() + 24
24  realmcrash share              	       0x1007aecac -[RLMRealm group] + 36
25  realmcrash share              	       0x100597e34 RLMClassInfo::table() const + 68
26  realmcrash share              	       0x10063b6f8 RLMGetObjects + 116
27  realmcrash share              	       0x10090b86c Realm.objects<A>(_:) + 124
28  realmcrash share              	       0x10052ef80 closure realm/realm-swift#1 in static RealmHelper.startReadTest() + 112
29  realmcrash share              	       0x10052ebb4 thunk for @escaping @callee_guaranteed @Sendable () -> () + 48
30  libdispatch.dylib             	       0x180bcb094 _dispatch_call_block_and_release + 24
31  libdispatch.dylib             	       0x180bcc094 _dispatch_client_callout + 16
32  libdispatch.dylib             	       0x180b6e858 _dispatch_queue_override_invoke + 720
33  libdispatch.dylib             	       0x180b7bb94 _dispatch_root_queue_drain + 340
34  libdispatch.dylib             	       0x180b7c39c _dispatch_worker_thread2 + 172
35  libsystem_pthread.dylib       	       0x1dc32cdc4 _pthread_wqthread + 224
36  libsystem_pthread.dylib       	       0x1dc32c92c start_wqthread + 8

Thread 6:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0

Thread 8:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0

Thread 9:
0   libsystem_pthread.dylib       	       0x1dc32c924 start_wqthread + 0


Thread 5 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x000000000000000c   x5: 0x0000000136041600   x6: 0x0000000000001400   x7: 0x0000000000000000
    x8: 0x000000016f9ef000   x9: 0x359acb654a81cc3e  x10: 0x000000000075c600  x11: 0x0000000000bf3d01
   x12: 0x0000000000004009  x13: 0x0000000136040000  x14: 0x0000000000003fff  x15: 0x000000000000f8fc
   x16: 0x0000000000000148  x17: 0x000000000000f8fc  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000006617  x21: 0x000000016f9ef0e0  x22: 0x0000000000000000  x23: 0x0000000000000114
   x24: 0x000000016f9ef0e0  x25: 0x00000001f63b4204  x26: 0x00000000000005ff  x27: 0x00000001f5b05940
   x28: 0x0000000002ffffff   fp: 0x000000016f9ed520   lr: 0x00000001dc338844
    sp: 0x000000016f9ed500   pc: 0x00000001bb7cbbbc cpsr: 0x40000000
   far: 0x0000000105dd4000  esr: 0x56000080  Address size fault

Binary Images:
       0x1bb7c5000 -        0x1bb7f8fff libsystem_kernel.dylib arm64  <102e8613667633f6a0b4f6e86a8636ce> /usr/lib/system/libsystem_kernel.dylib
       0x180e79000 -        0x1812b6fff CoreFoundation arm64  <eec1287d059b38c89bc158a0c8b1e6c2> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
       0x1a19b1000 -        0x1a19b9fff GraphicsServices arm64  <bb434d860991365fbed3c3923cf3073c> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
       0x1831b4000 -        0x18494ffff UIKitCore arm64  <9d3018772593385c8f72f075aa0b48fa> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
       0x1dc34a000 -        0x1dc382fff libxpc.dylib arm64  <184a84ec2aaa3c9780d3d7584a851a54> /usr/lib/system/libxpc.dylib
       0x18258c000 -        0x182870fff Foundation arm64  <f9235fc7ec4e31c9b56e95cf10b07239> /System/Library/Frameworks/Foundation.framework/Foundation
       0x1be8d3000 -        0x1be906fff PlugInKit arm64  <10d9e3085df43d539205e3f5503b3d41> /System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit
       0x1ca683000 -        0x1ca6cafff ExtensionFoundation arm64  <2440eee59988368ca58390353581845c> /System/Library/PrivateFrameworks/ExtensionFoundation.framework/ExtensionFoundation
       0x1034ac000 -        0x1034fffff dyld arm64  <16c8ea1a1c773f4f97a66ae7fb25eb29> /usr/lib/dyld
       0x1dc32b000 -        0x1dc33bfff libsystem_pthread.dylib arm64  <6679a5b3a40a37a7b1c5565a0f5cb6ab> /usr/lib/system/libsystem_pthread.dylib
       0x100524000 -        0x10168bfff realmcrash share arm64  <5430c4091bf13cdaa9db0ed14a3ef768> /private/var/containers/Bundle/Application/D4CB2D33-0B28-46CF-9253-08B21263E3D3/realmcrash.app/PlugIns/realmcrash share.appex/realmcrash share
       0x18531f000 -        0x185638fff libswiftFoundation.dylib arm64  <dbe12326ab403e74bf0bb8c02e855c5c> /usr/lib/swift/libswiftFoundation.dylib
       0x185639000 -        0x185a96fff libswiftCore.dylib arm64  <a31c8583dc6d3d6f9ac9d721b291b35d> /usr/lib/swift/libswiftCore.dylib
       0x180b68000 -        0x180beafff libdispatch.dylib arm64  <9ccdbde315e13a45b3304a5e2c3f92bd> /usr/lib/system/libdispatch.dylib
       0x18b5d9000 -        0x18b654fff libsystem_c.dylib arm64  <f90936ac0df438aeb3ea2cd6e5f97e64> /usr/lib/system/libsystem_c.dylib

Steps & Code to Reproduce

A test project is available at https://github.com/alinradut/realm-has_refs-crash

You need to start the main app, click "Run test", then share something using the sharing extension where you'll tap "Run test" as well.

The sharing extension will soon crash. The fastest way to reproduce it would be to tap "Run test" twice in the sharing extension.

The reason of the crash can vary, most of my users get Assertion failed: has_refs(), but this test usually brings out the abort() called crash, although I believe they are connected.

I've also created an issue a few weeks ago on realm-swift, but I figured I should create it here as well for visibility.

@tgoyne
Copy link
Member

tgoyne commented Nov 3, 2023

This is not a supported scenario, and crashes are expected behavior. From the docs for the readOnly configuration option:

For non-synchronized Realms, this is required to be able to open Realm files which are not writeable or are in a directory which is not writeable. This should only be used on files which will not be modified by anyone while they are open, and not just to get a read-only view of a file which may be written to by another thread or process. Opening in read-only mode requires disabling Realm's reader/writer coordination, so committing a write transaction from another process will result in crashes.

Read-only mode is there to let you read Realm files bundled with your app and not much else.

@tgoyne tgoyne closed this as completed Nov 3, 2023
@alinradut
Copy link
Author

Oof 😳

I've been using Realm for the last 8ish years and this is the first time I've heard this, read only mode was the only way to use an encrypted Realm in an extension.

Is there an alternative?

I see that multiprocess encryption has finally been added, I'll also give that a try.

@alinradut
Copy link
Author

@tgoyne apparently this is still a valid issue.

Using Realm in read-write mode reduces the likelihood of the crash happening, but doesn't prevent it altogether. I have updated the test project at https://github.com/alinradut/realm-has_refs-crash and it is reproducible 100%.

I no longer encountered has_refs, but I am running into abort() called and the console prints
libc++abi: terminating with uncaught exception of type realm::util::DecryptionFailed: Decryption failed: 'unable to decrypt after 0 seconds (retry_count=0, from=i != bytes_read, size=16384)'

Thread 3 Queue : com.apple.root.default-qos (concurrent)
#0	0x00000001bb7cbbbc in __pthread_kill ()
realm/realm-swift#1	0x00000001dc338844 in pthread_kill ()
realm/realm-swift#2	0x000000018b5f86ac in abort ()
realm/realm-swift#3	0x000000019882fde4 in abort_message ()
realm/realm-swift#4	0x000000019882066c in demangling_terminate_handler() ()
realm/realm-swift#5	0x000000019873d908 in _objc_terminate() ()
realm/realm-swift#6	0x000000019882f280 in std::__terminate(void (*)()) ()
realm/realm-swift#7	0x000000019882f228 in std::terminate() ()
realm/realm-swift#8	0x000000010122544c in __clang_call_terminate ()
realm/realm-swift#9	0x0000000101233524 in realm::Allocator::translate_critical(realm::Allocator::RefTranslation*, unsigned long) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/alloc.hpp:570
realm/realm-swift#10	0x00000001012285cc in realm::Allocator::translate(unsigned long) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/alloc.hpp:588
realm/realm-swift#11	0x00000001012ccf8c in realm::ClusterNodeInner::get(unsigned long, realm::ClusterNode::State&) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/cluster_tree.cpp:363
realm/realm-swift#12	0x00000001012d0b48 in realm::ClusterTree::get(unsigned long, realm::ObjKey&) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/cluster_tree.cpp:979
realm/realm-swift#13	0x00000001012d1bc8 in realm::ClusterTree::Iterator::go(unsigned long) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/cluster_tree.cpp:1337
realm/realm-swift#14	0x000000010154e4b4 in realm::Results::IteratorWrapper::get(realm::Table const&, unsigned long) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.cpp:392
realm/realm-swift#15	0x000000010154997c in std::__1::optional<realm::Obj> realm::Results::try_get<realm::Obj>(unsigned long) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.cpp:406
realm/realm-swift#16	0x0000000101549794 in realm::Obj realm::Results::get<realm::Obj>(unsigned long) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.cpp:483
realm/realm-swift#17	0x0000000100e369a4 in RLMAccessorContext auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)::operator()<realm::Obj*>(RLMAccessorContext&) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.hpp:408
realm/realm-swift#18	0x0000000100e363b0 in RLMAccessorContext realm::switch_on_type<realm::Obj, auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)>(realm::PropertyType, auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)&&) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/property.hpp:252
realm/realm-swift#19	0x0000000100e35e20 in RLMAccessorContext realm::Results::dispatch<auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long)::'lambda'(RLMAccessorContext&)>(RLMAccessorContext&) const at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.hpp:401
realm/realm-swift#20	0x0000000100e309b4 in auto realm::Results::get<RLMAccessorContext>(RLMAccessorContext&, unsigned long) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-core/src/realm/object-store/results.hpp:407
realm/realm-swift#21	0x0000000100e307bc in -[RLMFastEnumerator countByEnumeratingWithState:count:] at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-swift/Realm/RLMCollection.mm:151
realm/realm-swift#22	0x0000000100e30b6c in RLMFastEnumerate(NSFastEnumerationState*, unsigned long, id<RLMCollectionPrivate>) at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-swift/Realm/RLMCollection.mm:192
realm/realm-swift#23	0x000000010105a6e4 in -[RLMResults countByEnumeratingWithState:objects:count:] at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-swift/Realm/RLMResults.mm:186
realm/realm-swift#24	0x00000001853205c0 in NSFastEnumerationIterator.next() ()
realm/realm-swift#25	0x00000001011a3354 in RLMIterator.next() at /Users/user/Library/Developer/Xcode/DerivedData/realmcrash-bguqhgawqksoqyadfkutjutwgwix/SourcePackages/checkouts/realm-swift/RealmSwift/RealmCollection.swift:34
realm/realm-swift#26	0x00000001011a3400 in protocol witness for IteratorProtocol.next() in conformance RLMIterator<τ_0_0> ()
realm/realm-swift#27	0x000000018579bfac in Sequence._copySequenceContents(initializing:) ()
realm/realm-swift#28	0x00000001011bf1e4 in protocol witness for Sequence._copyContents(initializing:) in conformance Results<τ_0_0> ()
realm/realm-swift#29	0x00000001856584d0 in _copyCollectionToContiguousArray<τ_0_0>(_:) ()
realm/realm-swift#30	0x0000000185646c58 in Sequence._copyToContiguousArray() ()
realm/realm-swift#31	0x00000001011bf1cc in protocol witness for Sequence._copyToContiguousArray() in conformance Results<τ_0_0> ()
realm/realm-swift#32	0x000000018564e2a4 in Array.init<τ_0_0>(_:) ()
realm/realm-swift#33	0x0000000100dbbebc in closure realm/realm-swift#1 in closure realm/realm-swift#1 in static RealmHelper.startReadTest(onUpdate:) at /Volumes/Projects/OpenSource/test/realmcrash/shared/RealmHelper.swift:77
realm/realm-swift#34	0x0000000100dbc374 in partial apply for closure realm/realm-swift#1 in closure realm/realm-swift#1 in static RealmHelper.startReadTest(onUpdate:) ()
realm/realm-swift#35	0x00000001b2e51bd0 in autoreleasepool<τ_0_0>(invoking:) ()
realm/realm-swift#36	0x0000000100dbbc24 in closure realm/realm-swift#1 in static RealmHelper.startReadTest(onUpdate:) at /Volumes/Projects/OpenSource/test/realmcrash/shared/RealmHelper.swift:74
realm/realm-swift#37	0x0000000100dbb884 in thunk for @escaping @callee_guaranteed @Sendable () -> () ()
realm/realm-swift#38	0x0000000180bcb094 in _dispatch_call_block_and_release ()
realm/realm-swift#39	0x0000000180bcc094 in _dispatch_client_callout ()
realm/realm-swift#40	0x0000000180b6e858 in _dispatch_queue_override_invoke ()
realm/realm-swift#41	0x0000000180b7bb94 in _dispatch_root_queue_drain ()
realm/realm-swift#42	0x0000000180b7c39c in _dispatch_worker_thread2 ()
realm/realm-swift#43	0x00000001dc32cdc4 in _pthread_wqthread ()

The exception seems to be eventually thrown in alloc.hpp:570:

#if REALM_ENABLE_ENCRYPTION
            realm::util::encryption_read_barrier(addr, NodeHeader::header_size, txl.encrypted_mapping,
                                                 NodeHeader::get_byte_size_from_header);
#endif

and the error message is coming from encrypted_file_mapping.cpp:336:

        if (we_are_alone || (retry_count > 0 && elapsed > max_retry_period)) {
            auto str = util::format("unable to decrypt after %1 seconds (retry_count=%2, from=%3, size=%4)",
                                    std::chrono::duration_cast<std::chrono::seconds>(elapsed).count(), retry_count,
                                    debug_from, size);
            // std::cerr << std::endl << "*Timeout: " << str << std::endl;
            throw DecryptionFailed(str);

No precise idea about what the code does, but a naive impression would be that it's checking whether someone else is connected to the database and it doesn't see the main app that's also working on it (maybe because it caught it in between writes), so it assumes its dealing with an incorrect key and throws the error. The key cannot be wrong as it's fixed and 64 a characters.

Maybe a few more retries wouldn't be amiss here?

@alinradut alinradut changed the title Crash when read-only Realm is querying objects while read-write Realm is committing transactions Crash when Realm is querying objects in a process while another process is committing transactions Nov 3, 2023
@alinradut
Copy link
Author

@tgoyne: can this issue be reopened or should I file a new one? 🤔

@kiburtse kiburtse reopened this Nov 8, 2023
@kiburtse
Copy link
Contributor

@alinradut thanks for the reporting this and for the reproducer! I've checked the code you've provided and it looks similar to what we already test in our test/test_lang_bind_helper.cpp tests. So, essentially you keep adding batch of objects in every transaction to the realm in a loop in one process, and query for the count of added objects in the other process, right?

Could you confirm a few thing:

  • this only fails for you on a real device with ios? Or also in a simulator or just desktop run?
  • The stacktrace you see with an DecryptionFailed exception only happens in reader thread and that's the only error you noticed?

Looking at the assertion and its values it should happen only here on the first retry - and according to the code no concurrent write was detected. What does it tell us, @finnschiermer?

@alinradut
Copy link
Author

@kiburtse exactly, that's exactly what I am trying to do, write some batched data in a process while another process tries to pull data from the Realm db (not necessarily connected to the data that is being written).

This fails both on the simulator and on a real device.

I've only noticed this crash in the reader process, I can't say I've seen other errors.

@jedelbo
Copy link
Contributor

jedelbo commented Dec 1, 2023

@kiburtse are you able to reproduce?

@alinradut
Copy link
Author

Just bumping this issue after re-testing it with the latest release, it's still reproducible and present in RealmSwift 10.50.0.

@marinofaggiana
Copy link

Same issue and this is open from 2023 !!

Copy link

sync-by-unito bot commented Oct 1, 2024

➤ jedelbo commented:

[~[email protected]] would you be able to check if this is reproducible? Apparently there is a reproduction test case.

@alinradut
Copy link
Author

Original reporter here.

I just tried the test project with the latest realm-swift (10.54.0) and the crash is still very much happening.

@jedelbo
Copy link
Contributor

jedelbo commented Oct 1, 2024

@tgoyne ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants