What is BlockHound and how does it work?
masterBlockHound is a Java agent designed to detect blocking calls (such as I/O operations) when they are executed from threads designated as "non-blocking operations only".
It works by transparently instrumenting JVM classes and intercepting blocking calls. It identifies non-blocking threads by checking if they implement Reactor's NonBlocking marker interface (for example, threads started by Schedulers.parallel()). If a blocking call is detected on such a thread, BlockHound throws a reactor.blockhound.BlockingOperationError, which includes the exact location in your code where the violation occurred.
// Example of a violation
BlockHound.install();
Mono.delay(Duration.ofSeconds(1))
.doOnNext(it -> {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
})
.block();