https://kotlinlang.org logo
#ktor
Title
# ktor
m

Marc Knaup

04/02/2020, 2:05 PM
Is it normal that
Job.cancel()
throws a
CancellationException
itself? Or is this a bug in Ktor?
Copy code
java.util.concurrent.CancellationException: Job was cancelled
    at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Utils.kt:77)
    at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Unknown Source:2)
    …
    at kotlinx.coroutines.JobSupport.cancel(JobSupport.kt:614)
    at kotlinx.coroutines.Job$DefaultImpls.cancel$default(Job.kt:164)
e

elizarov

04/07/2020, 3:27 PM
Looks like a bug.
1
m

Marc Knaup

04/10/2020, 2:20 AM
@luke yes, but the call that's canceled should throw the exception, not the code that initiates the cancelation.
l

luke

04/10/2020, 2:27 AM
Does this not indicate that an instance of
Job
can throw a
CancellationException
when calling
cancel()
? https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html
m

Marc Knaup

04/10/2020, 2:30 AM
No, it doesn't
l

luke

04/10/2020, 2:39 AM
Nope, you're right, sorry. I have no idea then.
o

olegivo

02/14/2023, 1:29 PM
It must be a
kotlin.coroutines.cancellation.CancellationException
but was
java.util.concurrent.CancellationException
Something went wrong. I have this bug now
to work around this behavior I use this construction in HttpResponseValidator block:
Copy code
handleResponseExceptionWithRequest { exception, _ ->
    when (exception) {
        is java.util.concurrent.CancellationException -> throw kotlin.coroutines.cancellation.CancellationException(exception)
        else -> throw exception
    }
}
Maybe, the problem is in linking wrong exception type
201 Views