realm / realm-swift

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

Strange rare crash during array of objects dealloc #7021

Closed anton-plebanovich closed 3 years ago

anton-plebanovich commented 3 years ago

Steps for others to Reproduce

This one is rare to reproduce, I just got a report in the Crashlytics and in the console once. Basically, objects of some type were requested, some properties were read and then it crashed during objects dealloc.

From Crashlytics:

SIGABRT ABORT 0x00000001c72b398c

0  libsystem_kernel.dylib         0x1c72b398c __pthread_kill + 8
1  libsystem_pthread.dylib        0x1e28dbc74 pthread_kill + 272
2  libsystem_c.dylib              0x1a4484bb4 abort + 104
3  Realm                          0x103d10a4c please_report_this_error_to_help_at_realm_dot_io + 10
4  Realm                          0x103d10d18 realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 264
5  Realm                          0x103d10e8c realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 324
6  Realm                          0x103d116f8 realm::util::Mutex::destroy_failed(int) + 82
7  Realm                          0x103cc3350 realm::Table::~Table() + 620
8  Realm                          0x103a23b94 realm::List::~List() + 116
9  libobjc.A.dylib                0x1af3fb324 object_cxxDestructFromClass(objc_object*, objc_class*) + 112
10 libobjc.A.dylib                0x1af411d38 objc_destructInstance + 80
11 libobjc.A.dylib                0x1af41899c _objc_rootDealloc + 68
12 Realm                          0x103a721b0 -[RLMListBase .cxx_destruct] + 28
13 libobjc.A.dylib                0x1af3fb324 object_cxxDestructFromClass(objc_object*, objc_class*) + 112
14 libobjc.A.dylib                0x1af411d38 objc_destructInstance + 80
15 libobjc.A.dylib                0x1af41899c _objc_rootDealloc + 68
16 libobjc.A.dylib                0x1af3fb324 object_cxxDestructFromClass(objc_object*, objc_class*) + 112
17 libobjc.A.dylib                0x1af411d38 objc_destructInstance + 80
18 libobjc.A.dylib                0x1af41899c _objc_rootDealloc + 68
19 Realm                          0x103a7c9c0 -[RLMObjectBase dealloc] + 68
20 libswiftCore.dylib             0x19f1d680c swift_arrayDestroy + 128
21 libswiftCore.dylib             0x19eeff81c _ContiguousArrayStorage.__deallocating_deinit + 84
22 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
23 RxSwiftExt                     0x104a4e720 $s7RxSwift17PrimitiveSequenceV0aB3ExtAA11SingleTraitORszrlE5mapToyACyAFqd__Gqd__lF + 96
24 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
25 RxSwift                        0x1047d3b2c $s7RxSwift3Map33_5428EFA9A9B0C0340021B871D2E5AC01LLCfD + 36
26 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
27 RxSwift                        0x1047e7974 $s7RxSwift28ObserveOnSerialDispatchQueue33_277A93ABA8477198C125F3F26B2D4B62LLCfD + 36
28 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
29 RxSwiftExt                     0x104a4d470 $s7RxSwift13SchedulerType_pWOc + 276
30 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
31 RxSwift                        0x10477d338 $s7RxSwift5Catch33_7E15DB08AB19A7B882D5C92A393C6F6DLLCfD + 36
32 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
33 RxSwift                        0x10477d130 $s7RxSwift9CatchSink33_7E15DB08AB19A7B882D5C92A393C6F6DLLCfD + 36
34 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
35 RxSwift                        0x1047e7738 $s7RxSwift32ObserveOnSerialDispatchQueueSink33_277A93ABA8477198C125F3F26B2D4B62LLCfD + 64
36 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
37 libswiftCore.dylib             0x19f1f6a14 void tuple_destroy<false, true>(swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) + 52
38 RxSwift                        0x104766460 $s7RxSwift26DispatchQueueConfigurationV16schedulePeriodic_10startAfter6period6actionAA10Disposable_px_0C00C12TimeIntervalOAKxxctlFyycfU0_ + 296
39 libswiftCore.dylib             0x19f1e25a4 _swift_release_dealloc + 40
40 libsystem_blocks.dylib         0x1e2870794 _Block_release + 200
41 libdispatch.dylib              0x19af80ac8 _dispatch_client_callout + 20
42 libdispatch.dylib              0x19af87c08 _dispatch_lane_serial_drain + 580
43 libdispatch.dylib              0x19af88734 _dispatch_lane_invoke + 408
44 libdispatch.dylib              0x19af92528 _dispatch_workloop_worker_thread + 708
45 libsystem_pthread.dylib        0x1e28dc908 _pthread_wqthread + 276
46 libsystem_pthread.dylib        0x1e28e377c start_wqthread + 8

From console:

/Users/realm/workspace/realm_realm-core_release_5.23.8/src/realm/util/thread.cpp:186: [realm-core-5.23.8] pthread_mutex_destroy() failed
0   Realm                               0x000000010e12cd9c _ZN5realm4utilL18terminate_internalERNSt3__118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 28
1   Realm                               0x000000010e12cffc _ZN5realm4util9terminateEPKcS2_lOSt16initializer_listINS0_9PrintableEE + 324
2   Realm                               0x000000010e12d868 _ZN5realm4util5Mutex11lock_failedEi + 0
3   Realm                               0x000000010e0df4c0 _ZN5realm5TableD2Ev + 620
4   Realm                               0x000000010de3fd04 _ZN5realm4ListD2Ev + 116
5   libobjc.A.dylib                     0x000000018f39ec90 0769A08C-FB76-344B-B754-4F9CEB93E3D0 + 19600
6   libobjc.A.dylib                     0x000000018f3b2cc4 objc_destructInstance + 88
7   libobjc.A.dylib                     0x000000018f3b973c _objc_rootDealloc + 52
8   Realm                               0x000000010de8e320 -[RLMListBase .cxx_destruct] + 28
9   libobjc.A.dylib                     0x000000018f39ec90 0769A08C-FB76-344B-B754-4F9CEB93E3D0 + 19600
10  libobjc.A.dylib                     0x000000018f3b2cc4 objc_destructInstance + 88
11  libobjc.A.dylib                     0x000000018f3b973c _objc_rootDealloc + 52
12  Turvo Driver Preview                0x0000000104e0f9b8 $s20Turvo_Driver_Preview18LogEventRealmModelCfETo + 108
13  libobjc.A.dylib                     0x000000018f39ec90 0769A08C-FB76-344B-B754-4F9CEB93E3D0 + 19600
14  libobjc.A.dylib                     0x000000018f3b2cc4 objc_destructInstance + 88
15  libobjc.A.dylib                     0x000000018f3b973c _objc_rootDealloc + 52
16  Realm                               0x000000010de98b30 -[RLMObjectBase dealloc] + 68
17  libswiftCore.dylib                  0x000000019cde9fa0 swift_arrayDestroy + 68
18  libswiftCore.dylib                  0x000000019cb7c954 $ss23_ContiguousArrayStorageCfD + 44
19  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
20  RxSwiftExt                          0x000000011192e720 $s7RxSwift17PrimitiveSequenceV0aB3ExtAA11SingleTraitORszrlE5mapToyACyAFqd__Gqd__lF + 96
21  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
22  RxSwift                             0x0000000111063bcc $s7RxSwift3Map33_5428EFA9A9B0C0340021B871D2E5AC01LLCfD + 36
23  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
24  RxSwift                             0x0000000111077a14 $s7RxSwift28ObserveOnSerialDispatchQueue33_277A93ABA8477198C125F3F26B2D4B62LLCfD + 36
25  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
26  RxSwiftExt                          0x000000011192d470 $s7RxSwift13SchedulerType_pWOc + 276
27  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
28  RxSwift                             0x000000011100d3d8 $s7RxSwift5Catch33_7E15DB08AB19A7B882D5C92A393C6F6DLLCfD + 36
29  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
30  RxSwift                             0x000000011100d1d0 $s7RxSwift9CatchSink33_7E15DB08AB19A7B882D5C92A393C6F6DLLCfD + 36
31  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
32  RxSwift                             0x00000001110777d8 $s7RxSwift32ObserveOnSerialDispatchQueueSink33_277A93ABA8477198C125F3F26B2D4B62LLCfD + 64
33  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
34  libswiftCore.dylib                  0x000000019ce04b24 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 3029796
35  RxSwift                             0x0000000110ff6500 $s7RxSwift26DispatchQueueConfigurationV16schedulePeriodic_10startAfter6period6actionAA10Disposable_px_0C00C12TimeIntervalOAKxxctlFyycfU0_ + 296
36  libswiftCore.dylib                  0x000000019cdf3c40 CBDFB4D8-EC8C-3F02-8E23-9476F031A001 + 2960448
37  libsystem_blocks.dylib              0x000000018f26ba4c _Block_release + 148
38  libdispatch.dylib                   0x00000001128bf730 _dispatch_client_callout + 16
39  libdispatch.dylib                   0x00000001128c6740 _dispatch_lane_serial_drain + 744
40  libdispatch.dylib                   0x00000001128c72e0 _dispatch_lane_invoke + 444
41  libdispatch.dylib                   0x00000001128d26c4 _dispatch_workloop_worker_thread + 1304
42  libsystem_pthread.dylib             0x000000018f394b74 _pthread_wqthread + 272
43  libsystem_pthread.dylib             0x000000018f397740 start_wqthread + 8!!! IMPORTANT: Please send this log and info about Realm SDK version and other relevant reproduction info to help@realm.io.

Version of Realm and Tooling

ProductName:    Mac OS X
ProductVersion: 10.15.6
BuildVersion:   19G2021

/Applications/Xcode.app/Contents/Developer
Xcode 12.3
Build version 12C33

/usr/local/bin/pod
1.10.0

/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)

/usr/local/bin/carthage
0.36.0
github "realm/realm-cocoa" "v4.4.1"

/usr/bin/git
git version 2.24.3 (Apple Git-128)

iOS version: 14.0.1 and 13.7.0

DominicFrei commented 3 years ago

Hi @anton-plebanovich !

Thank you for the information above.

Could you provide a code example of what you are doing to reproduce that crash? You said it's rare but from your description it sounded like you know what it happens. It would be helpful to see some more code so that we can reproduce it on our end.

Thanks!

Kind regards, Dominic

anton-plebanovich commented 3 years ago

Hi @DominicFrei , the code is complicated because there are a lot of custom classes and I don't think I can share it anyway. There is one more detail though, objects have been retained until the end of a network request but a thread is always the main. I can't reliably reproduce the crash and instead reworked code to prevent async objects usage and passing them now by thread-safe references instead.

Created this issue mostly because it says please_report_this_error_to_help_at_realm_dot_io 😄 . Maybe it'll be anyhow useful and if not I believe we can close this issue.

DominicFrei commented 3 years ago

And thank you for doing so, @anton-plebanovich! :)

Regarding the problem with sharing code: no worries, I can understand that. Sometimes developers can excerpt some of there code which does not reveal sensitive information yet provokes a problem we talk about.

Since you mentioned ThreadSafeReference and problems around threads. We talked about that topic in https://github.com/realm/realm-cocoa/issues/7028 quite a lot yesterday. Can you find anything similar / helpful in there maybe?

anton-plebanovich commented 3 years ago

@DominicFrei sadly the thread you mentioned is unrelated. The reporter there just has an issue with threading while I always use the main thread in my case. The other thing he has Realm 10, while I have Realm 4 which might be important. I tried to upgrade to Realm 5 but had issues with Realm notifications sporadically stop working so I had to fall back to version 4.

DominicFrei commented 3 years ago

That's actually a good hint. Have you tried v10+ yet? Both issues might have been fixed there already. @anton-plebanovich

anton-plebanovich commented 3 years ago

@DominicFrei I checked changelog and didn't see anything about both issues and there are several open issues for Realm 10 so I'm just afraid to update at this point 😱

leemaguire commented 3 years ago

Whats likely happening here is RxSwift is releasing the Realm objects while they are still being referenced.

leemaguire commented 3 years ago

@anton-plebanovich Would you be able to upgrade to the latest version of Realm? I still think this is an Rx library related issue and not a Realm issue

anton-plebanovich commented 3 years ago

@leemaguire I saw this crash only once so it might be anything actually. Let me just close this one since the Realm version is outdated and I wasn't able to reproduce.