-
-
Save beefon/0d5dbd6bffc0775bfd0fbed0785daecd to your computer and use it in GitHub Desktop.
import Dispatch | |
import Foundation | |
// If you run this playground, you will never see `Request feedback` message in console. | |
let queue = OperationQueue() | |
// but if you uncomment the line below, it suddenly starts working great | |
// queue.maxConcurrentOperationCount = 63 | |
// but if you set maxConcurrentOperationCount to 64, it stops working again | |
for _ in 0...999 { | |
queue.addOperation { | |
let group = DispatchGroup() | |
var request = URLRequest(url: URL(string: "http://example.com")!) | |
request.timeoutInterval = 10 | |
let task = URLSession.shared.dataTask(with: request) { _,_,_ in | |
print("\(Date()) Request feedback") | |
group.leave() | |
} | |
print("\(Date()) Starting request") | |
task.resume() | |
group.enter() | |
if group.wait(wallTimeout: .now() + 30) == .timedOut { | |
task.cancel() | |
print("\(Date()) Request timeout") | |
} | |
} | |
} | |
queue.waitUntilAllOperationsAreFinished() | |
print("\(Date()) Queue is empty") |
So, because of all operations being run first, and both OperationQueue
and URLSessionDataTask
using the same threads-pool (limited to 64), not setting maxConcurrentOperationCount
(that is leaving its default value -1 that probably translates to using the entire pool) means that no URLSessionDataTask
has chance to run until first 960 operations timeout.
While limiting maxConcurrentOperationCount
to 63 means that there will always be at least 1 thread in threads-pull for URLSessionDataTask
.
Also looks like compiler flattens out the nested thread dispatching
Or even more probable than that, is that URLSessionDataTask
s are being added to OperationQueue.current
with lower priority, but could not confirm. OperationQueue.current === queue
inside URLSessionDataTask
's completion block yields false
(may be completion queue is different)
Thanks Vakhid, I forgot to attach a link to the forum where Apple stuff has commented on this issue.
https://forums.swift.org/t/operationqueue-urlsession-no-network-activity-oob/33247
You were absolutely right, there is a shared thread pool in GCD for all queues, and that explains a lot.
It looks like
OperationQueue
andURLSessionDataTask
here pull threads from the same threads-pool (which is apparently limited to 64).Also looks like compiler flattens out the nested thread dispatching, for example if you run this with 0...3 range you will get:
The pattern will be the same for 0...N