-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential data race in EventLoop (#3289)
Fixes #3251
- Loading branch information
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
kotlinx-coroutines-core/jvm/test/internal/ThreadSafeHeapStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
import java.util.concurrent.* | ||
import java.util.concurrent.CancellationException | ||
import kotlin.test.* | ||
|
||
class ThreadSafeHeapStressTest : TestBase() { | ||
private class DisposableNode : EventLoopImplBase.DelayedTask(1L) { | ||
override fun run() { | ||
} | ||
} | ||
|
||
@Test | ||
fun testConcurrentRemoveDispose() = runTest { | ||
val heap = EventLoopImplBase.DelayedTaskQueue(1) | ||
repeat(10_000 * stressTestMultiplierSqrt) { | ||
withContext(Dispatchers.Default) { | ||
val node = DisposableNode() | ||
val barrier = CyclicBarrier(2) | ||
launch { | ||
heap.addLast(node) | ||
barrier.await() | ||
heap.remove(node) | ||
} | ||
launch { | ||
barrier.await() | ||
Thread.yield() | ||
node.dispose() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test() | ||
fun testConcurrentAddDispose() = runTest { | ||
repeat(10_000 * stressTestMultiplierSqrt) { | ||
val jobToCancel = Job() | ||
val barrier = CyclicBarrier(2) | ||
val jobToJoin = launch(Dispatchers.Default) { | ||
barrier.await() | ||
jobToCancel.cancelAndJoin() | ||
} | ||
|
||
try { | ||
runBlocking { // Use event loop impl | ||
withContext(jobToCancel) { | ||
// This one is to work around heap allocation optimization | ||
launch(start = CoroutineStart.UNDISPATCHED) { | ||
delay(100_000) | ||
} | ||
barrier.await() | ||
delay(100_000) | ||
} | ||
} | ||
} catch (e: CancellationException) { | ||
// Expected exception | ||
} | ||
jobToJoin.join() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,4 +93,4 @@ class ThreadSafeHeapTest : TestBase() { | |
assertEquals(set.size, h.size) | ||
} | ||
} | ||
} | ||
} |