You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: kotlinx-coroutines-core/common/src/Timeout.kt
+54-57Lines changed: 54 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -14,26 +14,11 @@ import kotlin.time.*
14
14
importkotlin.time.Duration.Companion.milliseconds
15
15
16
16
/**
17
-
* Runs a given suspending [block] of code inside a coroutine with a specified [timeout][timeMillis] and throws
18
-
* a [TimeoutCancellationException] if the timeout was exceeded.
19
-
* If the given [timeMillis] is non-positive, [TimeoutCancellationException] is thrown immediately.
17
+
* Shortcut for calling [withTimeout] with a [Duration] timeout of [timeMillis] milliseconds.
18
+
* Please see that overload for details.
20
19
*
21
-
* The code that is executing inside the [block] is cancelled on timeout and the active or next invocation of
22
-
* the cancellable suspending function inside the block throws a [TimeoutCancellationException].
23
-
*
24
-
* The sibling function that does not throw an exception on timeout is [withTimeoutOrNull].
25
-
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
26
-
*
27
-
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
28
-
* even right before the return from inside the timeout [block]. Keep this in mind if you open or acquire some
29
-
* resource inside the [block] that needs closing or release outside the block.
30
-
* See the
31
-
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
32
-
* section of the coroutines guide for details.
33
-
*
34
-
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
35
-
*
36
-
* @param timeMillis timeout time in milliseconds.
20
+
* > Note: the behavior of this function can be different from [withTimeout] if [timeMillis] is greater than
21
+
* `Long.MAX_VALUE / 2` milliseconds.
37
22
*/
38
23
publicsuspendfun <T> withTimeout(timeMillis:Long, block:suspendCoroutineScope.() ->T): T {
39
24
contract {
@@ -52,12 +37,9 @@ public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineSco
52
37
* If the [block] execution times out, it is cancelled with a [TimeoutCancellationException].
53
38
* If the [timeout] is non-positive, this happens immediately and the [block] is not executed.
54
39
*
55
-
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
56
-
* even right before the return from inside the timeout [block]. Keep this in mind if you open or acquire some
57
-
* resource inside the [block] that needs closing or release outside the block.
58
-
* See the
59
-
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
60
-
* section of the coroutines guide for details.
40
+
* The cancellation on timeout is asynchronous with respect to the code running in the block
41
+
* and may happen at any time, even after the [block] finishes executing but before the caller gets resumed with
42
+
* the result.
61
43
*
62
44
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
63
45
*
@@ -160,26 +142,11 @@ public suspend fun <T> withTimeout(timeout: Duration, block: suspend CoroutineSc
160
142
}
161
143
162
144
/**
163
-
* Runs a given suspending block of code inside a coroutine with a specified [timeout][timeMillis] and returns
164
-
* `null` if this timeout was exceeded.
165
-
* If the given [timeMillis] is non-positive, `null` is returned immediately.
166
-
*
167
-
* The code that is executing inside the [block] is cancelled on timeout and the active or next invocation of
168
-
* cancellable suspending function inside the block throws a [TimeoutCancellationException].
169
-
*
170
-
* The sibling function that throws an exception on timeout is [withTimeout].
171
-
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
172
-
*
173
-
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
174
-
* even right before the return from inside the timeout [block]. Keep this in mind if you open or acquire some
175
-
* resource inside the [block] that needs closing or release outside the block.
176
-
* See the
177
-
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
178
-
* section of the coroutines guide for details.
179
-
*
180
-
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
145
+
* Shortcut for calling [withTimeoutOrNull] with a [Duration] timeout of [timeMillis] milliseconds.
146
+
* Please see that overload for details.
181
147
*
182
-
* @param timeMillis timeout time in milliseconds.
148
+
* > Note: the behavior of this function can be different from [withTimeoutOrNull] if [timeMillis] is greater than
@@ -201,24 +168,54 @@ public suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend Corout
201
168
}
202
169
203
170
/**
204
-
* Runs a given suspending block of code inside a coroutine with the specified [timeout] and returns
205
-
* `null` if this timeout was exceeded.
206
-
* If the given [timeout] is non-positive, `null` is returned immediately.
171
+
* Calls the specified suspending [block] with the specified [timeout], suspends until it completes,
172
+
* and returns the result.
173
+
*
174
+
* If the [block] execution times out, it is cancelled with a [TimeoutCancellationException].
175
+
* If the [timeout] is non-positive, this happens immediately and the [block] is not executed.
176
+
*
177
+
* The cancellation on timeout is asynchronous with respect to the code running in the block
178
+
* and may happen at any time, even after the [block] finishes executing but before the caller gets resumed with
179
+
* the result.
180
+
*
181
+
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
182
+
*
183
+
* ## Structured Concurrency
184
+
*
185
+
* [withTimeoutOrNull] behaves like [coroutineScope], as it, too, creates a new *scoped child coroutine*.
186
+
* Refer to the documentation of [coroutineScope] for details.
207
187
*
208
-
* The code that is executing inside the [block] is cancelled on timeout and the active or next invocation of
209
-
* cancellable suspending function inside the block throws a [TimeoutCancellationException].
188
+
* ## Pitfalls
189
+
*
190
+
* ### Cancellation is cooperative
210
191
*
211
-
* The sibling function that throws an exception on timeout is [withTimeout].
212
-
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
192
+
* [withTimeoutOrNull] will not automatically stop all code inside it from being executed
193
+
* once the timeout gets triggered.
194
+
* It only cancels the running [block], but it's up to the [block] to notice that it was cancelled, for example,
195
+
* using [ensureActive], checking [isActive], or using [suspendCancellableCoroutine].
196
+
*
197
+
* For example, this JVM code will run to completion, taking 10 seconds to do so:
198
+
*
199
+
* ```
200
+
* withTimeout(1.seconds) {
201
+
* Thread.sleep(10_000)
202
+
* }
203
+
* ```
213
204
*
214
-
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
215
-
* even right before the return from inside the timeout [block]. Keep this in mind if you open or acquire some
216
-
* resource inside the [block] that needs closing or release outside the block.
217
-
* See the
218
-
* [Asynchronous timeout and resources](https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources)
205
+
* On the JVM, use the `runInterruptible` function to propagate cancellations
206
+
* to blocking JVM code as thread interruptions.
207
+
*
208
+
* See the [Cancellation is cooperative](https://kotlinlang.org/docs/cancellation-and-timeouts.html#cancellation-is-cooperative).
219
209
* section of the coroutines guide for details.
220
210
*
221
-
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
211
+
* ### Returning closeable resources
212
+
*
213
+
* Values returned from [withTimeoutOrNull] will typically be lost if the caller is cancelled.
214
+
*
215
+
* See the corresponding section in the [coroutineScope] documentation for details.
0 commit comments