Create and access Realm instances on a single thread in sequentially-executed code without crashing the app.
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.
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.
We can not reproduce this ourselves but around 1/10 users are affected.
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)
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
@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.
?
@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
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
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
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
@grangej @Elamuruga for documentation's sake, what version of Realm are you running?
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.
@thomasAcademia Thanks for the response right now I am using the latest version of Realm 5.3.3 upgrade from version 4.4.1.
I've successfully reproduced a case which would cause spurious incorrect thread exceptions and am working on a fix.
Most helpful comment
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.
` in
// this block is on a serial background thread
.map { (responseData) -> ThreadsafeTabPaginatedResponse
`
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