realm / realm-core

Core database component for the Realm Mobile Database SDKs
https://realm.io
Apache License 2.0
1.02k stars 165 forks source link

Crash in do_encryption_read_barrier on Array::init_from_ref #6633

Open chibunduanwuna-dialpad opened 1 year ago

chibunduanwuna-dialpad commented 1 year ago

How frequently does the bug occur?

Sometimes

Description

I'm attempting to do a write transaction for an array of data that I get from a remote server. While this works most of the time, it's been known to fail sporadically with an unexplained error ( see the crash logs for details). Here's the code of what I'm doing:

var contacts = [Contact]()
do {
    let realm = try Current.realm()
    realm.beginWrite()
    for contactData in contactsData {
        let contact = insertContact(contactData, realm: realm)
        contacts.append(contact)
    }
    try realm.commitWrite()
} catch {
    ...
}

func insertContact(
    _ contactData: [String: AnyObject],
    realm: Realm? = nil
) -> Contact {
    do {
        let realm = try realm ?? Current.realm()
        var contact: Contact

        let storeContact = {
            contact = realm.create(SWTContact.self, value: contactData, update: .all)
        }

        if realm.isInWriteTransaction {
            storeContact()
        } else {
            realm.beginWrite()
            storeContact()
            try realm.commitWrite()
        }

        return contact
    } catch {
        ...
    }
    ...
}

Stacktrace & log output

Crashed: co.Dialpad.fetchQueue
0  libsystem_kernel.dylib         0x7200 __pthread_kill + 8
1  libsystem_pthread.dylib        0x71ac pthread_kill + 268
2  libsystem_c.dylib              0x20c8c abort + 180
3  Dialpad                        0x171dbc8 realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 147 (terminate.cpp:147)
4  Dialpad                        0x171df08 realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 142 (terminate.cpp:142)
5  Dialpad                        0x171dc84 realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 152 (terminate.cpp:152)
6  Dialpad                        0x12a5408 realm::util::do_encryption_read_barrier(void const*, unsigned long, unsigned long (*)(char const*), realm::util::EncryptedFileMapping*, bool) + 144 (file_mapper.hpp:144)
7  Dialpad                        0x149ed14 realm::Array::init_from_ref(unsigned long) + 576 (alloc.hpp:576)
8  Dialpad                        0x149f290 realm::Array::do_write_deep(realm::_impl::ArrayWriterBase&, bool) const + 843 (array.hpp:843)
9  Dialpad                        0x149f2a8 realm::Array::do_write_deep(realm::_impl::ArrayWriterBase&, bool) const + 846 (array.hpp:846)
10 Dialpad                        0x149f2a8 realm::Array::do_write_deep(realm::_impl::ArrayWriterBase&, bool) const + 846 (array.hpp:846)
11 Dialpad                        0x149f2a8 realm::Array::do_write_deep(realm::_impl::ArrayWriterBase&, bool) const + 846 (array.hpp:846)
12 Dialpad                        0x149f2a8 realm::Array::do_write_deep(realm::_impl::ArrayWriterBase&, bool) const + 846 (array.hpp:846)
13 Dialpad                        0x14e5c98 realm::GroupWriter::write_group() + 832 (array.hpp:832)
14 Dialpad                        0x14d1100 realm::DB::low_level_commit(unsigned long long, realm::Transaction&, bool) + 2258 (db.cpp:2258)
15 Dialpad                        0x14d0f48 realm::DB::do_commit(realm::Transaction&, bool) + 448 (replication.hpp:448)
16 Dialpad                        0x16f33e8 realm::Transaction::commit_and_continue_as_read(bool) + 209 (transaction.cpp:209)
17 Dialpad                        0x1561438 realm::_impl::RealmCoordinator::commit_write(realm::Realm&, bool) + 727 (realm_coordinator.cpp:727)
18 Dialpad                        0x15c3b90 realm::Realm::commit_transaction() + 997 (shared_realm.cpp:997)
19 Dialpad                        0x13b9f44 -[RLMRealm commitWriteTransactionWithoutNotifying:error:] + 750 (RLMRealm.mm:750)
20 Dialpad                        0x145b988 Realm.commitWrite(withoutNotifying:) + 322 (Realm.swift:322)
21 Dialpad                        0x1f7c60 specialized static Contact.insert(_:endpointSource:) + 4376198240 (<compiler-generated>:4376198240)
22 Dialpad                        0x258514 closure realm/realm-swift#1 in closure realm/realm-swift#1 in ContactDataManager.fetch(_:failure:) + 4376593684 (<compiler-generated>:4376593684)
23 Dialpad                        0x50d64 thunk for @escaping @callee_guaranteed () -> () + 4374465892 (<compiler-generated>:4374465892)
24 libdispatch.dylib              0x24b4 _dispatch_call_block_and_release + 32
25 libdispatch.dylib              0x3fdc _dispatch_client_callout + 20
26 libdispatch.dylib              0xb694 _dispatch_lane_serial_drain + 672
27 libdispatch.dylib              0xc1e0 _dispatch_lane_invoke + 384
28 libdispatch.dylib              0x16e10 _dispatch_workloop_worker_thread + 652
29 libsystem_pthread.dylib        0xdf8 _pthread_wqthread + 288
30 libsystem_pthread.dylib        0xb98 start_wqthread + 8

Can you reproduce the bug?

No

Reproduction Steps

No response

Version

10.28.2, 10.32.2

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 16, iOS 15, iOS 14, iPadOS 16

Build environment

Xcode version: ... Dependency manager and version: ...

sync-by-unito[bot] commented 1 year ago

➤ jedelbo commented:

[~finn.schiermer-andersen@mongodb.com] does this look familiar?

chibunduanwuna-dialpad commented 1 year ago

Do we have any update on this? The crash is still occurring quite frequently on our App.