3

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

<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

guard self.service?.handlers[id] != nil else {

service is a weak variable

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--

func testDictionaryAccess()
{
   for i in 0...3
   {
     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

5
  • I don't think there's enough code here to really understand or diagnose the issue. For example, service is a week variable... A variable of what? A class? dictionary? Something else? this code is called via observers over a Realm - what is being called, what's being observed and what's the process. Please review How to create a Minimal, Complete, and Verifiable example update and refine the question and we'll take a look.
    – Jay
    Commented Sep 5, 2020 at 13:06
  • Check this blog post medium.com/swiftcairo/…
    – Kamil.S
    Commented Sep 7, 2020 at 10:33
  • I'm seeing these errors too. Sometimes even on lines that simply pass a dictionary literal and enum. Does Swift represent enums as dicitionaries internally??
    – Hari Honor
    Commented Dec 7, 2020 at 13:00
  • @joão-serra, having same kind of issue. please share if you found solution or trick to handle this, Thanks. Commented Dec 27, 2023 at 13:14
  • @HardikDarji This was like 3 years ago, I dont quite remember what happen. But I read in another posts that this is probably not thread safe. So my guess is that you must make sure that you always write /read in the same DispatchQueue In my example i was creating a queue per loop iteration. Probably in order to test concurrency at the time to deal with another issue. Commented Dec 29, 2023 at 20:46

0

Browse other questions tagged or ask your own question.