Replace TimeoutCancellationException with TimeoutException
See original GitHub issueIn the current version of the library, withTimeout
throws TimeoutCancellationException
when a timeout was exceeded.
It can lead to very subtle errors, for example:
launch {
val result = withTimeout(...) {
// ... some computation ...
}
// process result
}
TimeoutCancellationException
is CancellationException
, thus is never reported.
But in the snippet above, it’s likely to be a programmatic error. If it is expected to miss the deadline, then withTimeoutOrNull
should be used explicitly.
My proposal is deprecation of TimeoutCancellationException
and replacement with TimeoutException
that is not CancellationException
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:5 (3 by maintainers)
Top Results From Across the Web
TimeoutCancellationException when running tests for a ...
I ran into the same issue. I figured out all this was because of kotlin-coroutines-test 1.6.0 and more specifically the runTest behavior.
Read more >Cancellation and timeouts | Kotlin
The TimeoutCancellationException that is thrown by withTimeout is a subclass of CancellationException. We have not seen its stack trace ...
Read more >TimeoutException (Java Platform SE 8 ) - Oracle Help Center
public class TimeoutException extends Exception. Exception thrown when a blocking operation times out. Blocking operations for which a timeout is specified ...
Read more >Index - javadoc.io
TimeoutCancellationException (message) - Constructor for exception kotlinx.coroutines.TimeoutCancellationException. Creates timeout exception with a given ...
Read more >kotlinx-coroutines-testのrunBlockingTestについて - sky's 雑記
Replace TimeoutCancellationException with TimeoutException. 上述の suspend fun foo でTimeoutExceptionをそのままthrowせずにtry/catchしてFooExceptionを投げ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sure, we use
withTimeout
for restricting our inner functions with deadlines. So, any exceptions except CancellationException will be reported from our business logic code, but TimeoutCancellationException won’t. We want to parse such exceptions as others. And, as you said before, it would be possible if withTimeout threw TimeoutException instead of TimeoutCancellationException. Another case is when we use launch within coroutineScope. Exceded deadline in one job doesn’t cancel other jobs. Of course, we can use our own custom launch which catches TimeoutCancellationException inside and throws TimeoutException or we can use withTimeoutOrNull. But, we’d like to know if you plan to change the behavior of withTimeout function.Thank you!
The preliminary design decision is to deprecate
withTimeout
function with@LowPriorityInOverloadResolution
and re-introduce it in the brand new packagekotlinx.coroutines.time
with the same name.The change is now blocked by the IDE bug that doesn’t replace a signature to the same name in a different package