Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not detected call to Files.readString(path) #192

Open
pmendelski opened this issue Apr 17, 2021 · 2 comments
Open

Not detected call to Files.readString(path) #192

pmendelski opened this issue Apr 17, 2021 · 2 comments

Comments

@pmendelski
Copy link

Expected Behavior

public static void main(String[] args) {
    BlockHound.install();
    Mono.fromCallable(() -> {
        try {
            return Files.readString(testFilePath);
        } catch (IOException e) {
            throw new RuntimeException("Could not read file: " + testFilePath, e);
        }
    })
            .subscribeOn(Schedulers.parallel())
            .block();
    // Expected blocking call error
}

Actual Behavior

BlockHound does not detect call Files.readString(path).

Steps to Reproduce

I created small project to test this issue.

Your Environment

  • Reactor version(s) used: 3.4.5
  • JVM version: OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)
  • OS and version: Linux 5.8.0-49-generic 20.04.1-Ubuntu x86_64
@bsideup
Copy link
Contributor

bsideup commented Apr 17, 2021

Hey @pmendelski,

Mono.fromCallable implements a special optimization that makes it execute on the same thread when block() is used.
Hint: try adding System.out.println(Thread.currentThread()) to your fromCallable's lambda.

You can verify it by changing block() to toFuture().get() that doesn't use the optimization.

/cc @simonbasle

@pmendelski
Copy link
Author

Thanks for such a fast answer.

I added logs and tried toFuture().get() but it seems to not solve the problem.
In logs I can see that the code is executed on the parallel scheduler and BlockHound did not detect any of the calls.

public static void main(String[] args) throws ExecutionException, InterruptedException {
    BlockHound.install();

    Mono.fromCallable(() -> {
        System.out.println("block() - " + Thread.currentThread().getName());
        return readFile(testFilePath);
    })
            .subscribeOn(Schedulers.parallel())
            .block();

    Mono.fromCallable(() -> {
        System.out.println("toFuture() - " + Thread.currentThread().getName());
        return readFile(testFilePath);
    })
            .subscribeOn(Schedulers.parallel())
            .toFuture()
            .get();

    Mono.delay(Duration.ofMillis(10))
            .doOnNext(it -> {
                System.out.println("doOnNext() - " + Thread.currentThread().getName());
                readFile(testFilePath);
            })
            .block();
}

static String readFile(Path path) {
    try {
        return Files.readString(path);
    } catch (IOException e) {
        throw new RuntimeException("Could not read file: " + path, e);
    }
}

// Logs:
// block() - parallel-1
// toFuture() - parallel-2
// doOnNext() - parallel-3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants