Skip to content

Fix BlockHound false-positive in ConflatedChannel #2881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class CoroutinesBlockHoundIntegration : BlockHoundIntegration {
*/
private fun BlockHound.Builder.allowBlockingCallsInConflatedChannel() {
for (method in listOf("offerInternal", "offerSelectInternal", "pollInternal", "pollSelectInternal",
"onCancelIdempotent"))
"onCancelIdempotent", "isEmpty", "enqueueReceiveInternal"))
{
allowBlockingCallsInside("kotlinx.coroutines.channels.ConflatedChannel", method)
}
Expand Down
27 changes: 23 additions & 4 deletions kotlinx-coroutines-debug/test/BlockHoundTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.channels.*
import org.junit.*
import reactor.blockhound.*

@Suppress("UnusedEquals", "DeferredResultUnused", "BlockingMethodInNonBlockingContext")
class BlockHoundTest : TestBase() {

@Before
Expand All @@ -12,21 +13,21 @@ class BlockHoundTest : TestBase() {
}

@Test(expected = BlockingOperationError::class)
fun shouldDetectBlockingInDefault() = runTest {
fun testShouldDetectBlockingInDefault() = runTest {
withContext(Dispatchers.Default) {
Thread.sleep(1)
}
}

@Test
fun shouldNotDetectBlockingInIO() = runTest {
fun testShouldNotDetectBlockingInIO() = runTest {
withContext(Dispatchers.IO) {
Thread.sleep(1)
}
}

@Test
fun shouldNotDetectNonblocking() = runTest {
fun testShouldNotDetectNonblocking() = runTest {
withContext(Dispatchers.Default) {
val a = 1
val b = 2
Expand Down Expand Up @@ -54,7 +55,7 @@ class BlockHoundTest : TestBase() {
}

@Test
fun testChannelsNotBeingConsideredBlocking() = runTest {
fun testChannelNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
// Copy of kotlinx.coroutines.channels.ArrayChannelTest.testSimple
val q = Channel<Int>(1)
Expand All @@ -74,6 +75,24 @@ class BlockHoundTest : TestBase() {
}
}

@Test
fun testConflatedChannelsNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
val q = Channel<Int>(Channel.CONFLATED)
check(q.isEmpty)
check(!q.isClosedForReceive)
check(!q.isClosedForSend)
val sender = launch {
q.send(1)
}
val receiver = launch {
q.receive() == 1
}
sender.join()
receiver.join()
}
}

@Test(expected = BlockingOperationError::class)
fun testReusingThreadsFailure() = runTest {
val n = 100
Expand Down