Class BlockingOnNonBlockingThreadDetector
Dispatchers.Default worker (#991, #993).
An event loop runs every task queued for it, one after another, and a reactive scheduler's
non-blocking threads are few by design, one per core. A Thread.sleep, a join or
get of a pending future, a latch await or a take from an empty queue on such a thread
stalls everything behind it: the server stops answering, the pipeline stops emitting, and nothing
fails, it just gets slow under load. It is the defining misuse of the reactive frameworks, and
BlockHound exists for it. Dispatchers.Default is the same for coroutines: a CPU-bound pool
of one thread per core that blocking starves, where Dispatchers.IO is the place to block.
A thread is classified the way BlockHound classifies it, by its class's names, since the
library depends on none of the frameworks: one implementing reactor.core.scheduler.NonBlocking;
a io.vertx.core.impl.VertxThread whose isWorker() is false; otherwise a
io.netty.util.concurrent.FastThreadLocalThread whose permitBlockingCalls() is
false; and a kotlinx.coroutines.scheduling.CoroutineScheduler$Worker whose state
is CPU_ACQUIRED. A coroutine worker running Dispatchers.IO work is in state
BLOCKING, which is that dispatcher's purpose, and is not reported.
A Kotlin runBlocking is the exception (#1007): recordRunBlocking(java.lang.Thread) reports one
entered on a coroutine worker in either state, since the caller is then itself a coroutine
parking its thread until another finishes, and one entered by a coroutine on any other thread,
which it recognises by a kotlinx.coroutines frame below the call on the calling thread's
own stack: a runBlocking nested in another, or on an executor-backed dispatcher, whose
threads have no class of their own. The agent's RunBlockingWeaver
feeds it from every woven runBlocking call site; on an ordinary thread outside any
coroutine it reports nothing.
Attribution needs the framework in includes=. With the agent
(collections=true) every woven blocking call reaches this detector, but only on a thread
the run is known to own. An event-loop thread is started inside the framework, so the run sees
it only when the framework's own code is woven: add its scheduler packages to the agent's
includes= (reactor.core.scheduler, io.netty.util.concurrent,
io.vertx.core, kotlinx.coroutines), so the woven Thread.start or executor
submission inside the framework carries the run to the thread. A thread the run never saw
reports nothing, which errs toward silence under parallel test runs; and a coroutine worker the
JVM created for an earlier test is that test's, not this one's.
Usage inside @AsyncTest(includes = DetectorType.BLOCKING_ON_NON_BLOCKING_THREAD):
var d = AsyncTestContext.blockingOnNonBlockingThreadDetector();
d.recordBlockingCall("Thread.sleep", Thread.currentThread()); // inside an event-loop task
- Since:
- 1.13.2
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyze()Returns report of the blocking calls on threads that must not block.voidrecordBlockingCall(String operation, Thread thread) Records a blocking call onthread; reported if the thread is one that must not block.voidrecordRunBlocking(Thread thread) Records a KotlinrunBlockingentered onthread(#1007); reported on a thread that must not block, and on any coroutine dispatcher worker.
-
Constructor Details
-
BlockingOnNonBlockingThreadDetector
public BlockingOnNonBlockingThreadDetector()
-
-
Method Details
-
recordBlockingCall
Records a blocking call onthread; reported if the thread is one that must not block.- Parameters:
operation- the blocking call, as the report should name it, such asThread.sleepthread- the thread that blocked (null-safe)
-
recordRunBlocking
Records a KotlinrunBlockingentered onthread(#1007); reported on a thread that must not block, and on any coroutine dispatcher worker.runBlockingparks its thread until the coroutine it starts completes. On an ordinary thread that is its purpose, bridging blocking code to suspending code. On aCoroutineSchedulerworker the caller is itself a coroutine: onDispatchers.Defaultit takes one of the per-core threads out of the pool, and onDispatchers.IO, or alimitedParallelismview of either, it holds a thread the coroutine it waits for may need, which deadlocks once every thread waits that way. So a worker in either state is reported, where a plain blocking call on aDispatchers.IOworker is not.A coroutine on any other thread is recognised only when
threadis the calling thread, by walking its stack for akotlinx.coroutinesframe: arunBlockingnested in another runs inside the outer one's event loop on the caller's own thread, and anasCoroutineDispatcherdispatcher runs its coroutines on the executor's plain threads, so neither has a thread class to classify. The walk costs microseconds, andrunBlockingis itself a parked thread and an event loop. The executor's thread must still be the run's: the wovenexecutehands the task over on a JDKScheduledThreadPoolExecutor, but not through the wrappernewSingleThreadContextputs around its pool, so that dispatcher's thread is never heard from (#1107).- Parameters:
thread- the thread that enteredrunBlocking(null-safe)- Since:
- 1.14.0
-
analyze
Returns report of the blocking calls on threads that must not block.- Returns:
- report of the blocking calls on threads that must not block
-