Skip to content

Make sure writes to URLSession.taskRegistry happen on work queue #7

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

Closed
Closed
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func NSLocalizedString(_ key: String,
bundle: Bundle = Bundle.main,
value: String = "",
comment: String) -> String {
return bundle.localizedString(forKey: key, value: value, table: tableName)
#if os(Android)
return key
#else
return bundle.localizedString(forKey: key, value: value, table: tableName)
#endif
}

#if os(OSX) || os(iOS)
Expand Down
21 changes: 15 additions & 6 deletions Foundation/URLSession/TaskRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ extension URLSession._TaskRegistry {
func remove(_ task: URLSessionTask) {
let identifier = task.taskIdentifier
guard identifier != 0 else { fatalError("Invalid task identifier") }
guard let tasksIdx = tasks.index(forKey: identifier) else {
fatalError("Trying to remove task, but it's not in the registry.")

if let tasksIdx = tasks.index(forKey: identifier) {
tasks.remove(at: tasksIdx)
} else {
NSLog("Trying to remove task, but it's not in the registry.")
}
tasks.remove(at: tasksIdx)
guard let behaviourIdx = behaviours.index(forKey: identifier) else {
fatalError("Trying to remove task's behaviour, but it's not in the registry.")

if let behaviourIdx = behaviours.index(forKey: identifier) {
behaviours.remove(at: behaviourIdx)
} else {
NSLog("Trying to remove task's behaviour, but it's not in the registry.")
}
behaviours.remove(at: behaviourIdx)


guard let allTasksFinished = tasksFinishedCallback else { return }
if self.isEmpty {
Expand Down Expand Up @@ -115,4 +120,8 @@ extension URLSession._TaskRegistry {
}
return b
}

func containsBehaviour(for task: URLSessionTask) -> Bool {
return behaviours[task.taskIdentifier] != nil
}
}
4 changes: 4 additions & 0 deletions Foundation/URLSession/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ internal extension URLSession {
}

func behaviour(for task: URLSessionTask) -> _TaskBehaviour {
guard taskRegistry.containsBehaviour(for: task) else {
return .noDelegate
}

switch taskRegistry.behaviour(for: task) {
case .dataCompletionHandler(let c): return .dataCompletionHandler(c)
case .downloadCompletionHandler(let c): return .downloadCompletionHandler(c)
Expand Down
26 changes: 18 additions & 8 deletions Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -702,24 +702,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 {
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 @@ -786,26 +792,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
2 changes: 1 addition & 1 deletion Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class _HTTPURLProtocol: URLProtocol {
suspend()
} else {
self.internalState = .transferFailed
guard let error = self.task?.error else { fatalError() }
let error = self.task?.error ?? NSError(domain: NSURLErrorDomain, code: URLError.Code.unknown.rawValue)
completeTask(withError: error)
}
}
Expand Down