Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequential creation and access of Realm instance crashes app. #6689

Closed
thomasAcademia opened this issue Aug 4, 2020 · 8 comments · Fixed by #6714
Closed

Sequential creation and access of Realm instance crashes app. #6689

thomasAcademia opened this issue Aug 4, 2020 · 8 comments · Fixed by #6714

Comments

@thomasAcademia
Copy link

thomasAcademia commented Aug 4, 2020

Goals

Create and access Realm instances on a single thread in sequentially-executed code without crashing the app.

Expected Results

Creating the Realm instance returns a valid object or throws an error. On the next line of code, accessing the Realm instance via methods like Realm.objects should not crash the app and return valid values.

Actual Results

Creating the Realm instance succeeds but the following lines using the Realm instance crash the app with

Fatal Exception: RLMException
Realm accessed from incorrect thread.

Steps for others to Reproduce

We can not reproduce this ourselves but around 1/10 users are affected.

Code Sample

do {
    let realm = try Realm()
    // This realm.write call sometimes causes a fatal exception
    try realm.write {
        newModel.lastFetched = Date()
        realm.add(newModel, update: .modified)
    }
} catch {
    log.error("{REDACTED}")
}
// In some static method
guard let realm = try? Realm() else { return nil }
// This return sometimes causes a fatal exception
return realm.object(ofType: Self.self, forPrimaryKey: id)

Version of Realm and Tooling

Realm framework version: v5.3.2

Realm Object Server version: N/A

Xcode version: 11.5

iOS/OSX version: iOS only, Minimum version 11.0, occurs on all iOS versions

Dependency manager + version: Carthage v0.35.0

@ejm01
Copy link
Contributor

ejm01 commented Aug 10, 2020

@thomasAcademia
Does your project use Realm across multiple threads in other parts of the application? And do you receive any other crash information other than

Fatal Exception: RLMException
Realm accessed from incorrect thread.

?

@thomasAcademia
Copy link
Author

@ericjordanmossman yes, we use Realm across multiple threads, but they are used in place and never passed between threads.

Our stack traces are currently not symbolicated because of issues with Bitcode and Firebase so the Realm methods used will be "(Missing)" unfortunately:

Fatal Exception: RLMException
0  CoreFoundation                 0x1b90b1794 __exceptionPreprocess
1  libobjc.A.dylib                0x1b8dd3bcc objc_exception_throw
2  Realm                          0x1056fcb10 (Missing)
3  Realm                          0x1056d14c0 (Missing)
4  Realm                          0x1056d2ea4 (Missing)
5  RealmSwift                     0x10691cd3c (Missing)
6  Academia                       0x104bb591c {REDACTED}
7  Academia                       0x104c4a508 {REDACTED}
8  Academia                       0x104c55b90 {REDACTED}
9  Academia                       0x104c930e4 {REDACTED}
10 Academia                       0x104c914e4 {REDACTED}
11 CFNetwork                      0x1bc30f0ec CFNetServiceBrowserSearchForServices
12 CFNetwork                      0x1bc31fb04 _CFHTTPMessageSetResponseProxyURL
13 Foundation                     0x1b94783f0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
14 Foundation                     0x1b9383710 -[NSBlockOperation main]
15 Foundation                     0x1b947a64c __NSOPERATION_IS_INVOKING_MAIN__
16 Foundation                     0x1b9383414 -[NSOperation start]
17 Foundation                     0x1b947b044 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__
18 Foundation                     0x1b947ab10 __NSOQSchedule_f
19 libdispatch.dylib              0x1b8d5add0 _dispatch_block_async_invoke2
20 libdispatch.dylib              0x1b8d77524 _dispatch_client_callout
21 libdispatch.dylib              0x1b8d51274 _dispatch_continuation_pop$VARIANT$armv81
22 libdispatch.dylib              0x1b8d509e8 _dispatch_async_redirect_invoke
23 libdispatch.dylib              0x1b8d5d534 _dispatch_root_queue_drain
24 libdispatch.dylib              0x1b8d5dcd0 _dispatch_worker_thread2
25 libsystem_pthread.dylib        0x1b8dc8b38 _pthread_wqthread
26 libsystem_pthread.dylib        0x1b8dcb740 start_wqthread

#10 is a callback from a network thread.

@grangej
Copy link

grangej commented Aug 13, 2020

I am also experiencing this issue , we create a separate thread for writes and while it works most of the the time I am able to crash if there are a lot of background writes happening concurrently.

`
// this block is on a serial background thread
.map { (responseData) -> ThreadsafeTabPaginatedResponse in

            let realm = try Realm()
            
            realm.beginWrite() // crash happens here

`

2020-08-12 22:40:24.566037-0700 xxxx[12639:281619] *** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread.' *** First throw call stack: ( 0 CoreFoundation 0x00007fff20438fda __exceptionPreprocess + 242 1 libobjc.A.dylib 0x00007fff2017712e objc_exception_throw + 48 2 xxxx 0x0000000102fad20f _Z18RLMSetErrorOrThrowP7NSErrorPU15__autoreleasingS0_ + 991 3 xxxx 0x0000000102f72060 _Z26RLMRealmTranslateExceptionPU15__autoreleasingP7NSError + 592 4 xxxx 0x0000000102f75347 -[RLMRealm beginWriteTransactionWithError:] + 87 5 xxxx 0x0000000102f752e8 -[RLMRealm beginWriteTransaction] + 40 6 xxxx 0x00000001036f9215 $s10RealmSwift0A0V10beginWriteyyF + 53 7 xxxx 0x00000001025b919e

@Elamuruga
Copy link

Ya even me too having the same issue I don't know where I did mistake. I can't able to reproduce it but it's crash randomly somewhere in each background task but not in the same task every time. Could you please help me to resolve this issue? thanks in advance

@thomasAcademia
Copy link
Author

@grangej @Elamuruga for documentation's sake, what version of Realm are you running?

@grangej
Copy link

grangej commented Aug 14, 2020

I am running 5.3.3, I believe I was able to solve the problem by passing the queue to the realm init, but this is not ideal since I use this in RX and had to find a way to get the current queue everywhere.

@Elamuruga
Copy link

Elamuruga commented Aug 17, 2020

@thomasAcademia Thanks for the response right now I am using the latest version of Realm 5.3.3 upgrade from version 4.4.1.

@tgoyne
Copy link
Member

tgoyne commented Aug 20, 2020

I've successfully reproduced a case which would cause spurious incorrect thread exceptions and am working on a fix.

@tgoyne tgoyne self-assigned this Aug 20, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants