Skip to content

Crash when enqueueing a lot of tasks on background threads #14

@gapl

Description

@gapl

We are enqueueing all our Realm writes with TaskQueue and are seeing occasional crashes when a lot of these tasks are enqueued.

Crash is happening on line 97, task = tasks.removeAtIndex(0) on a concurrent background thread.

We are enqueueing our writes in the following manner:

/// We have a static variable that serves as our TaskQueue
RLMRealm.taskQueue.tasks +=~ {
realm.write {
    // Do writes here...
}

// Start running, if it's already running this is simply a no-op
RLMRealm.taskQueue.run()

Stack trace:

* thread #26: tid = 0x68f2d, 0x0000000102cf3d4c libswiftCore.dylib`_swift_release_(swift::HeapObject*) + 8, queue = 'com.apple.root.utility-qos', stop reason = EXC_BAD_ACCESS (code=1, address=0x16a5bd0876d60d4)
    frame #0: 0x0000000102cf3d4c libswiftCore.dylib`_swift_release_(swift::HeapObject*) + 8
    frame #1: 0x0000000102cf6434 libswiftCore.dylib`swift::metadataimpl::ValueWitnesses<(anonymous namespace)::ThickFunctionBox>::destroyArray(swift::OpaqueValue*, unsigned long, swift::Metadata const*) + 36
    frame #2: 0x0000000102b3d900 libswiftCore.dylib`Swift.UnsafeMutablePointer.moveAssignFrom (Swift.UnsafeMutablePointer<A>, count : Swift.Int) -> () + 120
    frame #3: 0x0000000102b32f54 libswiftCore.dylib`(extension in Swift):Swift._ArrayBufferType.replace <A where A1: Swift.CollectionType, A.Element == A1.Generator.Element> (subRange : Swift.Range<Swift.Int>, with : Swift.Int, elementsOf : A1) -> () + 2236
    frame #4: 0x0000000102b54b5c libswiftCore.dylib`Swift.Array.replaceRange <A where A1: Swift.CollectionType, A == A1.Generator.Element> (Swift.Range<Swift.Int>, with : A1) -> () + 1464
    frame #5: 0x0000000102b5ba74 libswiftCore.dylib`Swift.Array.removeAtIndex (Swift.Int) -> A + 448
  * frame #6: 0x000000010046e990 App`TaskQueue.(result=nil, self=0x00000001513742a0)(AnyObject?) -> () + 696 at TaskQueue.swift:97
    frame #7: 0x0000000100472c8c App `TaskQueue.(nextResult=nil, self=0x00000001513742a0)(AnyObject?) -> ()).(closure #1).(closure #1) + 164 at TaskQueue.swift:114
    frame #8: 0x00000001004729b4 App `thunk + 32 at TaskQueue.swift:0
    frame #9: 0x0000000100472a7c App `thunk + 36 at TaskQueue.swift:0
    frame #10: 0x00000001004729b4 App `thunk + 32 at TaskQueue.swift:0
    frame #11: 0x0000000100472a7c App `thunk + 36 at TaskQueue.swift:0
    frame #12: 0x0000000100473470 App `static (task=0x0000000100329904 App `partial apply forwarder for (extension in App):__ObjC.RLMRealm.(enqueueWrite ((realm : __ObjC.RLMRealm) -> ()) -> __ObjC.BFTask).(closure #1) at RLMRealmExtension.swift, next=0x0000000100472014 App `partial apply forwarder for reabstraction thunk helper from @callee_owned (@in Swift.Optional<Swift.AnyObject>) -> (@out ()) to @callee_owned (@owned Swift.Optional<Swift.AnyObject>) -> (@unowned ()) at TaskQueue.swift) -> ()) -> ()], () -> ()) -> ()).(closure #1).(closure #1) + 92 at TaskQueue.swift:246
    frame #13: 0x00000001001193dc App `thunk + 44 at AddressBookContactExtension.swift:0
    frame #14: 0x0000000103225a7c libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #15: 0x0000000103225a3c libdispatch.dylib`_dispatch_client_callout + 16
    frame #16: 0x0000000103234c9c libdispatch.dylib`_dispatch_root_queue_drain + 2344
    frame #17: 0x0000000103234364 libdispatch.dylib`_dispatch_worker_thread3 + 132
    frame #18: 0x0000000181c11470 libsystem_pthread.dylib`_pthread_wqthread + 1092
    frame #19: 0x0000000181c11020 libsystem_pthread.dylib`start_wqthread + 4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions