Skip to content

Commit dc9ca27

Browse files
committed
Fix native-mt awaitAll
Fixes #2025
1 parent 57df180 commit dc9ca27

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,18 @@ private class AwaitAll<T>(private val deferreds: Array<out Deferred<T>>) {
8383

8484
private inner class DisposeHandlersOnCancel(private val nodes: Array<AwaitAllNode>) : CancelHandler() {
8585
fun disposeAll() {
86-
nodes.forEach { it.handle.dispose() }
86+
nodes.forEach { it.handle!!.dispose() }
8787
}
8888

8989
override fun invoke(cause: Throwable?) { disposeAll() }
9090
override fun toString(): String = "DisposeHandlersOnCancel[$nodes]"
9191
}
9292

9393
private inner class AwaitAllNode(private val continuation: CancellableContinuation<List<T>>, job: Job) : JobNode<Job>(job) {
94-
lateinit var handle: DisposableHandle
94+
private val _handle = atomic<DisposableHandle?>(null)
95+
var handle: DisposableHandle?
96+
get() = _handle.value
97+
set(value) { _handle.value = value }
9598

9699
private val _disposer = atomic<DisposeHandlersOnCancel?>(null)
97100
var disposer: DisposeHandlersOnCancel?

kotlinx-coroutines-core/native/test/WorkerDispatcherTest.kt

+13
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,18 @@ class WorkerDispatcherTest : TestBase() {
313313
finish(6)
314314
}
315315

316+
@Test
317+
fun testAwaitAll() = runTest {
318+
expect(1)
319+
val d1 = async(dispatcher) {
320+
"A"
321+
}
322+
val d2 = async(dispatcher) {
323+
"B"
324+
}
325+
assertEquals("AB", awaitAll(d1, d2).joinToString(""))
326+
finish(2)
327+
}
328+
316329
private data class Data(val s: String)
317330
}

0 commit comments

Comments
 (0)