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

[SR-13017] error returned by URLSessionTask.cancel() differs on mac and linux #3250

Closed
glessard opened this issue Jun 15, 2020 · 2 comments
Closed
Assignees

Comments

@glessard
Copy link
Contributor

Previous ID SR-13017
Radar None
Original Reporter @glessard
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

Swift 5.2.4 on macOS (Xcode 11.5/macOS 10.15) and Linux (ubuntu 18.04)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @glessard
Priority Medium

md5: d1ad5b8e981926caf0d4c6c32f6eebdf

Issue Description:

When one cancels a URLSessionDataTask before it even starts, a `URLError` is passed to the callback. The `userInfo` field is blank on Linux, in an unnecessary inconsistency.

import Dispatch
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

let session = URLSession(configuration: .default)
defer { session.finishTasksAndInvalidate() }

let s = DispatchSemaphore(value: 0)

let task = session.dataTask(with: URL(string: "https://www.swift.org")!) {
  (_, _, error) in
  let e = error as! URLError
  let i = e.userInfo
  i.keys.sorted().forEach { print("\($0): \(i[$0]!)") }
  print("\(i.count) key-value pair\(i.count == 1 ? "" : "s")")

  s.signal()
}

task.cancel()
s.wait()

On macOS:

$ swift --version && swift urlsessiontask-cancellation.swift
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin19.5.0
NSErrorFailingURLKey: https://www.swift.org
NSErrorFailingURLStringKey: https://www.swift.org
NSLocalizedDescription: cancelled
3 key-value pairs

On Linux:

$ swift --version && swift urlsessiontask-cancellation.swift 
Swift version 5.2.4 (swift-5.2.4-RELEASE)
Target: x86_64-unknown-linux-gnu
0 key-value pairs
@glessard
Copy link
Contributor Author

swift-corelibs-foundation is generally inconsistent about how it builds its URLError instances. Some include the URL, some also include NSLocalizedDescription, some include nothing. As a result, user code cannot rely on them for context.

@glessard
Copy link
Contributor Author

Fixed in #2825

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant