Skip to content

[SR-2798] Deadlock in URLSession if finishTasksAndInvalidate() was called #4325

Closed
@swift-ci

Description

@swift-ci
Previous ID SR-2798
Radar None
Original Reporter gal (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee gal (JIRA)
Priority Medium

md5: dc4437b959353fe64f9f8bcaa344526a

Issue Description:

The problem is in the current implementation of finishTasksAndInvalidate, if there're executing tasks in session - it will create a semaphore, that will block session work queue, but to release, it has to enter that queue again.

Consider following code:

class SessionDelegate: NSObject, URLSessionDelegate {
    func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
        // THIS WILL NEVER BE CALLED
    }
}

let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
let url = URL(string: "https://bugs.swift.org")!
let session = URLSession(configuration: URLSessionConfiguration.default,
                         delegate: delegate, delegateQueue: nil)
let task = session.dataTask(with: url) { data, response, error in
    // THIS WILL NEVER BE EXECUTED
}
task.resume()
session.finishTasksAndInvalidate()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions