Realm-cocoa: Terminating app due to uncaught exception 'RLMException', reason: 'ConstIterator copy failed'

Created on 23 Jun 2020  路  9Comments  路  Source: realm/realm-cocoa

Goals

Use Realm to perform basic database operations

Expected Results

App should not encounter any Realm-related crashes

Actual Results

App crashes with the following log:
Terminating app due to uncaught exception 'RLMException', reason: 'ConstIterator copy failed'

Steps to Reproduce

The issue is intermittent. Crash happens when all objects are deleted using

try realm.write {
   realm.deleteAll()
}

and try to perform operations like realm.add(objects, update: update).

Or could be possible it is due to copying the realm object using:

extension Object: DetachableObject {
  func detached() -> Self {
    let detached = type(of: self).init()
    for property in objectSchema.properties {
      guard let value = value(forKey: property.name) else {
        continue
      }
      if let detachable = value as? DetachableObject {
        detached.setValue(detachable.detached(), forKey: property.name)
      } else { // Then it is a primitive
        detached.setValue(value, forKey: property.name)
      }
    }
    return detached
  }
}

Version of Realm and Tooling

ProductName: Mac OS X
ProductVersion: 10.15.5
BuildVersion: 19F101

Xcode version: 11.5
Build version: 11E608c

Cocoapods version: 1.9.3

Realm version: 5.0.2
RealmSwift version: 5.0.2

O-Community

All 9 comments

@finnschiermer Do you have any clue what would cause this? realm.deleteAll() is just a call to Table::clear() on each table.

Hello everybody,

Reproducing the issue randomly here ! Anyone with an idea on how correct or work around this issue ?

Steps to Reproduce

I init Realm with :

Realm.Configuration.defaultConfiguration = Realm.Configuration(
    schemaVersion: RealmMigrations.currentVersion,
    migrationBlock: { migration, oldSchemaVersion in
        // NOT RELEVANT MIGRATION STUFF HERE
})

And crash happens here :

try? realm.write {
   realm.delete(validGroceries)
}

The real is shared on main thread by all the classes needing to do DB actions.

Version of Realm and Tooling

ProductName: iOS
ProductVersion: 13.6 (Also on iOS 12)
BuildVersion: 17G68

Xcode version: 11.3
Build version: 11C29

Cocoapods version: 1.9.3

RealmSwift version: =10.0.0-beta.2

@micheau-bastien Can you share a stack trace, or even better send us a sample app that reproduces this crash? Thanks!

@leemaguire : Thank you very much for your great reactivity here! Here are some more debug informations :

Error message :

2020-08-11 12:44:01.020347+0200 Meal[1419:289765] *** Terminating app due to uncaught exception 'RLMException', reason: 'ConstIterator copy failed'
*** First throw call stack:
(0x1a688dea8 0x1a65b0f48 0x105b7c00c 0x105b80354 0x105b802c4 0x1059ecab0 0x105b7cb00 0x1e2cd30a8 0x10780b818 0x10780bd8c 0x1b5c16e48 0x104a332a4 0x1049c38cc 0x107827b38 0x1078158f0 0x107815f98 0x1077cfbf8 0x1077cfd14 0x1059f9294 0x1059f8e88 0x1058568dc 0x105856838 0x105832f48 0x105832eb8 0x105834078 0x105cb4f60 0x105cb4b98 0x1058fd410 0x105bf2ec0 0x105b5a244 0x105b5a20c 0x1078043ac 0x107803f90 0x104982ec8 0x10499edb0 0x1048c970c 0x104a4c928 0x1049746d4 0x104b772a4 0x107c57db8 0x107c595fc 0x107c67a6c 0x1a680d80c 0x1a6807cc4 0x1a6806ddc 0x1b2528598 0x1aafc8f70 0x1aafce544 0x104a39318 0x1a669c5c4)
libc++abi.dylib: terminating with uncaught exception of type NSException

And the matching stack trace :

(lldb) po Thread.callStackSymbols.forEach{print($0)}
0   ???                                 0x00000001103dc804 0x0 + 4567451652
1   Meal                                0x0000000104a392cc main + 0
2   libsystem_c.dylib                   0x00000001a64e3a30 727925A3-9C8F-38CC-8433-ED48FF7FCA1E + 473648
3   libsystem_c.dylib                   0x00000001a64e39c0 727925A3-9C8F-38CC-8433-ED48FF7FCA1E + 473536
4   libc++abi.dylib                     0x00000001a6664cc8 6D43770A-498B-3E93-82B5-82FAEC483796 + 77000
5   libc++abi.dylib                     0x00000001a6656ca0 6D43770A-498B-3E93-82B5-82FAEC483796 + 19616
6   libobjc.A.dylib                     0x00000001a65b11f8 737FD936-3292-3701-91A0-29F0235E4C66 + 25080
7   libc++abi.dylib                     0x00000001a6664154 6D43770A-498B-3E93-82B5-82FAEC483796 + 74068
8   libc++abi.dylib                     0x00000001a66640ec _ZSt9terminatev + 44
9   libdispatch.dylib                   0x0000000107c59610 _dispatch_client_callout + 36
10  libdispatch.dylib                   0x0000000107c67a6c _dispatch_main_queue_callback_4CF + 972
11  CoreFoundation                      0x00000001a680d80c F316EAE2-3DAB-368C-831E-B294E3A51E9A + 628748
12  CoreFoundation                      0x00000001a6807cc4 F316EAE2-3DAB-368C-831E-B294E3A51E9A + 605380
13  CoreFoundation                      0x00000001a6806ddc CFRunLoopRunSpecific + 572
14  GraphicsServices                    0x00000001b2528598 GSEventRunModal + 160
15  UIKitCore                           0x00000001aafc8f70 B7BE7F60-4955-39B4-9192-BBF83C82B17E + 11612016
16  UIKitCore                           0x00000001aafce544 UIApplicationMain + 164
17  Meal                                0x0000000104a39318 main + 76
18  libdyld.dylib                       0x00000001a669c5c4 B717ED30-FFAC-3945-B3F8-12308550996D + 5572

Is it helping you ?

@micheau-bastien this stack trace doesn't really help us, would you have a stack trace with realm symbols that lead up to the crash? Thanks

@micheau-bastien Is this a single-threaded scenario or are there other threads working concurrently?

I think I've identified the cause of this bug: ObjectStore assumes that constiterators can be copied, even if they have become invalidated. Core does not support this, as it tries to validate during copy and throws if validation fails. Possible fix: postpone validation to point of use.

Assuming this is correct, a fix should be part of Core release 6.0.17

@finnschiermer Thank you for your response and your help!
This is a single thread scenario (on main thread).
And I have a quite large amount of invalidated objects moving around so it might indeed be the cause of the issue here!
Thanks again for helping me out here, you made my day!
I鈥檒l keep you updated on this thread as soon as I鈥檒l be able to test on 6.0.17!

Fix in progress, but I missed the 6.0.17 train, so I expect it to go into 6.0.18 instead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

duribreux picture duribreux  路  3Comments

dennisgec picture dennisgec  路  3Comments

TheHmmka picture TheHmmka  路  3Comments

jpsim picture jpsim  路  3Comments

fadylateef picture fadylateef  路  3Comments