Dictionary Subscript getter crash

I'm having several crashes that I've been unable to replicate

Code Block
<compiler-generated> - Line 4370483668
specialized Dictionary.subscript.getter + 4370483668

I suspect this might be related with dictionaries not being accessed in a safe way due the use of several threads(?)

The most common crash happens in this line
Code Block
guard self.service?.handlers[id] != nil else {

any ideas or suggestions?

Do you think a .async(flags: .barrier) could solve the issue? because I'm unable to replicate it.

this code is called via observers over a Realm database so the Unit test i've created does several threads that try to write the database at same time.

UNIT TEST PSEUDO CODE
Code Block
func testDictionaryAccess()
{
for i in 0...10
{
dispatchGroup.enter()
DispatchQueue(label: "createActivityThreads").async {
create records on database()
dispatchGroup.leave()
}
}
dispatchGroup.wait()
XCTAssertTrue(true)
}

each loop iteration takes about 20 seconds to run so theres plenty of time for them to start having conflicts between each other

I suspect this might be related with dictionaries not being accessed in a safe way due the use of several threads(?)

Very likely.

Do you think a .async(flags: .barrier) could solve the issue?

Not sure.


A reliable way, create a serial queue and touch the dictionary (or any other thread-unsafe objects) only in that queue.
(I mean both read and write with touch.)

Dictionary Subscript getter crash
 
 
Q