reactor / BlockHound

Java agent to detect blocking calls from non-blocking threads.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't whitelist blocking lambda in Mono.fromRunnable

jvalkeal opened this issue · comments

While enabling BlockHound for reactive work I've done in a spring-statemachine space there was a one test where I had to extract blocking sleep out from a lambda order to define it in a BlockHoundIntegration.

This was a change I had to make in my tests to extract Thread.sleep() out from it:
https://github.com/spring-projects/spring-statemachine/blob/9e0bdae46fd80b2242c61149cf3d08987a9feedf/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ObjectStateTests.java#L155-L183

Then I defined sleep as:
https://github.com/spring-projects/spring-statemachine/blob/9e0bdae46fd80b2242c61149cf3d08987a9feedf/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineBlockHoundIntegration.java#L31

Previously having

.allowBlockingCallsInside("org.springframework.statemachine.state.ObjectStateTests$TestBlockingAction", "apply")

just didn't work.

@jvalkeal
I just tried this and it works perfectly fine:

public class LambdaTest {

    static {
        BlockHound.install(b -> {
            b.allowBlockingCallsInside(LambdaTest.class.getName(), "lambda$shouldWhitelistLambdas$1");
        });
    }
    
    @Test
    public void shouldWhitelistLambdas() {
        Mono.fromCallable(() -> {
            Thread.sleep(0);
            return "";
        }).subscribeOn(Schedulers.parallel()).block();
    }
}

you were whitelisting the wrong method. TestBlockingAction#apply only returns Mono.fromRunnable and does not contain any blocking calls, the actual blocking call happens inside TestBlockingAction#lambda$apply$1, aka fromRunnable's argument.