You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Invocation of [resume] or [resumeWithException] in _resumed_ state produces [IllegalStateException]
* but is ignored in _cancelled_ state.
The part about "ignored in cancelled state" doesn't seem to be true in practice. Running the below code allows me to catch the JobCancellationException but still prints a stack trace of the IOException thrown.
funmain(args:Array<String>) {
val client =OkHttpClient()
val request =Request.Builder()
.url("http://httpbin.org/get")
.build()
val call = client.newCall(request)
try {
val inResponse =CountDownLatch(1)
val continueReading =CountDownLatch(1)
val job =GlobalScope.launch(Dispatchers.Default) {
val result:String=try {
call.await {
inResponse.countDown()
continueReading.await()
throwIOException("IO")
}
} catch (e:Exception) {
"${e.javaClass} was caught"
}
println("result is [$result]")
}
inResponse.await()
job.cancel()
continueReading.countDown()
Thread.sleep(5000)
} finally {
client.dispatcher().executorService().shutdown()
}
}
suspendfun <T> Call.await(responseConverter: (Response) ->T): T {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
this@await.cancel()
}
enqueue(object:Callback {
overridefunonResponse(call:Call, response:Response) {
try {
val result = response.use(responseConverter)
continuation.resume(result)
} catch (e:Exception) {
println("Calling resumeWithException with ${e.javaClass}")
continuation.resumeWithException(e)
}
}
overridefunonFailure(call:Call, e:IOException) {
continuation.resumeWithException(e)
}
})
}
}
Following the path from resumeWithException leads to this code in AbstractContinuation:
The docs are incorrect. Whether it has to be done depends on on what kind of API you are integrating with. In case of Retrofit, it invoke callback when you cancel it and reports IOException, so this check is needed. We'll improve docs and we also plan to provide some better ways to do in the future.
The docs for
CancellableContinuation
state:kotlinx.coroutines/common/kotlinx-coroutines-core-common/src/CancellableContinuation.kt
Lines 31 to 32 in 52bfdab
The part about "ignored in cancelled state" doesn't seem to be true in practice. Running the below code allows me to catch the
JobCancellationException
but still prints a stack trace of theIOException
thrown.Following the path from
resumeWithException
leads to this code inAbstractContinuation
:kotlinx.coroutines/common/kotlinx-coroutines-core-common/src/AbstractContinuation.kt
Lines 160 to 171 in 52bfdab
Are the docs incorrect? Am I doing something wrong? I see in https://github.com/gildor/kotlin-coroutines-okhttp/blob/master/src/main/kotlin/ru/gildor/coroutines/okhttp/CallAwait.kt#L23 they check cancellation before calling
resumeWithException
. Is that something that always needs to be done?The text was updated successfully, but these errors were encountered: