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-11602] URLSessionDataTasks that encounter a 401 error won't call completionHandler on Linux #3385

Open
swift-ci opened this issue Oct 11, 2019 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-11602
Radar None
Original Reporter Sephiroth87 (JIRA User)
Type Bug
Status Reopened
Resolution
Environment

Swift version 5.1 (swift-5.1-RELEASE)

Target: x86_64-unknown-linux-gnu

Tested on docker using swift:5.1 image

Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: c519a9ec862920bbb3a54722a4e53bec

Issue Description:

Sample code to reproduce the issue:

import Foundation
import FoundationNetworking


print("yeah")


//let url = URL(string: "https://httpstat.us/200")!
let url = URL(string: "https://httpstat.us/401")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    print(response as Any)
    exit(0)
}
task.resume()


RunLoop.current.run()

When running with the first url, the program correctly prints the response and exits.

When using the second one, which returns a 401, the completion handler is never called and the program never exits.

Other error codes will also call the completion handler

It may be related or not, but if using a data task without completion handlers and URLSessionTaskDelegate, similarly urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) is never called.

(I didn't have time to make sample code for this, might add later if needed)

@swift-ci
Copy link
Contributor Author

Comment by Fabio Ritrovato (JIRA)

Hello?

@spevans
Copy link
Collaborator

spevans commented Mar 18, 2020

This is now fixed in master. It was fixed by this PR #2728

@swift-ci
Copy link
Contributor Author

Comment by Lasse Jansen (JIRA)

This bug is back with Swift 5.4 on Linux, the example worked with 5.3.

@swift-ci
Copy link
Contributor Author

Comment by Lasse Jansen (JIRA)

It's possible to use a delegate as a workaround:

private class DummyDelegate: NSObject, URLSessionTaskDelegate {
  let httpAuthMethods = [
    NSURLAuthenticationMethodDefault,
    NSURLAuthenticationMethodHTTPBasic,
    NSURLAuthenticationMethodHTTPDigest,
  ]

  func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    if httpAuthMethods.contains(challenge.protectionSpace.authenticationMethod) {
        completionHandler(.cancelAuthenticationChallenge, nil)
    } else {
        completionHandler(.performDefaultHandling, nil)
    }
  }
}

@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
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

2 participants