Skip to content

make sure writes to URLSession.taskRegistry happen on work queue #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -552,24 +552,30 @@ extension _ProtocolClient : URLProtocolClient {
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: nil)
task.state = .completed
task.workQueue.async {
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for necroposting but this and this https://github.com/apple/swift-corelibs-foundation/pull/1625/files#diff-14c6cb724eead661596e4468ae5ca638R625 looks like overkill
There is no thread switching with session.delegateQueue.addOperation
And if this method itself not synchronized race will be earlier at line 545

session.taskRegistry.remove(task)
}
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(`protocol`.properties[URLProtocol._PropertyKey.responseData] as? Data ?? Data(), task.response, nil)
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(`protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as? URL, task.response, nil)
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
}
task._protocol = nil
Expand Down Expand Up @@ -610,26 +616,30 @@ extension _ProtocolClient : URLProtocolClient {
session.delegateQueue.addOperation {
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
task.state = .completed
task.workQueue.async {
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
case .noDelegate:
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
session.taskRegistry.remove(task)
}
case .dataCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
task.workQueue.async {
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
case .downloadCompletionHandler(let completion):
session.delegateQueue.addOperation {
completion(nil, nil, error)
task.state = .completed
session.taskRegistry.remove(task)
session.workQueue.async {
session.taskRegistry.remove(task)
}
}
}
task._protocol = nil
Expand Down