Skip to content

Commit 7267280

Browse files
committed
Use TimeoutException instead of TimeoutCancellationException
1 parent 4e8e608 commit 7267280

24 files changed

+76
-52
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
260260
val firstNonCancellation = exceptions.firstOrNull { it !is CancellationException }
261261
if (firstNonCancellation != null) return firstNonCancellation
262262
val first = exceptions[0]
263+
@Suppress("DEPRECATION")
263264
if (first is TimeoutCancellationException) {
264265
val detailedTimeoutException = exceptions.firstOrNull { it !== first && it is TimeoutCancellationException }
265266
if (detailedTimeoutException != null) return detailedTimeoutException

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

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineSco
4848
contract {
4949
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
5050
}
51+
@Suppress("DEPRECATION")
5152
if (timeMillis <= 0L) throw TimeoutCancellationException("Timed out immediately")
5253
return suspendCoroutineUninterceptedOrReturn { uCont ->
5354
setupTimeout(TimeoutCoroutine(timeMillis, uCont), block)
@@ -113,6 +114,7 @@ public suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend Corout
113114
if (timeMillis <= 0L) return null
114115

115116
var coroutine: TimeoutCoroutine<T?, T?>? = null
117+
@Suppress("DEPRECATION")
116118
try {
117119
return suspendCoroutineUninterceptedOrReturn { uCont ->
118120
val timeoutCoroutine = TimeoutCoroutine(timeMillis, uCont)
@@ -179,6 +181,11 @@ private class TimeoutCoroutine<U, in T: U>(
179181
/**
180182
* This exception is thrown by [withTimeout] to indicate timeout.
181183
*/
184+
@Deprecated("Use TimeoutException from the 'kotlinx-coroutines-time' package instead.",
185+
ReplaceWith("kotlinx.coroutines.time.TimeoutException",
186+
"kotlinx.coroutines.time"),
187+
level = DeprecationLevel.WARNING)
188+
@Suppress("DEPRECATION")
182189
public class TimeoutCancellationException internal constructor(
183190
message: String,
184191
@JvmField @Transient internal val coroutine: Job?
@@ -195,6 +202,7 @@ public class TimeoutCancellationException internal constructor(
195202
TimeoutCancellationException(message ?: "", coroutine).also { it.initCause(this) }
196203
}
197204

205+
@Suppress("DEPRECATION")
198206
internal fun TimeoutCancellationException(
199207
time: Long,
200208
coroutine: Job

kotlinx-coroutines-core/common/src/flow/operators/Delay.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.*
1111
import kotlinx.coroutines.channels.*
1212
import kotlinx.coroutines.flow.internal.*
1313
import kotlinx.coroutines.selects.*
14+
import kotlinx.coroutines.time.*
1415
import kotlin.jvm.*
1516
import kotlin.time.*
1617

@@ -346,7 +347,7 @@ internal fun CoroutineScope.fixedPeriodTicker(delayMillis: Long, initialDelayMil
346347
public fun <T> Flow<T>.sample(period: Duration): Flow<T> = sample(period.toDelayMillis())
347348

348349
/**
349-
* Returns a flow that will emit a [TimeoutCancellationException] if the upstream doesn't emit an item within the given time.
350+
* Returns a flow that will emit a [TimeoutException] if the upstream doesn't emit an item within the given time.
350351
*
351352
* Example:
352353
*
@@ -386,7 +387,7 @@ public fun <T> Flow<T>.timeout(
386387
private fun <T> Flow<T>.timeoutInternal(
387388
timeout: Duration
388389
): Flow<T> = scopedFlow { downStream ->
389-
if (timeout <= Duration.ZERO) throw TimeoutCancellationException("Timed out immediately")
390+
if (timeout <= Duration.ZERO) throw TimeoutException("Timed out immediately")
390391
val values = buffer(Channel.RENDEZVOUS).produceIn(this)
391392
whileSelect {
392393
values.onReceiveCatching { value ->
@@ -398,7 +399,7 @@ private fun <T> Flow<T>.timeoutInternal(
398399
return@onReceiveCatching true
399400
}
400401
onTimeout(timeout) {
401-
throw TimeoutCancellationException("Timed out waiting for $timeout")
402+
throw TimeoutException("Timed out waiting for $timeout")
402403
}
403404
}
404405
}

kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal fun <T> (suspend () -> T).startCoroutineUnintercepted(completion: Conti
2727
* It does not use [ContinuationInterceptor] and does not update the context of the current thread.
2828
*/
2929
internal fun <R, T> (suspend (R) -> T).startCoroutineUnintercepted(receiver: R, completion: Continuation<T>) {
30-
startDirect(completion) { actualCompletion ->
30+
startDirect(completion) { actualCompletion ->
3131
startCoroutineUninterceptedOrReturn(receiver, actualCompletion)
3232
}
3333
}
@@ -97,7 +97,9 @@ internal fun <T, R> ScopeCoroutine<T>.startUndispatchedOrReturn(receiver: R, blo
9797
internal fun <T, R> ScopeCoroutine<T>.startUndispatchedOrReturnIgnoreTimeout(
9898
receiver: R, block: suspend R.() -> T
9999
): Any? {
100-
return undispatchedResult({ e -> !(e is TimeoutCancellationException && e.coroutine === this) }) {
100+
return undispatchedResult({ e ->
101+
@Suppress("DEPRECATION") !(e is TimeoutCancellationException && e.coroutine === this)
102+
}) {
101103
block.startCoroutineUninterceptedOrReturn(receiver, this)
102104
}
103105
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package kotlinx.coroutines
88

9+
import kotlinx.coroutines.time.*
910
import kotlin.test.*
1011
import kotlin.time.*
1112
import kotlin.time.Duration.Companion.milliseconds
@@ -177,7 +178,7 @@ class WithTimeoutDurationTest : TestBase() {
177178
expectUnreached()
178179
"OK"
179180
}
180-
} catch (e: TimeoutCancellationException) {
181+
} catch (e: TimeoutException) {
181182
assertEquals("Timed out immediately", e.message)
182183
finish(2)
183184
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package kotlinx.coroutines
99

1010
import kotlinx.coroutines.channels.*
11+
import kotlinx.coroutines.time.*
1112
import kotlin.test.*
1213
import kotlin.time.*
1314
import kotlin.time.Duration.Companion.milliseconds
@@ -117,7 +118,7 @@ class WithTimeoutOrNullDurationTest : TestBase() {
117118
}
118119

119120
@Test
120-
fun testNestedTimeout() = runTest(expected = { it is TimeoutCancellationException }) {
121+
fun testNestedTimeout() = runTest(expected = { it is TimeoutException }) {
121122
withTimeoutOrNull(Duration.INFINITE) {
122123
// Exception from this withTimeout is not suppressed by withTimeoutOrNull
123124
kotlinx.coroutines.time.withTimeout(10.milliseconds) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package kotlinx.coroutines
99

1010
import kotlinx.coroutines.channels.*
11+
import kotlinx.coroutines.time.*
1112
import kotlin.test.*
1213

1314
class WithTimeoutOrNullTest : TestBase() {
@@ -114,7 +115,7 @@ class WithTimeoutOrNullTest : TestBase() {
114115
}
115116

116117
@Test
117-
fun testNestedTimeout() = runTest(expected = { it is TimeoutCancellationException }) {
118+
fun testNestedTimeout() = runTest(expected = { it is TimeoutException }) {
118119
withTimeoutOrNull(Long.MAX_VALUE) {
119120
// Exception from this withTimeout is not suppressed by withTimeoutOrNull
120121
kotlinx.coroutines.time.withTimeout(10) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package kotlinx.coroutines
99

10+
import kotlinx.coroutines.time.*
1011
import kotlin.test.*
1112

1213
class WithTimeoutTest : TestBase() {
@@ -169,7 +170,7 @@ class WithTimeoutTest : TestBase() {
169170
expectUnreached()
170171
"OK"
171172
}
172-
} catch (e: TimeoutCancellationException) {
173+
} catch (e: TimeoutException) {
173174
assertEquals("Timed out immediately", e.message)
174175
finish(2)
175176
}

kotlinx-coroutines-core/common/test/flow/channels/ChannelFlowTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.coroutines.flow
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*
9+
import kotlinx.coroutines.time.*
910
import kotlin.test.*
1011

1112
class ChannelFlowTest : TestBase() {
@@ -137,7 +138,7 @@ class ChannelFlowTest : TestBase() {
137138

138139
val flow = flowOf(1, 2, 3).bufferWithTimeout()
139140
expect(1)
140-
assertFailsWith<TimeoutCancellationException>(flow)
141+
assertFailsWith<TimeoutException>(flow)
141142
finish(6)
142143
}
143144

kotlinx-coroutines-core/common/test/flow/operators/DebounceTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.coroutines.flow
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*
9+
import kotlinx.coroutines.time.*
910
import kotlin.test.*
1011
import kotlin.time.Duration.Companion.milliseconds
1112

@@ -96,10 +97,10 @@ class DebounceTest : TestBase() {
9697
}
9798

9899
@Test
99-
fun testUpstreamError()= testUpstreamError(TimeoutCancellationException(""))
100+
fun testUpstreamError()= testUpstreamError(TimeoutException(""))
100101

101102
@Test
102-
fun testUpstreamErrorCancellation() = testUpstreamError(TimeoutCancellationException(""))
103+
fun testUpstreamErrorCancellation() = testUpstreamError(TimeoutException(""))
103104

104105
private inline fun <reified T: Throwable> testUpstreamError(cause: T) = runTest {
105106
val latch = Channel<Unit>()

kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.coroutines.flow
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*
9+
import kotlinx.coroutines.time.*
910
import kotlin.test.*
1011

1112
class FlowOnTest : TestBase() {
@@ -244,7 +245,7 @@ class FlowOnTest : TestBase() {
244245
}.flowOn(NamedDispatchers("foo")).onEach {
245246
expect(1)
246247
}
247-
assertFailsWith<TimeoutCancellationException>(flow)
248+
assertFailsWith<TimeoutException>(flow)
248249
finish(2)
249250
}
250251

@@ -257,7 +258,7 @@ class FlowOnTest : TestBase() {
257258
expect(1)
258259
kotlinx.coroutines.time.withTimeout(-1) {}
259260
}
260-
assertFailsWith<TimeoutCancellationException>(flow)
261+
assertFailsWith<TimeoutException>(flow)
261262
finish(3)
262263
}
263264

kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.coroutines.flow.operators
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.flow.*
9+
import kotlinx.coroutines.time.*
910
import kotlin.test.*
1011
import kotlin.time.Duration.Companion.milliseconds
1112

@@ -27,7 +28,7 @@ class TimeoutTest : TestBase() {
2728

2829
expect(2)
2930
val list = mutableListOf<String>()
30-
assertFailsWith<TimeoutCancellationException>(flow.timeout(300.milliseconds).onEach { list.add(it) })
31+
assertFailsWith<TimeoutException>(flow.timeout(300.milliseconds).onEach { list.add(it) })
3132
assertEquals(listOf("A", "B", "C"), list)
3233
finish(5)
3334
}
@@ -60,7 +61,7 @@ class TimeoutTest : TestBase() {
6061

6162
expect(2)
6263
val list = mutableListOf<String>()
63-
flow.timeout(300.milliseconds).catch { if (it is TimeoutCancellationException) emit("-1") }.collect { list.add(it) }
64+
flow.timeout(300.milliseconds).catch { if (it is TimeoutException) emit("-1") }.collect { list.add(it) }
6465
assertEquals(listOf("A", "B", "C", "-1"), list)
6566
finish(5)
6667
}
@@ -164,7 +165,7 @@ class TimeoutTest : TestBase() {
164165
expectUnreached()
165166
}.flowOn(NamedDispatchers("upstream")).timeout(100.milliseconds)
166167

167-
assertFailsWith<TimeoutCancellationException>(flow)
168+
assertFailsWith<TimeoutException>(flow)
168169
finish(3)
169170
}
170171

@@ -192,7 +193,7 @@ class TimeoutTest : TestBase() {
192193
try {
193194
MutableSharedFlow<Int>().asSharedFlow().timeout(100.milliseconds).collect()
194195
expectUnreached()
195-
} catch (e: TimeoutCancellationException) {
196+
} catch (e: TimeoutException) {
196197
finish(1)
197198
}
198199
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
1+
kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
22
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt)
3-
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.outerChildWithTimeout(StackTraceRecoveryWithTimeoutTest.kt:48)
4-
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$testStacktraceIsRecoveredFromLexicalBlockWhenTriggeredByChild$1.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt:40)
5-
Caused by: kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
6-
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:116)
7-
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:86)
3+
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.outerChildWithTimeout(StackTraceRecoveryWithTimeoutTest.kt)
4+
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$testStacktraceIsRecoveredFromLexicalBlockWhenTriggeredByChild$1.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt)
5+
Caused by: kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
6+
at kotlinx.coroutines.time.TimeoutKt.TimeoutException(Timeout.kt)
7+
at kotlinx.coroutines.time.TimeoutCoroutine.run(Timeout.kt)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
1+
kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
22
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt)
33
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.suspendForever(StackTraceRecoveryWithTimeoutTest.kt:42)
44
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$outerWithTimeout$2.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt:32)
55
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.outerWithTimeout(StackTraceRecoveryWithTimeoutTest.kt:31)
66
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$testStacktraceIsRecoveredFromSuspensionPoint$1.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt:19)
7-
Caused by: kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
8-
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:116)
9-
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:86)
7+
Caused by: kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
8+
at kotlinx.coroutines.time.TimeoutKt.TimeoutException(Timeout.kt:116)
9+
at kotlinx.coroutines.time.TimeoutCoroutine.run(Timeout.kt:86)
1010
at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:492)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
1+
kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
22
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt)
33
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.suspendForever(StackTraceRecoveryWithTimeoutTest.kt:92)
44
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$outerChild$2.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt:78)
55
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest.outerChild(StackTraceRecoveryWithTimeoutTest.kt:74)
66
at kotlinx.coroutines.exceptions.StackTraceRecoveryWithTimeoutTest$testStacktraceIsRecoveredFromSuspensionPointWithChild$1.invokeSuspend(StackTraceRecoveryWithTimeoutTest.kt:66)
7-
Caused by: kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 200 ms
8-
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:116)
9-
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:86)
7+
Caused by: kotlinx.coroutines.time.TimeoutException: Timed out waiting for 200 ms
8+
at kotlinx.coroutines.time.TimeoutKt.TimeoutException(Timeout.kt:116)
9+
at kotlinx.coroutines.time.TimeoutCoroutine.run(Timeout.kt:86)

kotlinx-coroutines-core/jvm/test/exceptions/StackTraceRecoveryWithTimeoutTest.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package kotlinx.coroutines.exceptions
66

77
import kotlinx.coroutines.*
8+
import kotlinx.coroutines.time.*
89
import org.junit.*
910
import org.junit.rules.*
1011

@@ -17,7 +18,7 @@ class StackTraceRecoveryWithTimeoutTest : TestBase() {
1718
fun testStacktraceIsRecoveredFromSuspensionPoint() = runTest {
1819
try {
1920
outerWithTimeout()
20-
} catch (e: TimeoutCancellationException) {
21+
} catch (e: TimeoutException) {
2122
verifyStackTrace("timeout/${name.methodName}", e)
2223
}
2324
}
@@ -38,7 +39,7 @@ class StackTraceRecoveryWithTimeoutTest : TestBase() {
3839
fun testStacktraceIsRecoveredFromLexicalBlockWhenTriggeredByChild() = runTest {
3940
try {
4041
outerChildWithTimeout()
41-
} catch (e: TimeoutCancellationException) {
42+
} catch (e: TimeoutException) {
4243
verifyStackTrace("timeout/${name.methodName}", e)
4344
}
4445
}
@@ -64,7 +65,7 @@ class StackTraceRecoveryWithTimeoutTest : TestBase() {
6465
fun testStacktraceIsRecoveredFromSuspensionPointWithChild() = runTest {
6566
try {
6667
outerChild()
67-
} catch (e: TimeoutCancellationException) {
68+
} catch (e: TimeoutException) {
6869
verifyStackTrace("timeout/${name.methodName}", e)
6970
}
7071
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleCancel07
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
kotlinx.coroutines.time.withTimeout(1300L) {
11+
withTimeout(1300L) {
1212
repeat(1000) { i ->
1313
println("I'm sleeping $i ...")
1414
delay(500L)

kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class Resource {
1717
fun main() {
1818
runBlocking {
1919
repeat(100_000) { // Launch 100K coroutines
20-
launch {
21-
val resource = kotlinx.coroutines.time.withTimeout(60) { // Timeout of 60 ms
20+
launch {
21+
val resource = withTimeout(60) { // Timeout of 60 ms
2222
delay(50) // Delay for 50 ms
23-
Resource() // Acquire a resource and return it from withTimeout block
23+
Resource() // Acquire a resource and return it from withTimeout block
2424
}
2525
resource.close() // Release the resource
2626
}

kotlinx-coroutines-core/jvm/test/guide/example-cancel-10.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class Resource {
1717
fun main() {
1818
runBlocking {
1919
repeat(100_000) { // Launch 100K coroutines
20-
launch {
20+
launch {
2121
var resource: Resource? = null // Not acquired yet
2222
try {
23-
kotlinx.coroutines.time.withTimeout(60) { // Timeout of 60 ms
23+
withTimeout(60) { // Timeout of 60 ms
2424
delay(50) // Delay for 50 ms
25-
resource = Resource() // Store a resource to the variable if acquired
25+
resource = Resource() // Store a resource to the variable if acquired
2626
}
2727
// We can do something else with the resource here
28-
} finally {
28+
} finally {
2929
resource?.close() // Release the resource if it was acquired
3030
}
3131
}

0 commit comments

Comments
 (0)