question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace TimeoutCancellationException with TimeoutException

See original GitHub issue

In 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:open
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
artbezcommented, Oct 16, 2020

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!

1reaction
qwwdfsadcommented, Nov 11, 2021

The preliminary design decision is to deprecate withTimeout function with @LowPriorityInOverloadResolution and re-introduce it in the brand new package kotlinx.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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found