realm / realm-core

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

Intermittent EXC_BAD_ACCESS exceptions #7312

Open Cheezzhead opened 1 year ago

Cheezzhead commented 1 year ago

How frequently does the bug occur?

Sometimes

Description

While retrieving and updating a Realm object, the app intermittently crashes with an EXC_BAD_ACCESS exception, pointing to __cxx_atomic_store. 9 out of 10 times everything works as expected.

Below is relevant code for one of the instances of this error. The app throws an exception at the Realm.object(ofType:forPrimaryKey) call in the latter helper function.

struct AgendaStatusData: Decodable {
    @discardableResult
    func process(on api: API) async throws {
        guard let userId = api.userId else { throw APIError.notLoggedIn }
        let realm = await api.realm

        try await realm.update(DBUser.self, id: userId) { user in
            // Update user data
        }
    }
}

// Helper functions
extension Realm {
    public func update<O: Object & Identifiable, Result>(_ type: O.Type, id: O.ID, _ block: (O) throws -> Result) async throws -> Result {
        let object = try get(O.self, id: id).safeThaw()
        return try await self.asyncWrite { try block(object) }
    }

    public func get<O: Object & Identifiable>(_ object: O.Type, id: O.ID) throws -> O
    {
        guard let result = self.object(ofType: O.self, forPrimaryKey: id) else {
            throw AppError.databaseError(.invalidObjectReference(O.itemInfo(id: id)))
        }

        return result
    }
}

Stacktrace & log output

#0  0x000000010311b97c in void std::__1::__cxx_atomic_store[abi:v15006]<realm::Table*>(std::__1::__cxx_atomic_base_impl<realm::Table*>*, realm::Table*, std::__1::memory_order) at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.4.sdk/usr/include/c++/v1/atomic:945
realm/realm-swift#1 0x000000010311b90c in std::__1::__atomic_base<realm::Table*, false>::store[abi:v15006](realm::Table*, std::__1::memory_order) at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.4.sdk/usr/include/c++/v1/atomic:1552
realm/realm-swift#2 0x000000010311b8d8 in void std::__1::atomic_store_explicit[abi:v15006]<realm::Table*>(std::__1::atomic<realm::Table*>*, std::__1::atomic<realm::Table*>::value_type, std::__1::memory_order) at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.4.sdk/usr/include/c++/v1/atomic:1906
realm/realm-swift#3 0x000000010310b724 in void realm::util::store_atomic<realm::Table*>(realm::Table*&, realm::Table*, std::__1::memory_order) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-core/src/realm/util/thread.hpp:812
realm/realm-swift#4 0x000000010310aa4c in realm::Group::create_table_accessor(unsigned long) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-core/src/realm/group.cpp:793
realm/realm-swift#5 0x000000010310a5d4 in realm::Group::do_get_table(unsigned long) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-core/src/realm/group.cpp:663
realm/realm-swift#6 0x0000000102bdddf4 in realm::Group::get_table(realm::TableKey) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-core/src/realm/group.hpp:960
realm/realm-swift#7 0x0000000102cb7478 in realm::Object realm::Object::get_for_primary_key<objc_object* __strong, RLMAccessorContext>(RLMAccessorContext&, std::__1::shared_ptr<realm::Realm> const&, realm::ObjectSchema const&, objc_object* __strong) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-core/src/realm/object-store/object_accessor.hpp:390
realm/realm-swift#8 0x0000000102cb7190 in RLMGetObject at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-swift/Realm/RLMObjectStore.mm:226
realm/realm-swift#9 0x0000000102f8001c in Realm.object<τ_0_0, τ_0_1>(ofType:forPrimaryKey:) at /Users/***/Library/Developer/Xcode/DerivedData/App-gieupfwmgkxfrjaauikcpstmrmdx/SourcePackages/checkouts/realm-swift/RealmSwift/Realm.swift:794
realm/realm-swift#10    0x0000000103c8ef70 in Realm.get<τ_0_0>(_:id:) at ***
realm/realm-swift#11    0x0000000103c90c18 in Realm.update<τ_0_0, τ_0_1>(_:id:_:) at ***
realm/realm-swift#12    0x00000001039724dc in AgendaStatusData.process(on:) at ***
realm/realm-swift#13    0x0000000103996678 in closure realm/realm-swift#1 in API.updateAgendas() at ***
realm/realm-swift#14    0x00000001039969e0 in partial apply for closure realm/realm-swift#1 in API.updateAgendas() ()
realm/realm-swift#15    0x000000010399dc48 in closure realm/realm-swift#1 in API.startJob<τ_0_0, τ_0_1>(_:item:block:) at ***
realm/realm-swift#16    0x000000010399e480 in partial apply for closure realm/realm-swift#1 in API.startJob<τ_0_0, τ_0_1>(_:item:block:) ()

Can you reproduce the bug?

Sometimes

Reproduction Steps

No response

Version

10.42.0

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 16.4

Build environment

Xcode version: 14.3 (14E222b) Dependency manager and version: SPM

ironage commented 5 months ago

Observations: Swift version 10.42.0 uses Core v13.17.1. The stack trace points to this line of code: https://github.com/realm/realm-core/blob/v13.17.1/src/realm/group.cpp#L793 which may indicate an invalid atomic is being used.