Skip to content

[SR-2798] Fix deadlock in URLSession. #662

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 30, 2016
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
22 changes: 12 additions & 10 deletions Foundation/NSURLSession/NSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,20 @@ open class URLSession : NSObject {
//don't allow creation of new tasks from this point onwards
self.invalidated = true

//wait for running tasks to finish
if !self.taskRegistry.isEmpty {
let tasksCompletion = DispatchSemaphore(value: 0)
self.taskRegistry.notify(on: tasksCompletion)
tasksCompletion.wait()
let invalidateSessionCallback = { [weak self] in
//invoke the delegate method and break the delegate link
guard let `self` = self, let sessionDelegate = self.delegate else { return }
self.delegateQueue.addOperation {
sessionDelegate.urlSession(self, didBecomeInvalidWithError: nil)
self.delegate = nil
}
}

//invoke the delegate method and break the delegate link
guard let sessionDelegate = self.delegate else { return }
self.delegateQueue.addOperation {
sessionDelegate.urlSession(self, didBecomeInvalidWithError: nil)
self.delegate = nil
//wait for running tasks to finish
if !self.taskRegistry.isEmpty {
self.taskRegistry.notify(on: invalidateSessionCallback)
} else {
invalidateSessionCallback()
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSURLSession/TaskRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension URLSession {

fileprivate var tasks: [Int: URLSessionTask] = [:]
fileprivate var behaviours: [Int: _Behaviour] = [:]
fileprivate var tasksFinished: DispatchSemaphore?
fileprivate var tasksFinishedCallback: (() -> ())?
}
}

Expand Down Expand Up @@ -81,14 +81,14 @@ extension URLSession._TaskRegistry {
}
behaviours.remove(at: behaviourIdx)

guard let allTasksFinished = tasksFinished else { return }
guard let allTasksFinished = tasksFinishedCallback else { return }
if self.isEmpty {
allTasksFinished.signal()
allTasksFinished()
}
}

func notify(on semaphore: DispatchSemaphore) {
tasksFinished = semaphore
func notify(on tasksCompetion: @escaping () -> ()) {
tasksFinishedCallback = tasksCompetion
}

var isEmpty: Bool {
Expand Down
27 changes: 26 additions & 1 deletion TestFoundation/TestNSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestURLSession : XCTestCase {
("test_downloadTaskWithURLRequest", test_downloadTaskWithURLRequest),
("test_downloadTaskWithRequestAndHandler", test_downloadTaskWithRequestAndHandler),
("test_downloadTaskWithURLAndHandler", test_downloadTaskWithURLAndHandler),

("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate)
]
}

Expand Down Expand Up @@ -247,6 +247,31 @@ class TestURLSession : XCTestCase {
task.resume()
waitForExpectations(timeout: 12)
}

func test_finishTasksAndInvalidate() {
let invalidateExpectation = expectation(description: "URLSession wasn't invalidated")
let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
let url = URL(string: "http://127.0.0.1:\(serverPort)/Nepal")!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: delegate, delegateQueue: nil)
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
let task = session.dataTask(with: url) { _ in
completionExpectation.fulfill()
}
task.resume()
session.finishTasksAndInvalidate()
waitForExpectations(timeout: 12)
}
}

class SessionDelegate: NSObject, URLSessionDelegate {
let invalidateExpectation: XCTestExpectation
init(invalidateExpectation: XCTestExpectation){
self.invalidateExpectation = invalidateExpectation
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
invalidateExpectation.fulfill()
}
}

class DataTask : NSObject {
Expand Down