Why is dispatch_sync on custom concurrent queue deadlocking

后端 未结 1 1280
耶瑟儿~
耶瑟儿~ 2021-02-05 18:31

I\'m seeing an intermittent deadlock in my app when using dispatch_sync on a custom concurrent dispatch_queue. I\'m using something similar to the method described in Mike Ash\'

1条回答
  •  时光说笑
    2021-02-05 19:12

    EDIT: Turns out the problem was that the NSOperationQueue which is calling enqueueOperation: is using all available threads, so since they are all waiting (via dispatch_sync) for something to happen on the activeRequestsQueue. Reducing the maxConcurrentOperations on this queue solved the problem (see comments), though this is not really a great solution because it makes assumptions about the number of cores, etc. A better solution would be to use dispatch_async rather than dispatch_sync, though this will make the code more complex.

    My earlier suggestions:

    • You're calling dispatch_sync(activeRequestsQueue, ...) when you're already on the activeRequestsQueue (and your assert isn't firing for some reason, like you're running in release.)

    • [activeRequests removeObjectForKey:key]; is causing a request to be deallocated, and the dealloc is waiting for something that calls activeRequestForRpc:, which would cause a deadlock.

    0 讨论(0)
提交回复
热议问题