Skip to content
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

URLSession delegate method urlSession(_:task:didFinishCollecting:) is not invoked on Linux #4988

Open
johnnzhou opened this issue Jun 22, 2024 · 0 comments

Comments

@johnnzhou
Copy link

URLSession delegate method urlSession(_:task:didFinishCollecting:) is not invoked on Linux platform.

Steps to reproduce:

Given the following code snippet, the program will hang forever, since the didFinishCollecting delegate method is not called.

import Foundation
import FoundationNetworking

let group = DispatchGroup()
group.enter()

class URLSessionClient: NSObject {
    private lazy var session: URLSession = {
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
        return session
    }() 

    func start() {
        let url = URL(string: "https://www.apple.com")
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        let dataTask = session.downloadTask(with: request)
        dataTask.resume()
    }
}

extension URLSessionClient: URLSessionDelegate, URLSessionDataDelegate, URLSessionTaskDelegate {
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: (any Error)?) {
        print("Complete")
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
        guard let metric = metrics.transactionMetrics.last else {
            print("No metric collected. Exit")
            return
        }
        group.leave()
        print("Metric: \(metric)")
    }
}

DispatchQueue.global().async {
    let client = URLSessionClient()
    client.start()
}
group.wait()

Platform

Ubuntu 22.04.4 LTS

Swift version

Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant