Skip to content

Commit 3c4168b

Browse files
committed
Throw TimeoutCancellationException instead of CancellationException on negative timeout in withTimeout
Fixes #870
1 parent a5b6a33 commit 3c4168b

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

common/kotlinx-coroutines-core-common/src/Timeout.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import kotlin.jvm.*
2525
* @param timeMillis timeout time in milliseconds.
2626
*/
2727
public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineScope.() -> T): T {
28-
if (timeMillis <= 0L) throw CancellationException("Timed out immediately")
28+
if (timeMillis <= 0L) throw TimeoutCancellationException("Timed out immediately")
2929
return suspendCoroutineUninterceptedOrReturn { uCont ->
3030
setupTimeout(TimeoutCoroutine(timeMillis, uCont), block)
3131
}

common/kotlinx-coroutines-core-common/test/WithTimeoutTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class WithTimeoutTest : TestBase() {
175175
expectUnreached()
176176
"OK"
177177
}
178-
} catch (e: CancellationException) {
178+
} catch (e: TimeoutCancellationException) {
179179
assertEquals("Timed out immediately", e.message)
180180
finish(2)
181181
}

0 commit comments

Comments
 (0)