Gleb Minaev
05/17/2026, 9:50 PMCancellableContinuation to ignore onCancellation argument in its method
public fun R: T resume(
value: R, onCancellation: ((cause: Throwable, value: R, context: CoroutineContext) -> Unit)?
)
when some CompletionHandler is set via
public fun invokeOnCancellation(handler: CompletionHandler)
? Is it a bug or a feature? I did not find any mention of such behaviour in docs of kotlinx.coroutines.
Here is my situation. I wrote my own Mutex implementation (just for fun). And I try to debug it with Lincheck. I just tried to add prints in lambdas of both operations like this:
continuation.invokeOnCancellation {
println("invokeOnCancellation!")
// ...
}
//...
continuation.resume(...) { _, _, _ ->
println("onCancellation!")
// ...
}
When I run Lincheck tests, I see only a pile of invokeOnCancellation!. No onCancellation! is printed. But if comment the only usage of invokeOnCancellation then only a pile of onCancellation! is printed. So I try to understand what's going on. And it looks to me like an issue above.
UPD. No, CancellableContinuation does not ignore the argument. It's just some strange things happening in my program. Solved.