Does Task cancellation propagate to URLSessionTasks?

If I have a Swift Task like this:

let task = Task {
  let (data, response) = try await URL(string: "")!)
  // ...

And then I do task.cancel(), does URLSession automatically cancel the URLSessionTask as well? It seems careless to assume that the URLSessionTask is cancelled too, but I can't find any documentation on this matter. However, if the URLSessionTask is not automatically cancelled, then I'm forced to use the closure-based URLSession.dataTask(with:,completionHandler:) to get a reference to the URLSessionTask, and then to also call something like dataTask.cancel() on top of task.cancel(), which is somewhat odd.


Yes. Consider this test code:

import Foundation

func main() async {
    let t = Task {
        do {
            print("\( task will start")
            let url = URL(string: "")!
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
            let (data, response) = try await request)
            print("\( task did complete, response: \(response), data: \(data)")
        } catch {
            print("\( task did error")
    if true {
        try? await Task.sleep(for: .seconds(1))
        print("\( will cancel task")
        print("\( did cancel task")
    print("\( waiting")
    try? await Task.sleep(for: .seconds(60))

await main()

It prints:

2023-05-18 08:18:52 +0000: task will start
2023-05-18 08:18:53 +0000: will cancel task
2023-05-18 08:18:53 +0000: did cancel task
2023-05-18 08:18:53 +0000: waiting
2023-05-18 08:18:53 +0000: task did error

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple