Skip to content

Commit bbe6d8a

Browse files
committed
Add workaround for K/N bug
1 parent aa100ea commit bbe6d8a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,17 @@ public suspend fun <T> withTimeoutOrNull(time: Int, block: suspend CoroutineScop
134134
public suspend fun <T> withTimeoutOrNull(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS, block: suspend CoroutineScope.() -> T): T? {
135135
if (time <= 0L) return null
136136

137-
var coroutine: TimeoutCoroutine<T?, T?>? = null
137+
// Workaround for K/N bug
138+
val array = arrayOfNulls<TimeoutCoroutine<T?, T?>>(1)
138139
try {
139140
return suspendCoroutineUninterceptedOrReturn { uCont ->
140-
TimeoutCoroutine(time, unit, uCont).let {
141-
coroutine = it
142-
setupTimeout<T?, T?>(it, block)
143-
}
141+
val timeoutCoroutine = TimeoutCoroutine(time, unit, uCont)
142+
array[0] = timeoutCoroutine
143+
setupTimeout<T?, T?>(timeoutCoroutine, block)
144144
}
145145
} catch (e: TimeoutCancellationException) {
146146
// Return null iff it's our exception, otherwise propagate it upstream (e.g. in case of nested withTimeouts)
147-
if (e.coroutine === coroutine) {
147+
if (e.coroutine === array[0]) {
148148
return null
149149
}
150150
throw e

0 commit comments

Comments
 (0)