8000 Hanging when using preconfigured NSURLSession in last version (3.1.3) · Issue #4888 · ktorio/ktor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Hanging when using preconfigured NSURLSession in last version (3.1.3) #4888

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

Closed
timnazar opened this issue May 29, 2025 · 5 comments
Closed

Comments

@timnazar
Copy link
timnazar commented May 29, 2025

Hey there

the requests are hanging when I'm passing my session and delegate

HttpClient(Darwin) {
    engine {
          usePreconfiguredSession(
               session = session,
               delegate = delegate
          )
          configureRequest {
              setAllowsCellularAccess(true)
          }
    }
}

It even doesn't call my KtorURLSessionDelegate

val bridgedDelegate: KtorNSURLSessionDelegate = KtorNSURLSessionDelegate { session, task, challenge, completionHandler ->
    Napier.d("challenge handler invoked", tag = "MEOWD")
}

        KodiKmpInitializer.initialize(
            data = data,
            componentRegistries = listOf(
                IosKtorComponentRegistry(
                    session = session,
                    delegate = bridgedDelegate
                ),
                RemoteConfigComponentRegistry(
                    appLanguageProvider = data.appLanguageProvider,
                    cacheDirPath = data.platformData.cacheDirPath,
                ),
            ),
        )

any tips for what can be the cause of the hanging? i also tried to pass NSURLSessionDelegate from my swift code (it crashes the app)

@timnazar
Copy link
Author
timnazar commented May 29, 2025

Also tried this one. But response was never returned

val config = NSURLSessionConfiguration.defaultSessionConfiguration()
val newSession = NSURLSession.sessionWithConfiguration(config)
usePreconfiguredSession(
     session = newSession,
     delegate = delegate
)

and my delegate is

val bridgedDelegate: KtorNSURLSessionDelegate = KtorNSURLSessionDelegate { session, task, challenge, completionHandler ->
    Napier.d("challenge handler invoked", tag = "MEOWD")
    completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge.toLong(), null)
}

@osipxd
Copy link
Member
osipxd commented May 29, 2025

Are you able to reproduce this behavior if you wire the delegate with the session by passing it as a parameter to the NSURLSession.sessionWithConfiguration?

 val config = NSURLSessionConfiguration.defaultSessionConfiguration()
-val newSession = NSURLSession.sessionWithConfiguration(config)
+val newSession = NSURLSession.sessionWithConfiguration(config, delegate, delegateQueue = null)
 usePreconfiguredSession(
      session = newSession,
      delegate = delegate
 )

@timnazar
Copy link
Author
timnazar commented May 29, 2025

thanks for your reply, @osipxd

it calls my ktor delegate with suggested method. but now i'm wondering how to pass my swift session with swift delegate

is it possible?

actually, i'm handling my ssl pinning in swift`s delegate

urlSession(_ session: URLSession,
                           didReceive challenge: URLAuthenticationChallenge,
                           completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

so ktor should call this method in swift implementation and receive the completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void and continue handling the response from the api

@osipxd
Copy link
Member
osipxd commented May 29, 2025

Does method DarwinClientEngineConfig.handleChallenge(block: ChallengeHandler) fits your needs?
Here is an example how this method can be used with Ktor's CertificatePinner:

HttpClient(Darwin) {
    engine {
        val pinningHandler = CertificatePinner.Builder()
            .add("example.com", "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
            .build()
        handleChallenge(pinningHandler)
    }
}

@timnazar
Copy link
Author

much appreciate, Osip.
your first suggestion fits better in my case
i implemented KtorNSURLSessionDelegate with challenger and resolved everything i wanted
have a great day

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

2 participants
0