Marc Lefrancois
08/31/2023, 9:04 PMprivate val sessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfiguration(identifier)
private val ktorDelegate = KtorNSURLSessionDelegate()
private val session = NSURLSession.sessionWithConfiguration(
sessionConfiguration,
ktorDelegate,
delegateQueue = null
)
protected val httpClient = HttpClient(Darwin) {
engine {
usePreconfiguredSession(session, ktorDelegate)
}
expectSuccess = true
}
Everything works fine as long as requests succeed. If a request returns for example a 404 response, a simple httpClient.get(<RequestWithURLThatReturns404>)
simply never returns
Changing sessionConfiguration to private val sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration
fixes the problem
Question 1: Anyone has ever encountered this problem and come up with a solution
Question 2: I would like to contribute a fix if it’s not the case, any pointers on where I could start in the ktor code baseMarc Lefrancois
08/31/2023, 9:05 PMdidReceiveData
and didCompleteWithError
Aleksei Tirman [JB]
09/01/2023, 5:24 AMMarc Lefrancois
09/01/2023, 12:24 PMAleksei Tirman [JB]
09/03/2023, 8:42 AMval sessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfiguration("identifier")
val ktorDelegate = KtorNSURLSessionDelegate()
val session = NSURLSession.sessionWithConfiguration(
sessionConfiguration,
ktorDelegate,
delegateQueue = null
)
val httpClient = HttpClient(Darwin) {
engine {
usePreconfiguredSession(session, ktorDelegate)
}
expectSuccess = true
}
@OptIn(DelicateCoroutinesApi::class)
fun main() {
val job = GlobalScope.launch {
httpClient.get("<https://example.com/404>")
}
while (job.isActive) {}
}
Is it only reproducible on iOS? Can you file an issue with a minimal sample project attached?Łukasz Zieliński
09/03/2023, 10:36 AMŁukasz Zieliński
09/06/2023, 2:07 PMval client = HttpClient() {
install(Logging) {
level = LogLevel.BODY
}
install(ContentNegotiation) {
json()
}
install(HttpTimeout) {
requestTimeoutMillis = 20000L
connectTimeoutMillis = 20000L
socketTimeoutMillis = 20000L
}
}
Marc Lefrancois
09/06/2023, 2:25 PMexpectSuccess = true
that should threat 404 response has errors for the flow ?
As for my problem @Aleksei Tirman [JB] I can now confirm that it was a setup problem on our side combine with an unexpected (bug?) behaviour of backgroundSessionConfiguration
. I’ll try to explain clearly, do not hesitate if it is not clear enough
I was by error using 2 backgroundSessionConfiguration()
with the same identifier
in 2 different instance of HttpClient
. So 2 URLSession
instances , with 2 KtorNSURLSessionDelegate
instances.
1. When the requests are successful, no problems.
2. When the requests have an error status code (like 404). The following happened using the second HttpClient:
a. When the request is initiated, read
is called on the right KtorDelegate
instance
b. When the urlSession calls back to the delegate for didReceiveData
and didCompleteWithError
it does so on the other KtorDelegate
instance. So there is no NSURLSessionTask
in the taskHandlers
map and the flow never emits anything.Marc Lefrancois
09/06/2023, 2:26 PMNSURLSessionConfiguration.backgroundSessionConfiguration
returns shared configuration per identifier and that there is a mechanism to handle the multiple delegatesMarc Lefrancois
09/06/2023, 2:26 PMMarc Lefrancois
09/06/2023, 2:28 PM