realm / realm-swift

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

assertion failure in Table::add_search_index() #7795

Open thegreenline opened 2 years ago

thegreenline commented 2 years ago

How frequently does the bug occur?

Sometimes

Description

App crashes sometime in production since I updated Realm to be compatible with the new version of Realm Studio. The problem is that the app crashes instead of going into the block... I can't reproduce on my devices...

Stacktrace & log output

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001ef034b78 __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000002286503bc pthread_kill + 268 (pthread.c:1668)
2   libsystem_c.dylib               0x00000001c280044c __abort + 128 (abort.c:155)
3   libsystem_c.dylib               0x00000001c27a8528 abort + 180 (abort.c:126)
4   Realm                           0x0000000101915fc0 please_report_this_issue_in_github_realm_realm_core + 12
5   Realm                           0x0000000101916288 realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 260
6   Realm                           0x0000000101916448 realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 328
7   Realm                           0x00000001018cdc90 realm::Table::add_search_index(realm::ColKey) + 396
8   Realm                           0x0000000101a0a5dc 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<re... + 3392
9   Realm                           0x0000000101a5bcac 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<... + 368
10  Realm                           0x0000000101748448 0x101628000 + 1180744 (RLMRealm.mm:560)
11  RealmSwift                      0x0000000101d6b324 init + 40 (<compiler-generated>:0)
12  RealmSwift                      0x0000000101d6b324 init + 40 (<compiler-generated>:0)
13  RealmSwift                      0x0000000101d6b324 Realm.init(queue:) + 104 (Realm.swift:76)
14  QRCode Gen                      0x000000010035ca08 getRealmInstance + 12 (Realm+Extension.swift:30)
15  QRCode Gen                      0x000000010035ca08 QRTabBarController.viewDidLoad() + 88 (QRTabBarController.swift:33)
16  QRCode Gen                      0x000000010035d5d8 @objc QRTabBarController.viewDidLoad() + 28 (<compiler-generated>:0)
17  UIKitCore                       0x00000001b9c486c4 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 108 (UIViewController.m:2732)
18  UIKitCore                       0x00000001b9c4b214 -[UIViewController loadViewIfRequired] + 1048 (UIViewController.m:4032)
19  UIKitCore                       0x00000001b9c1e274 -[UIViewController view] + 32 (UIViewController.m:4054)
20  UIKitCore                       0x00000001b9d60450 -[UIWindow addRootViewControllerViewIfPossible] + 180 (UIWindow.m:2128)
21  UIKitCore                       0x00000001b9d6d248 -[UIWindow setRootViewController:] + 600 (UIWindow.m:2404)
22  QRCode Gen                      0x00000001004d484c closure #1 in SplashScreenViewController.startFetchs() + 188 (SplashScreenViewController.swift:103)
23  QRCode Gen                      0x0000000100519af8 thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:0)
24  libdispatch.dylib               0x00000001b732ae68 _dispatch_call_block_and_release + 32 (init.c:1517)
25  libdispatch.dylib               0x00000001b732ca2c _dispatch_client_callout + 20 (object.m:560)
26  libdispatch.dylib               0x00000001b733af48 _dispatch_main_queue_drain + 928 (inline_internal.h:2622)
27  libdispatch.dylib               0x00000001b733ab98 _dispatch_main_queue_callback_4CF + 44 (queue.c:7770)
28  CoreFoundation                  0x00000001b767e2f0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1795)
29  CoreFoundation                  0x00000001b76381f4 __CFRunLoopRun + 2532 (CFRunLoop.c:3144)
30  CoreFoundation                  0x00000001b764b6b8 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
31  GraphicsServices                0x00000001d36e5374 GSEventRunModal + 164 (GSEvent.c:2200)
32  UIKitCore                       0x00000001b9fb0e88 -[UIApplication _run] + 1100 (UIApplication.m:3511)
33  UIKitCore                       0x00000001b9d325ec UIApplicationMain + 364 (UIApplication.m:5064)
34  QRCode Gen                      0x00000001002fafb8 main + 68 (ContentCreatorProtocol.swift:16)
35  dyld                            0x00000001014b9ce4 start + 520 (dyldMain.cpp:879)

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

Thread 2:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 4 name:
Thread 4:
0   libsystem_kernel.dylib          0x00000001ef02e4e0 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001ef02eb24 mach_msg + 76 (mach_msg.c:119)
2   CoreFoundation                  0x00000001b7633820 __CFRunLoopServiceMachPort + 372 (CFRunLoop.c:2646)
3   CoreFoundation                  0x00000001b7637cac __CFRunLoopRun + 1180 (CFRunLoop.c:3000)
4   CoreFoundation                  0x00000001b764b6b8 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
5   Foundation                      0x00000001b8e32414 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 236 (NSRunLoop.m:373)
6   Foundation                      0x00000001b8e73cac -[NSRunLoop(NSRunLoop) runUntilDate:] + 92 (NSRunLoop.m:420)
7   UIKitCore                       0x00000001b9f2afb0 -[UIEventFetcher threadMain] + 524 (UIEventFetcher.m:1167)
8   Foundation                      0x00000001b8e8255c __NSThread__start__ + 808 (NSThread.m:972)
9   libsystem_pthread.dylib         0x000000022864a9ac _pthread_start + 148 (pthread.c:891)
10  libsystem_pthread.dylib         0x0000000228649e68 thread_start + 8

Thread 5:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

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

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

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

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

Thread 10:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 11 name:
Thread 11:
0   libsystem_kernel.dylib          0x00000001ef02e4e0 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001ef02eb24 mach_msg + 76 (mach_msg.c:119)
2   QRCode Gen                      0x0000000100a1cdcc 0x1002f4000 + 7507404
3   libsystem_pthread.dylib         0x000000022864a9ac _pthread_start + 148 (pthread.c:891)
4   libsystem_pthread.dylib         0x0000000228649e68 thread_start + 8

Thread 12:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 13:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 14:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 15:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 16:
0   libsystem_pthread.dylib         0x0000000228649e54 start_wqthread + 0

Thread 17 name:
Thread 17:
0   libsystem_kernel.dylib          0x00000001ef02ef64 __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x0000000228651298 _pthread_cond_wait + 1236 (pthread_cond.c:636)
2   JavaScriptCore                  0x00000001c3aae4d4 scavenger_thread_main + 1232 (pas_scavenger.c:131)
3   libsystem_pthread.dylib         0x000000022864a9ac _pthread_start + 148 (pthread.c:891)
4   libsystem_pthread.dylib         0x0000000228649e68 thread_start + 8

Thread 18 name:
Thread 18:
0   libsystem_kernel.dylib          0x00000001ef02e4e0 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001ef02eb24 mach_msg + 76 (mach_msg.c:119)
2   CoreFoundation                  0x00000001b7633820 __CFRunLoopServiceMachPort + 372 (CFRunLoop.c:2646)
3   CoreFoundation                  0x00000001b7637cac __CFRunLoopRun + 1180 (CFRunLoop.c:3000)
4   CoreFoundation                  0x00000001b764b6b8 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
5   CFNetwork                       0x00000001b80c14d8 +[__CFN_CoreSchedulingSetRunnable _run:] + 472 (CoreSchedulingSet.mm:1372)
6   Foundation                      0x00000001b8e8255c __NSThread__start__ + 808 (NSThread.m:972)
7   libsystem_pthread.dylib         0x000000022864a9ac _pthread_start + 148 (pthread.c:891)
8   libsystem_pthread.dylib         0x0000000228649e68 thread_start + 8

Thread 19 name:
Thread 19:
0   libsystem_kernel.dylib          0x00000001ef02fe58 kevent + 8
1   Realm                           0x00000001019c1dd0 realm::_impl::ExternalCommitHelper::listen() + 156
2   Realm                           0x00000001019c1f20 0x101628000 + 3776288
3   libsystem_pthread.dylib         0x000000022864a9ac _pthread_start + 148 (pthread.c:891)
4   libsystem_pthread.dylib         0x0000000228649e68 thread_start + 8

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x0000000000989680   x6: 0x0000000000000000   x7: 0x0000000000000000
    x8: 0x33ea73aa07005d76   x9: 0x33ea73ab065098f6  x10: 0x00000000000003e8  x11: 0x0000000000000a00
   x12: 0x0000000000000004  x13: 0x0000000103800000  x14: 0x00000002455443e2  x15: 0x0000000100000000
   x16: 0x0000000000000148  x17: 0x000000010150c580  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000000103  x21: 0x000000010150c660  x22: 0x0000000101b737d4  x23: 0x000000016fb09cb0
   x24: 0x000000016fb09cb0  x25: 0x0000000101b735ca  x26: 0x0000000101ba8de8  x27: 0x0000000101ba8ff0
   x28: 0x0000000283c1eea8   fp: 0x000000016fb09bc0   lr: 0x00000002286503bc
    sp: 0x000000016fb09ba0   pc: 0x00000001ef034b78 cpsr: 0x40000000
   esr: 0x56000080  Address size fault

Binary Images:
0x1002f4000 - 0x100f0bfff QRCode Gen arm64  <4d44da0b48f637c09aca4eb2e05766a9> /private/var/containers/Bundle/Application/E780DDAC-3CBD-4CC6-98DC-BA01F5C62ACE/QRCode Gen.app/QRCode Gen
0x1014a0000 - 0x1014f7fff dyld arm64e  <d16b72c0d6193e4892622c10a1d3066f> /usr/lib/dyld
0x101628000 - 0x101ba3fff Realm arm64  <d8cca7fe7daa387e9acd292aeb5f4b9a> /private/var/containers/Bundle/Application/E780DDAC-3CBD-4CC6-98DC-BA01F5C62ACE/QRCode Gen.app/Frameworks/Realm.framework/Realm
0x101d00000 - 0x101dcbfff RealmSwift arm64  <d215c11d4c2639879fa1dc0c867b0b02> /private/var/containers/Bundle/Application/E780DDAC-3CBD-4CC6-98DC-BA01F5C62ACE/QRCode Gen.app/Frameworks/RealmSwift.framework/RealmSwift
0x1b7329000 - 0x1b736efff libdispatch.dylib arm64e  <69dac9c7ddaf318eb068be596d142a2f> /usr/lib/system/libdispatch.dylib
0x1b762c000 - 0x1b7a81fff CoreFoundation arm64e  <d2716f2f688b3d51ba8b17e6553b209e> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
0x1b7e4a000 - 0x1b830afff CFNetwork arm64e  <eae64611a84036fea69d5658f1bcf4d0> /System/Library/Frameworks/CFNetwork.framework/CFNetwork
0x1b8e1a000 - 0x1b9122fff Foundation arm64e  <feeef9b1b0e63561adfa3ef0302d23f8> /System/Library/Frameworks/Foundation.framework/Foundation
0x1b9a9d000 - 0x1bb337fff UIKitCore arm64e  <e6e89bb5b5863725bf8f4c85dcd443bd> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
0x1c2788000 - 0x1c2807fff libsystem_c.dylib arm64e  <b0793d31ed1b3549b86192a10841bf68> /usr/lib/system/libsystem_c.dylib
0x1c2860000 - 0x1c3bd5fff JavaScriptCore arm64e  <6592b928ee2c3bc2b4e94cdc77022fe0> /System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore
0x1d36e4000 - 0x1d36ecfff GraphicsServices arm64e  <75a910dc87a43b3a8c0ea868b945f5d4> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
0x1ef02d000 - 0x1ef062fff libsystem_kernel.dylib arm64e  <c2fcc05874803c14b4790064a3f843e3> /usr/lib/system/libsystem_kernel.dylib
0x228649000 - 0x228654fff libsystem_pthread.dylib arm64e  <b48efcbed4f7308ba3fa172e0ea78e33> /usr/lib/system/libsystem_pthread.dylib

EOF

Can you reproduce the bug?

Not yet

Reproduction Steps

No response

Version

10.25.2

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 13 min target, but all crash on iOS 15.x

Build environment

Xcode version: 13.3.1 Dependency manager and version: Cocoapods 1.11.2

ejm01 commented 2 years ago

Are you bundling the realms on device before being opened? I'm wondering because there appears to be a schema change needed. Perhaps the schemas got mismatched between the two and the schema change isn't being handled correctly?

thegreenline commented 2 years ago

I also think it's a schema problem, but I did the migration from v1 of the shema with separate "if" and not "if else if". For the transition to the new version I also updated the IDs of the objects that seemed not to work. But I don't understand because all the versions are managed correctly and I never had such problems (no passage in the catch block)

ejm01 commented 2 years ago

I don't think I understand either, but it doesn't seem like you're doing anything incorrectly. Are you able to provide a .zip with a reproduction? It's hard to resolve this without more information.

thegreenline commented 2 years ago

The problem is that I can't reproduce the crashes either with the Testflight versions or with the local versions. I just have crash feedbacks without any other information than this kind of stuff

Capture d’écran 2022-05-21 à 12 37 28 Capture d’écran 2022-05-21 à 12 37 23 Capture d’écran 2022-05-21 à 12 37 09

If I open the stack trace from Xcode Organizer crash report, I can found the exception, but nothing more Capture d’écran 2022-05-21 à 12 42 06

The migration is done directly in the AppDelegate(didFinishLaunchingWithOptions) before anything else.

My migration blocks are like

if oldSchemaVersion < 15 {
do migration
  }
  if oldSchemaVersion < 16 {
do migration
 }
 if oldSchemaVersion < 17 {
do migration
 }

It's quite possible that it's a migration problem, but I don't even have an error in the catch block of the try Realm...

thegreenline commented 2 years ago

If have a crash trace with "more infos"

Capture d’écran 2022-05-25 à 07 37 56

Since upgrading from 10.5 to 10.25, my migration block does only

 if oldSchemaVersion < 17 {
    newObject!["id"] = UUID().uuidString
 }

did I miss something ?

(This is still the trace I get from a crash in production via Xcode organizer, still no local reproduction) It seems that crashes occur when the user has many objects in the database

pavel-ship-it commented 2 years ago

Hi @thegreenline , Are you adding the primary key field to the schema?

thegreenline commented 2 years ago

I have set the primaryKey only in the Realm object with

  override static func primaryKey() -> String? {
    return "id"
  }
pavel-ship-it commented 2 years ago

Looks like I have a reproduction for that issue.

Just to confirm. newObject!["id"] = UUID().uuidString In this line the newObject is getting the value for newly added primary key field? Am I rignt?

thegreenline commented 2 years ago

newObject get a new ID UUID().uuidString return a string UUID. I’m not getting the old object id, but I dont remember why maybe: cant get old obejct ID, or just cant apply a empty migration to update Realm from 10.4 to 10.25 (10.4 format not compatible with current version of Realm studio). But my ID had alway been a non optionnal String with a UUID()uuidString as value.

The Id is only used to avoid duplicates, it has no other impact on the data

nbapps commented 2 years ago

Hi

I have new error for this crash from crashlitics, realm v 10.28.3

Plantage : com.apple.main-thread EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000000

Crashed: com.apple.main-thread 0 libsystem_pthread.dylib 0x7744 + 102 1 libc++.1.dylib 0x3caf4 std::1::mutex::lock() + 16 2 Realm 0x133c8c RLMGetThreadLocalCachedRealmForPath(std::1::basic_string<char, std::__1::char_traits, std::1::allocator > const&, void*) + 59 (RLMRealmUtil.mm:59) 3 Realm 0x12b2ac +[RLMRealm realmWithConfiguration:queue:error:] + 430 (RLMRealm.mm:430) 4 RealmSwift 0x6e1d8 Realm.init(queue:) + 4326334936 (:4326334936) 5 QRCode Gen 0xa1354 viewDidLoad + 30 (Realm+Extension.swift:30) 6 QRCode Gen 0xa1fa4 viewDidLoad + 4306952100 (:4306952100) 7 UIKitCore 0x49d58c -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 108 8 UIKitCore 0x4a1db4 -[UIViewController loadViewIfRequired] + 956 9 UIKitCore 0x4a2178 -[UIViewController view] + 32 10 UIKitCore 0xc0bc10 -[UIWindow addRootViewControllerViewIfPossible] + 180 11 UIKitCore 0xc0cf28 -[UIWindow setRootViewController:] + 632 12 QRCode Gen 0x2d847c + 112 (SplashScreenViewController.swift:112) 13 QRCode Gen 0x367e84 + 4309859972 (:4309859972) 14 libdispatch.dylib 0x2a84 _dispatch_call_block_and_release + 32 15 libdispatch.dylib 0x481c _dispatch_client_callout + 20 16 libdispatch.dylib 0x12c70 _dispatch_main_queue_callback_4CF + 884 17 CoreFoundation 0xa3a2c CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 16 18 CoreFoundation 0x9d904 CFRunLoopRun + 2524 19 CoreFoundation 0x9c9f4 CFRunLoopRunSpecific + 600 20 GraphicsServices 0x3734 GSEventRunModal + 164 21 UIKitCore 0xbca75c -[UIApplication _run] + 1072 22 UIKitCore 0xbcffcc UIApplicationMain + 168 23 QRCode Gen 0x6c78 main + 19 (ContentCreatorProtocol.swift:19) 24 libdyld.dylib 0x1cf8 start + 4

nbapps commented 2 years ago

And from Xcode, I have this 2

Capture d’écran 2022-08-04 à 18 38 59 Capture d’écran 2022-08-04 à 18 39 18