We have a mobile app that allows the user to switc...
# ktor
t
We have a mobile app that allows the user to switch to a different user on-the-fly, which sets a session cookie. A problem with this, though, is that requests that are waiting to be dispatched will be sent with the old cookie, result in the server returning a 401 Unauthorized and clearing the cookie. I tried canceling and waiting for all requests to finish with this code:
Copy code
httpClient.coroutineContext.job.children.forEach { it.cancelAndJoin() }
But that doesn't seem to do what I expect, which is to suspend until all in-flight requests are canceled and completed. Another idea is to cancel the job entirely and construct a new HttpClient, but I'd like to avoid this if possible as it means reworking quite a bit of DI architecture. Does anyone here have any ideas how I can cancel all requests that are in-flight?
a
You can try to cancel the in-flight requests by cancelling the coroutine scope. Here is an example:
Copy code
val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)

scope.launch {
    client.get("<https://httpbin.org/delay/10>")
    client.get("<https://httpbin.org/delay/10>")
}

// ...
scope.cancel()