Class BlockingOnNonBlockingThreadDetector

java.lang.Object
se.deversity.asynctest.diagnostics.BlockingOnNonBlockingThreadDetector

public final class BlockingOnNonBlockingThreadDetector extends Object
Detects a blocking call on a thread that must not block: a Reactor non-blocking scheduler thread, a Netty or Vert.x event loop, or a Kotlin 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
  • Constructor Details

    • BlockingOnNonBlockingThreadDetector

      public BlockingOnNonBlockingThreadDetector()
  • Method Details

    • recordBlockingCall

      public void recordBlockingCall(String operation, Thread thread)
      Records a blocking call on thread; reported if the thread is one that must not block.
      Parameters:
      operation - the blocking call, as the report should name it, such as Thread.sleep
      thread - the thread that blocked (null-safe)
    • recordRunBlocking

      public void recordRunBlocking(Thread thread)
      Records a Kotlin runBlocking entered on thread (#1007); reported on a thread that must not block, and on any coroutine dispatcher worker.

      runBlocking parks its thread until the coroutine it starts completes. On an ordinary thread that is its purpose, bridging blocking code to suspending code. On a CoroutineScheduler worker the caller is itself a coroutine: on Dispatchers.Default it takes one of the per-core threads out of the pool, and on Dispatchers.IO, or a limitedParallelism view 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 a Dispatchers.IO worker is not.

      A coroutine on any other thread is recognised only when thread is the calling thread, by walking its stack for a kotlinx.coroutines frame: a runBlocking nested in another runs inside the outer one's event loop on the caller's own thread, and an asCoroutineDispatcher dispatcher runs its coroutines on the executor's plain threads, so neither has a thread class to classify. The walk costs microseconds, and runBlocking is itself a parked thread and an event loop. The executor's thread must still be the run's: the woven execute hands the task over on a JDK ScheduledThreadPoolExecutor, but not through the wrapper newSingleThreadContext puts around its pool, so that dispatcher's thread is never heard from (#1107).

      Parameters:
      thread - the thread that entered runBlocking (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