realm-swift: Crash on app start

How frequently does the bug occur?

All the time

Description

Since updating from Realm 10.8.1 to 10.28.1 (including recent updates), some users started to receive repeated random crashes in realm, where app crashes as soon as it starts. I have managed to reproduce a few of them by force restarting device, while some Realm write operation was happening. This was leading to corrupt realm database (see crashlog 1) or some corrupt record inside realm database (see crashlog 2). If I try to open database for “crashlog 2” using RealmStudio, it hangs when I try to scroll to record that got corrupted. I also see random information inserted for other records (see screenshot). If I try to access corrupted record imagePath, I get crashlog 3.

I guess, if app gets unexpectedly killed for some reason, there is chance that db becomes corrupted. Is there any good way to detect and recover from such state or prevent it from happening?

Stacktrace & log output

crashlog 1

/Users/realm/workspace/realm_realm-core_release_12.9.0/src/realm/table.cpp:913: [realm-core-12.9.0] Assertion failed: has_search_index(col_key)

Thread 0 Crashed:
0   libsystem_kernel.dylib        	       0x1fe262200 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x20e49f1ac pthread_kill + 268
2   libsystem_c.dylib             	       0x1c9059ca0 abort + 180
3   Realm                         	       0x1077f2c04 please_report_this_issue_in_github_realm_realm_core_v_12_9_0 + 12
4   Realm                         	       0x1077f2eec realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 260
5   Realm                         	       0x1077f2d4c realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 328
6   Realm                         	       0x1077a2ee4 realm::Table::add_search_index(realm::ColKey) + 396
7   Realm                         	       0x10790bb58 realm::ObjectStore::apply_schema_changes(realm::Transaction&, unsigned long long, realm::Schema&, unsigned long long, realm::SchemaMode, std::__1::vector<realm::SchemaChange, std::__1::allocator<realm::SchemaChange> > const&, bool, std::__1::function<void ()>) + 2004
8   Realm                         	       0x10796abc0 realm::Realm::update_schema(realm::Schema, unsigned long long, std::__1::function<void (std::__1::shared_ptr<realm::Realm>, std::__1::shared_ptr<realm::Realm>, realm::Schema&)>, std::__1::function<void (std::__1::shared_ptr<realm::Realm>)>, bool) + 400
9   Realm                         	       0x107642ba0 +[RLMRealm realmWithConfiguration:queue:error:] + 1824
10  RealmSwift                    	       0x106ecd228 @nonobjc RLMRealm.__allocating_init(configuration:queue:) + 72
11  RealmSwift                    	       0x106ecd6a4 Realm.init(queue:) + 124
12  TestApp                      	       0x104f44cf8 specialized RealmManager.testRealm(failedBlock:passedBlock:) + 888056 (RealmManager.swift:157)
13  TestApp                      	       0x104f43e44 RealmManager.setup() + 884292 (<compiler-generated>:0)
14  TestApp                      	       0x10512b18c specialized AppDelegate.setupThirdParty() + 2879884 (AppDelegate.swift:146)
15  TestApp                      	       0x10512b318 specialized AppDelegate.application(_:didFinishLaunchingWithOptions:) + 2880280 (AppDelegate.swift:27)
16  TestApp                      	       0x10512ad64 @objc AppDelegate.application(_:didFinishLaunchingWithOptions:) + 2878820 (<compiler-generated>:24)
17  UIKitCore                     	       0x1c4058f40 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 300
18  UIKitCore                     	       0x1c4058664 -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 2848
19  UIKitCore                     	       0x1c4057640 -[UIApplication _runWithMainScene:transitionContext:completion:] + 856
20  UIKitCore                     	       0x1c405728c -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 132
21  UIKitCore                     	       0x1c3d9cd64 _UIScenePerformActionsWithLifecycleActionMask + 108
22  UIKitCore                     	       0x1c40dec3c __101-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]_block_invoke + 216
23  UIKitCore                     	       0x1c3f8f888 -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 220
24  UIKitCore                     	       0x1c3f8f6b0 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 620
25  UIKitCore                     	       0x1c3f8f260 -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 252
26  UIKitCore                     	       0x1c3f8f12c __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 148
27  UIKitCore                     	       0x1c466d5a0 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:fromCurrentState:actions:completion:] + 736
28  UIKitCore                     	       0x1c470596c _UISceneSettingsDiffActionPerformChangesWithTransitionContextAndCompletion + 224
29  UIKitCore                     	       0x1c3e3b2d8 -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 316
30  UIKitCore                     	       0x1c42ad048 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke.215 + 556
31  UIKitCore                     	       0x1c3f0dbec -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 216
32  UIKitCore                     	       0x1c3f0da5c -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 244
33  UIKitCore                     	       0x1c3f0cfb0 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 520
34  UIKitCore                     	       0x1c3f0cd3c -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 288
35  FrontBoardServices            	       0x1d74e5d48 -[FBSScene _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 344
36  FrontBoardServices            	       0x1d7525104 __92-[FBSWorkspaceScenesClient createSceneWithIdentity:parameters:transitionContext:completion:]_block_invoke.78 + 120
37  FrontBoardServices            	       0x1d74e9ae4 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168
38  FrontBoardServices            	       0x1d7524d3c __92-[FBSWorkspaceScenesClient createSceneWithIdentity:parameters:transitionContext:completion:]_block_invoke + 360
39  libdispatch.dylib             	       0x1c8ff5fdc _dispatch_client_callout + 20
40  libdispatch.dylib             	       0x1c8ff9a5c _dispatch_block_invoke_direct + 264
41  FrontBoardServices            	       0x1d74f3f2c __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52
42  FrontBoardServices            	       0x1d74f3ac8 -[FBSSerialQueue _targetQueue_performNextIfPossible] + 220
43  FrontBoardServices            	       0x1d74f62a8 -[FBSSerialQueue _performNextFromRunLoopSource] + 28
44  CoreFoundation                	       0x1c1c4622c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
45  CoreFoundation                	       0x1c1c52614 __CFRunLoopDoSource0 + 176
46  CoreFoundation                	       0x1c1bd651c __CFRunLoopDoSources0 + 244
47  CoreFoundation                	       0x1c1bebeb8 __CFRunLoopRun + 836
48  CoreFoundation                	       0x1c1bf11e4 CFRunLoopRunSpecific + 612
49  GraphicsServices              	       0x1faa11368 GSEventRunModal + 164
50  UIKitCore                     	       0x1c40a0d88 -[UIApplication _run] + 888
51  UIKitCore                     	       0x1c40a09ec UIApplicationMain + 340
52  TestApp                      	       0x104e73ebc main + 32444 (<compiler-generated>:0)
53  dyld                          	       0x1dff15948 start + 2504




Crashlog 2

*** Terminating app due to uncaught exception 'RLMException', reason: 'No object with key '24222' in 'class_RMImageTask''

Last Exception Backtrace:
0   CoreFoundation                	       0x194b0a248 __exceptionPreprocess + 164
1   libobjc.A.dylib               	       0x18ded7a68 objc_exception_throw + 60
2   Realm                         	       0x107a68f20 RLMThrowResultsError(NSString*) + 852
3   Realm                         	       0x107a69570 auto translateRLMResultsErrors<-[RLMResults count]::$_1>(-[RLMResults count]::$_1&&, NSString*) + 96
4   Realm                         	       0x107a69504 -[RLMResults count] + 40
5   RealmSwift                    	       0x106a82aa4 RealmCollectionImpl.count.getter + 172


Crashlog 3

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0x1ec092740'

Last Exception Backtrace:
0   CoreFoundation                	       0x194b0a248 __exceptionPreprocess + 164
1   libobjc.A.dylib               	       0x18ded7a68 objc_exception_throw + 60
2   CoreFoundation                	       0x194c7e3f0 +[NSObject(NSObject) _copyDescription] + 0
3   CoreFoundation                	       0x194b20360 ___forwarding___ + 1592
4   CoreFoundation                	       0x194b88660 _CF_forwarding_prep_0 + 96
5   libswiftCore.dylib            	       0x18ebc53e4 specialized _withCocoaASCIIPointer<A>(_:requireStableAddress:work:) + 60
6   libswiftCore.dylib            	       0x18ebc5598 _bridgeCocoaString(_:) + 152
7   libswiftCore.dylib            	       0x18ebc5774 String.init(_cocoaString:) + 28
8   Foundation                    	       0x18f1eab30 static String._unconditionallyBridgeFromObjectiveC(_:) + 172

Can you reproduce the bug?

Yes, sometimes

Reproduction Steps

  • start app with multiple realm write operations
  • force restart device
  • start app again. try to load realm database and read / update records

Version

starting from 10.28.1 to 10.32.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 15, iOS 16

Build environment

ProductName:	macOS
ProductVersion:	12.6
BuildVersion:	21G115

/Applications/Xcode.app/Contents/Developer
Xcode 14.0
Build version 14A309

/usr/local/bin/pod
1.11.3
Realm (10.32.0)
RealmSwift (10.32.0)
RealmSwift (= 10.32.0)

/bin/bash
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)

carthage not found
(not in use here)

/usr/bin/git
git version 2.37.0 (Apple Git-136)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16

Most upvoted comments

I guess is version 10.33.0?

@sipersso I think we are close to having found the root cause for these issues.Hopefully we will have a fix ready soon.