Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-787

mutex lock failed: Invalid argument while releasing Realm

      Goals

      Release realm in autoreleasepool.

      Expected Results

      Released realm.

      Actual Results

      Fatal Exception: std::__1::system_error
      mutex lock failed: Invalid argument
      
      Crashed: com.google.firebase.crashlytics.ios.exception
      0  pitt                           0x102fff184 FIRCLSProcessRecordAllThreads + 392 (FIRCLSProcess.c:392)
      1  pitt                           0x102fff568 FIRCLSProcessRecordAllThreads + 423 (FIRCLSProcess.c:423)
      2  pitt                           0x102ff5de4 FIRCLSHandler + 34 (FIRCLSHandler.m:34)
      3  pitt                           0x102ff1ae4 __FIRCLSExceptionRecord_block_invoke + 218 (FIRCLSException.mm:218)
      4  libdispatch.dylib              0x1b958b524 _dispatch_client_callout + 16
      5  libdispatch.dylib              0x1b953e0b0 _dispatch_lane_barrier_sync_invoke_and_complete + 56
      6  pitt                           0x102ff127c FIRCLSExceptionRecord + 225 (FIRCLSException.mm:225)
      7  pitt                           0x102ff0f1c FIRCLSTerminateHandler() + 285 (FIRCLSException.mm:285)
      8  libc++abi.dylib                0x1b968e14c std::__terminate(void (*)()) + 16
      9  libc++abi.dylib                0x1b968e0e4 std::terminate() + 44
      10 Realm                          0x104a2b36c std::__1::shared_ptr<realm::_impl::CollectionNotifier> std::__1::atomic_load<realm::_impl::CollectionNotifier>(std::__1::shared_ptr<realm::_impl::CollectionNotifier> const*) + 5427 (memory:5427)
      11 Realm                          0x104c84ddc realm::Group::~Group() + 122
      12 Realm                          0x104c8f25c realm::Transaction::close() + 60
      13 Realm                          0x104c91d34 TransactionDeleter(realm::Transaction*) + 20
      14 Realm                          0x104c969a8 std::__1::__shared_ptr_pointer<realm::Transaction*, void (*)(realm::Transaction*), std::__1::allocator<realm::Transaction> >::__on_zero_shared() + 13896
      15 Realm                          0x104a2b484 std::__1::shared_ptr<realm::_impl::CollectionNotifier>::~shared_ptr() + 3586 (memory:3586)
      16 Realm                          0x104b3a514 realm::Realm::~Realm() + 81 (shared_realm.cpp:81)
      17 Realm                          0x104a2b484 std::__1::shared_ptr<realm::_impl::CollectionNotifier>::~shared_ptr() + 3586 (memory:3586)
      18 libobjc.A.dylib                0x1b95e6c90 object_cxxDestructFromClass(objc_object*, objc_class*) + 112
      19 libobjc.A.dylib                0x1b95facc4 objc_destructInstance + 88
      20 libobjc.A.dylib                0x1b960173c _objc_rootDealloc + 52
      21 Realm                          0x104b0b890 -[RLMRealm dealloc] + 814 (RLMRealm.mm:814)
      22 libobjc.A.dylib                0x1b960404c AutoreleasePoolPage::releaseUntil(objc_object**) + 180
      23 libobjc.A.dylib                0x1b9603f44 objc_autoreleasePoolPop + 224
      24 libswiftObjectiveC.dylib       0x1efff2bd8 autoreleasepool<A>(invoking:) + 68
      25 pitt                           0x102f1a8c4 partial apply for closure #1 in PageCacheService.delete(in:) + 584 (PageCacheService.swift:584)
      26 pitt                           0x102f353f4 thunk for @escaping @callee_guaranteed () -> () + 4376204276 (<compiler-generated>:4376204276)
      27 libdispatch.dylib              0x1b958a9a8 _dispatch_call_block_and_release + 24
      28 libdispatch.dylib              0x1b958b524 _dispatch_client_callout + 16
      29 libdispatch.dylib              0x1b95378a4 _dispatch_lane_serial_drain$VARIANT$mp + 608
      30 libdispatch.dylib              0x1b9538294 _dispatch_lane_invoke$VARIANT$mp + 416
      31 libdispatch.dylib              0x1b954178c _dispatch_workloop_worker_thread + 588
      32 libsystem_pthread.dylib        0x1b95dcb74 _pthread_wqthread + 272
      33 libsystem_pthread.dylib        0x1b95df740 start_wqthread + 8
      

      Steps for others to Reproduce

      Use autoreleasepool in DispatchQueue.

      Code Sample

          public func delete(in controllerIds: [String]) {
              guard !controllerIds.isEmpty else { return }
              transactionQueue.async {
                  autoreleasepool{ [weak self] in
                      let logHelper = LogHelper(subsystem: "PageCacheService", category: "DeleteByControllerId")
      
                      do {
                          logHelper.d("Deleting \(controllerIds)")
      
                          let realm = try RealmCacheHelper.shared.realm()
      
                          let matrix = self?.list(realm, in: controllerIds)
                          let state = self?.find(realm, in: controllerIds)
      
                          let emptyMatrix = matrix?.isEmpty ?? true
                          let emptyState = state?.isEmpty ?? true
      
                          guard !emptyMatrix || !emptyState else {
                              logHelper.d("Cache not found \(controllerIds)")
                              return
                          }
      
                          try realm.write {
                              if let s = state {
                                  realm.delete(s)
                              }
                              if let m = matrix {
                                  realm.delete(m)
                              }
                          }
      
                          logHelper.d("Deleted \(controllerIds)")
                      }
                      catch {
                          logHelper.e("Failed to delete page cache, error = %@", error: error)
                          Crashlytics.crashlytics().record(error: error)
                      }
                  }
              }
          }
      

      Version of Realm and Tooling

      Realm framework version: 5.3.2

      Realm Object Server version: N/A

      Xcode version: 11.6

      iOS/OSX version: 10, 12, 13, 14

      Dependency manager + version: CocoaPods 1.8.3

            Assignee:
            pavel.yakimenko@mongodb.com Pavel Yakimenko (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: