realm-swift: Crash happens: Multiple sync agents attempted to join the same session

Goals

Realm Sync normally

Actual Results

Very often crash with this error

terminating with uncaught exception of type realm::MultipleSyncAgents: 
Multiple sync agents attempted to join the same session

Xnip2020-12-08UNITO-UNDERSCORE!18-45-22!

Xnip2020-12-08UNITO-UNDERSCORE!18-45-33!

libc++abi.dylib: terminating with uncaught exception of type realm::MultipleSyncAgents: Multiple sync agents attempted to join the same session
Exception backtrace:
0   Realm                               0x0000000113de1b78 _ZN5realm2DB7do_openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEbbNS_9DBOptionsE + 6620
1   Realm                               0x0000000113de3870 _ZN5realm2DB4openERNS_11ReplicationENS_9DBOptionsE + 244
2   Realm                               0x0000000113de8358 _ZN5realm2DB6createERNS_11ReplicationENS_9DBOptionsE + 392
3   Realm                               0x0000000113b9fde8 _ZN5realm5_impl21ClientFileAccessCache4Slot4openEv + 620
4   Realm                               0x0000000113bef6fc _ZN12_GLOBAL__N_111SessionImpl12access_realmEv + 28
5   Realm                               0x0000000113baa780 _ZN5realm5_impl14ClientImplBase7Session8activateEv + 604
6   Realm                               0x0000000113baa41c _ZN5realm5_impl14ClientImplBase10Connection16activate_sessionENSt3__110unique_ptrINS1_7SessionENS3_14default_deleteIS5_EEEE + 244
7   Realm                               0x0000000113bebd28 _ZN5realm4util7network7Trigger8ExecOperIZN12_GLOBAL__N_110ClientImplC1ENS_4sync6Client6ConfigEEUlvE_E19recycle_and_executeEv + 2104
8   Realm                               0x0000000113cb0bb8 _ZN5realm4util7network7Service4Impl3runEv + 404
9   Realm                               0x0000000113be4fb0 _ZN5realm4sync6Client3runEv + 36
10  Realm                               0x0000000113b000f0 _ZZN5realm5_impl10SyncClientC1ENSt3__110unique_ptrINS_4util6LoggerENS2_14default_deleteIS5_EEEERKNS_16SyncClientConfigENS2_10shared_ptrIKNS_11SyncManagerEEEENKUlvE0_clEv + 232
11  Realm                               0x0000000113afffc0 _ZNSt3__1L8__invokeIZN5realm5_impl10SyncClientC1ENS_10unique_ptrINS1_4util6LoggerENS_14default_deleteIS6_EEEERKNS1_16SyncClientConfigENS_10shared_ptrIKNS1_11SyncManagerEEEEUlvE0_JEEEDTclclsr3std3__1E7forwardIT_Efp_Espclsr3std3__1E7forwardIT0_Efp0_EEEOSI_DpOSJ_ + 28
12  Realm                               0x0000000113afff20 _ZNSt3__1L16__thread_executeINS_10unique_ptrINS_15__thread_structENS_14default_deleteIS2_EEEEZN5realm5_impl10SyncClientC1ENS1_INS6_4util6LoggerENS3_ISA_EEEERKNS6_16SyncClientConfigENS_10shared_ptrIKNS6_11SyncManagerEEEEUlvE0_JEJEEEvRNS_5tupleIJT_T0_DpT1_EEENS_15__tuple_indicesIJXspT2_EEEE + 32
13  Realm                               0x0000000113aff64c _ZNSt3__1L14__thread_proxyINS_5tupleIJNS_10unique_ptrINS_15__thread_structENS_14default_deleteIS3_EEEEZN5realm5_impl10SyncClientC1ENS2_INS7_4util6LoggerENS4_ISB_EEEERKNS7_16SyncClientConfigENS_10shared_ptrIKNS7_11SyncManagerEEEEUlvE0_EEEEEPvSN_ + 116
14  libsystem_pthread.dylib             0x00000001f396ab40 _pthread_start + 320
15  libsystem_pthread.dylib             0x00000001f3973768 thread_start + 8

Code Sample

    public static func writeAsync<T: Object>(obj: T) {
        guard let config = realmConfig else { return }
        realmQueue.async {
            autoreleasepool {
                do {
                    let realm = try? Realm(configuration: config)
                    try realm?.write {
                        realm?.add(obj)
                    }
                }
                catch {
                    print("writeAsync error: \(error.localizedDescription)")
                }
            }
        }
    }

Is there any chance that the mongoDB Sync doesn’t cover the realm object writing and sync at the same times?

Version of Realm and Tooling

Realm framework version: 10.2.0

Xcode version: 12.2

iOS/OSX version: iOS 14.2.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (5 by maintainers)

Most upvoted comments

I was getting the same issue, but turns out that my sync’d realm was also open in Realm Browser when I was trying to run the app on the simulator. As soon as I close the Realm Browser, the issue is gone 😃

This happens to us in the production environment with the 10.23.0 Realm version. There is no way our users could use RealmBrowser/Studio there.

Fatal Exception: realm::MultipleSyncAgents
Multiple sync agents attempted to join the same session Exception backtrace: 
0 Divtracker 0x00000001007b9bb4 _ZNK5realm19ArrayFixedBytesNullINS_4UUIDELi16EE15find_first_nullEmm + 166096 
1 Divtracker 0x0000000100903d74 _ZNK5realm5Table10find_firstINS_4util8OptionalINS_8ObjectIdEEEEENS_6ObjKeyENS_6ColKeyET_ + 320680 
2 Divtracker 0x0000000100903744 _ZNK5realm5Table10find_firstINS_4util8OptionalINS_8ObjectIdEEEEENS_6ObjKeyENS_6ColKeyET_ + 319096 
3 Divtracker 0x00000001009375b4 _ZNK5realm5Table10find_firstINS_4util8OptionalINS_8ObjectIdEEEEENS_6ObjKeyENS_6ColKeyET_ + 531688 
4 Divtracker 0x000000010096cf8c _ZNK5realm5Table10find_firstINS_4util8OptionalINS_8ObjectIdEEEEENS_6ObjKeyENS_6ColKeyET_ + 751296 
5 Divtracker 0x0000000100907d94 _ZNK5realm5Table10find_firstINS_4util8OptionalINS_8ObjectIdEEEEENS_6ObjKeyENS_6ColKeyET_ + 337096 
6 Divtracker 0x0000000100a6e8bc _ZN5realm12object_store3Set6insertINS_4util8OptionalINS_4UUIDEEEEENSt3__14pairImbEET_ + 548316 
7 libsystem_pthread.dylib 0x00000001f0cca9ac _pthread_start + 148 
8 libsystem_pthread.dylib 0x00000001f0cc9e68 thread_start + 8

Update 1: The user received Error Domain=io.realm.sync Code=5 "Unable to refresh the user access token." UserInfo={NSLocalizedDescription=Unable to refresh the user access token., statusCode=203} error, and the app reacted with the user logout/login flow. Now, when I think about it, I believe I had to resetAppCache() the same way I do on other errors because it looks like Realm does not close sessions on errors which causes crashes later.

@jlavyan I haven’t run into this issue on newer realm versions, and I have multiple realms running simultaneously. Maybe post your verbose Realm logs and related code to see if something else is causing the issue?

This crash always happen if you have multiply realms. Tested on 10.7.2

I created some class which prevent multiply connection, so I suspend all session then run with in serialization order. I used suspend resume functions