forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed memory leak on a race between adding/removing from lock-free li…
…st (Kotlin#1845) * The problem was introduced by Kotlin#1565. When doing concurrent add+removeFirst the following can happen: - "add" completes, but has not correct prev pointer in next node yet - "removeFirst" removes freshly added element - "add" performs "finishAdd" that adjust prev pointer of the next node and thus removed element is pointed from the list again * A separate LockFreeLinkedListAddRemoveStressTest is added that reproduces this problem. * The old LockFreeLinkedListAtomicLFStressTest is refactored a bit.
- Loading branch information
Showing
3 changed files
with
90 additions
and
29 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
56 changes: 56 additions & 0 deletions
56
kotlinx-coroutines-core/jvm/test/internal/LockFreeLinkedListAddRemoveStressTest.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,56 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.atomicfu.* | ||
import kotlinx.coroutines.* | ||
import java.util.concurrent.* | ||
import kotlin.concurrent.* | ||
import kotlin.test.* | ||
|
||
class LockFreeLinkedListAddRemoveStressTest : TestBase() { | ||
private class Node : LockFreeLinkedListNode() | ||
|
||
private val nRepeat = 100_000 * stressTestMultiplier | ||
private val list = LockFreeLinkedListHead() | ||
private val barrier = CyclicBarrier(3) | ||
private val done = atomic(false) | ||
private val removed = atomic(0) | ||
|
||
@Test | ||
fun testStressAddRemove() { | ||
val threads = ArrayList<Thread>() | ||
threads += testThread("adder") { | ||
val node = Node() | ||
list.addLast(node) | ||
if (node.remove()) removed.incrementAndGet() | ||
} | ||
threads += testThread("remover") { | ||
val node = list.removeFirstOrNull() | ||
if (node != null) removed.incrementAndGet() | ||
} | ||
try { | ||
for (i in 1..nRepeat) { | ||
barrier.await() | ||
barrier.await() | ||
assertEquals(i, removed.value) | ||
list.validate() | ||
} | ||
} finally { | ||
done.value = true | ||
barrier.await() | ||
threads.forEach { it.join() } | ||
} | ||
} | ||
|
||
private fun testThread(name: String, op: () -> Unit) = thread(name = name) { | ||
while (true) { | ||
barrier.await() | ||
if (done.value) break | ||
op() | ||
barrier.await() | ||
} | ||
} | ||
} |
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