Skip to content

Commit efb4720

Browse files
committed
Implement BlockHound integration properly
Now it relies on the ServiceLoader mechanism to load the integration: if the user installs BlockHound and `kotlinx-coroutines-debug` is in the classpath, then BlockHound will know to detect blocking calls in scenarios that forbid it.
1 parent 5058081 commit efb4720

File tree

6 files changed

+24
-39
lines changed

6 files changed

+24
-39
lines changed

kotlinx-coroutines-debug/api/kotlinx-coroutines-debug.api

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public final class kotlinx/coroutines/debug/CoroutineInfo {
88
public fun toString ()Ljava/lang/String;
99
}
1010

11+
public final class kotlinx/coroutines/debug/CoroutinesBlockHoundIntegration : reactor/blockhound/integration/BlockHoundIntegration {
12+
public fun <init> ()V
13+
public fun applyTo (Lreactor/blockhound/BlockHound$Builder;)V
14+
}
15+
1116
public final class kotlinx/coroutines/debug/DebugProbes {
1217
public static final field INSTANCE Lkotlinx/coroutines/debug/DebugProbes;
1318
public final fun dumpCoroutines (Ljava/io/PrintStream;)V
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kotlinx.coroutines.debug.CoroutinesBlockHoundIntegration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
2+
package kotlinx.coroutines.debug
3+
4+
import reactor.blockhound.BlockHound
5+
import kotlinx.coroutines.scheduling.*
6+
import reactor.blockhound.integration.*
7+
8+
@Suppress("UNUSED")
9+
public class CoroutinesBlockHoundIntegration: BlockHoundIntegration {
10+
11+
override fun applyTo(builder: BlockHound.Builder) {
12+
builder.addDynamicThreadPredicate { isSchedulerWorker(it) }
13+
builder.nonBlockingThreadPredicate { p -> p.or { mayNotBlock(it) } }
14+
}
15+
16+
}

kotlinx-coroutines-debug/src/internal/BlockHoundIntegration.kt

-27
This file was deleted.

kotlinx-coroutines-debug/src/internal/DebugProbesImpl.kt

-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ internal object DebugProbesImpl {
6666
.name(cl.name)
6767
.make()
6868
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
69-
70-
BlockHoundIntegration.install()
7169
}
7270

7371
public fun uninstall(): Unit = coroutineStateLock.write {
@@ -84,8 +82,6 @@ internal object DebugProbesImpl {
8482
.name(cl.name)
8583
.make()
8684
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
87-
88-
BlockHoundIntegration.uninstall()
8985
}
9086

9187
public fun hierarchyToString(job: Job): String = coroutineStateLock.write {

kotlinx-coroutines-debug/test/BlockHoundTest.kt

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package kotlinx.coroutines.debug
22
import kotlinx.coroutines.*
3-
import kotlinx.coroutines.debug.internal.BlockHoundIntegration
43
import org.junit.*
5-
import reactor.blockhound.BlockingOperationError
4+
import reactor.blockhound.*
65

76
class BlockHoundTest : TestBase() {
87

98
@Before
109
fun init() {
11-
BlockHoundIntegration.install()
12-
}
13-
14-
@After
15-
fun deinit() {
16-
BlockHoundIntegration.uninstall()
10+
BlockHound.install()
1711
}
1812

1913
@Test(expected = BlockingOperationError::class)

0 commit comments

Comments
 (0)