Skip to content

URLSessionTask retain cycle fix #1203

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
Sep 6, 2017
Merged
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
32 changes: 26 additions & 6 deletions Foundation/URLSession/URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ open class URLSessionTask : NSObject, NSCopying {
internal var suspendCount = 1
internal var session: URLSessionProtocol! //change to nil when task completes
internal let body: _Body
fileprivate var _protocol: URLProtocol! = nil
fileprivate var _protocol: URLProtocol? = nil

/// All operations must run on this queue.
internal let workQueue: DispatchQueue
/// Using dispatch semaphore to make public attributes thread safe.
Expand Down Expand Up @@ -200,8 +200,8 @@ open class URLSessionTask : NSObject, NSCopying {
self.workQueue.async {
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil))
self.error = urlError
self._protocol.stopLoading()
self._protocol.client?.urlProtocol(self._protocol, didFailWithError: urlError)
self._protocol?.stopLoading()
self._protocol?.client?.urlProtocol(self._protocol!, didFailWithError: urlError)
}
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ open class URLSessionTask : NSObject, NSCopying {

if self.suspendCount == 1 {
self.workQueue.async {
self._protocol.stopLoading()
self._protocol?.stopLoading()
}
}
}
Expand All @@ -278,7 +278,21 @@ open class URLSessionTask : NSObject, NSCopying {
self.updateTaskState()
if self.suspendCount == 0 {
self.workQueue.async {
self._protocol.startLoading()
if let _protocol = self._protocol {
_protocol.startLoading()
}
else if self.error == nil {
var userInfo: [String: Any] = [NSLocalizedDescriptionKey: "unsupported URL"]
if let url = self.originalRequest?.url {
userInfo[NSURLErrorFailingURLErrorKey] = url
userInfo[NSURLErrorFailingURLStringErrorKey] = url.absoluteString
}
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain,
code: NSURLErrorUnsupportedURL,
userInfo: userInfo))
self.error = urlError
_ProtocolClient().urlProtocol(task: self, didFailWithError: urlError)
}
}
}
}
Expand Down Expand Up @@ -573,6 +587,7 @@ extension _ProtocolClient : URLProtocolClient {
session.taskRegistry.remove(task)
}
}
task._protocol = nil
}

func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
Expand Down Expand Up @@ -600,6 +615,10 @@ extension _ProtocolClient : URLProtocolClient {

func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
guard let task = `protocol`.task else { fatalError() }
urlProtocol(task: task, didFailWithError: error)
}

func urlProtocol(task: URLSessionTask, didFailWithError error: Error) {
guard let session = task.session as? URLSession else { fatalError() }
switch session.behaviour(for: task) {
case .taskDelegate(let delegate):
Expand All @@ -624,6 +643,7 @@ extension _ProtocolClient : URLProtocolClient {
session.taskRegistry.remove(task)
}
}
task._protocol = nil
}

func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {
Expand Down
12 changes: 5 additions & 7 deletions Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ import Dispatch
internal class _HTTPURLProtocol: URLProtocol {

fileprivate var easyHandle: _EasyHandle!
fileprivate var tempFileURL: URL
fileprivate lazy var tempFileURL: URL = {
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
return URL(fileURLWithPath: fileName)
}()

public required init(task: URLSessionTask, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
self.internalState = _InternalState.initial
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init(request: task.originalRequest!, cachedResponse: cachedResponse, client: client)
self.task = task
self.easyHandle = _EasyHandle(delegate: self)
}

public required init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
self.internalState = _InternalState.initial
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init(request: request, cachedResponse: cachedResponse, client: client)
self.easyHandle = _EasyHandle(delegate: self)
}
Expand Down