realm-swift: Fatal Exception: realm::KeyNotFound in Realm notification listener thread

How frequently does the bug occur?

Sometimes

Description

Since version 10.22.0, but it happens in 10.24.0 too, we started to see this crash for some users. It happens in random places.

For example:

Crashed: Realm notification listener
Fatal Exception: realm::KeyNotFound No object with key '99' in 'class_[OBJECT]'`

There’s no stack trace for this crash.

The stracktrace below is what’s happening on main queue, but like I said, it happens randomly… when we insert something or read something.

Stacktrace & log output

com.apple.main-thread
0   libsystem_kernel.dylib         0x2640 __psynch_mutexwait + 8
1   libsystem_pthread.dylib        0x23dc _pthread_mutex_firstfit_lock_wait + 84
2   libsystem_pthread.dylib        0x952c _pthread_mutex_firstfit_lock_slow + 248
3   libc++.1.dylib                 0xf15c std::__1::mutex::lock() + 16
4   Realm                          0x3957a4 realm::_impl::RealmCoordinator::register_notifier(std::__1::shared_ptr<realm::_impl::CollectionNotifier>) + 56
5   Realm                          0x3d2488 realm::Results::prepare_async(realm::util::TaggedBool<realm::ForCallback>) + 640
6   Realm                          0x3c94a0 realm::Results::ensure_up_to_date(realm::Results::EvaluateMode) + 416
7   Realm                          0x3c8dc0 realm::util::Optional<realm::Obj> realm::Results::try_get<realm::Obj>(unsigned long) + 48
8   Realm                          0x3c9174 realm::util::Optional<realm::Obj> realm::Results::first<realm::Obj>() + 68
9   Realm                          0x12ffe0 RLMAccessorContext realm::Results::dispatch<auto realm::Results::first<RLMAccessorContext>(RLMAccessorContext&)::'lambda'(RLMAccessorContext&)>(RLMAccessorContext&) const + 412
10  Realm                          0x12fe30 auto realm::Results::first<RLMAccessorContext>(RLMAccessorContext&) + 24
11  Realm                          0x12dbe8 -[RLMResults firstObject] + 48

Can you reproduce the bug?

Not yet

Reproduction Steps

We wren’t able to reproduce and few users are affected, but it happens repetitively for them.

Version

10.24.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.3.1

Build environment

Xcode version: Xcide 13.2.1 Dependency manager and version: Carthage

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (2 by maintainers)

Most upvoted comments

@bodnar-dan can you update to our latest version 10.35.0, there are some fixes related to this in core, which may fix this issue.

This issue may be caused by a file corruption that without access to a realm file it is really hard to diagnose. There is a fix which will be available in a future release which may fix this corruption issue realm/realm-core#5993 Be aware that the fix will not fix already corrupted files, but hopefully prevent creating corrupted files in the future…

Thanks! Reading https://github.com/realm/realm-swift/issues/8000#issuecomment-1337012692, it sounds like this fix is included in v10.33.0, will update to that for our next release.