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
Added docs on withTimeout asynchrony and its use with resources (Kotlin#2252)
This is a tricky gotcha that needs additional explanation. There are two examples added, one showing the bad code and explaining why it does not work, and the other showing the correct way to write it.
FixesKotlin#2233
* <aname='asynchronous-timeout-and-resources'></a>[Asynchronous timeout and resources](docs/cancellation-and-timeouts.md#asynchronous-timeout-and-resources)
Copy file name to clipboardExpand all lines: kotlinx-coroutines-core/common/src/Timeout.kt
+32-4
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,14 @@ import kotlin.time.*
24
24
* The sibling function that does not throw an exception on timeout is [withTimeoutOrNull].
25
25
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
26
26
*
27
-
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
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 of the timeout [block]. Keep this in mind if you open or acquire some
29
+
* resource inside the [block] that needs closing or release outside of 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].
28
35
*
29
36
* @param timeMillis timeout time in milliseconds.
30
37
*/
@@ -48,7 +55,14 @@ public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineSco
48
55
* The sibling function that does not throw an exception on timeout is [withTimeoutOrNull].
49
56
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
50
57
*
51
-
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
58
+
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
59
+
* even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some
60
+
* resource inside the [block] that needs closing or release outside of the block.
61
+
* See the
62
+
* [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources]
63
+
* section of the coroutines guide for details.
64
+
*
65
+
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
52
66
*/
53
67
@ExperimentalTime
54
68
publicsuspendfun <T> withTimeout(timeout:Duration, block:suspendCoroutineScope.() ->T): T {
@@ -68,7 +82,14 @@ public suspend fun <T> withTimeout(timeout: Duration, block: suspend CoroutineSc
68
82
* The sibling function that throws an exception on timeout is [withTimeout].
69
83
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
70
84
*
71
-
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
85
+
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
86
+
* even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some
87
+
* resource inside the [block] that needs closing or release outside of the block.
88
+
* See the
89
+
* [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources]
90
+
* section of the coroutines guide for details.
91
+
*
92
+
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
72
93
*
73
94
* @param timeMillis timeout time in milliseconds.
74
95
*/
@@ -101,7 +122,14 @@ public suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend Corout
101
122
* The sibling function that throws an exception on timeout is [withTimeout].
102
123
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
103
124
*
104
-
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
125
+
* **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time,
126
+
* even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some
127
+
* resource inside the [block] that needs closing or release outside of the block.
128
+
* See the
129
+
* [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources]
130
+
* section of the coroutines guide for details.
131
+
*
132
+
* > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
0 commit comments