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

Mono.toFuture(): invocation of cancel after completion. #2070

Closed
anuchandy opened this issue Mar 10, 2020 · 2 comments
Closed

Mono.toFuture(): invocation of cancel after completion. #2070

anuchandy opened this issue Mar 10, 2020 · 2 comments
Labels
type/bug A general bug

Comments

@anuchandy
Copy link

We have a code like below which does the following:

  1. Create a local file
  2. Download contents to this file
  3. If the subscription is canceled or there is an error we remove the possibly partially written file
final String fileName = "myLocalFile.txt";
Mono<String> fileDownloadMono = Mono.using(() -> new PrintWriter(fileName, "UTF-8"),
        new Function<PrintWriter, Mono<String>>() {
            @Override
            public Mono<String> apply(PrintWriter printWriter) {
                // Download from network.
                System.out.println("Downloading from network");
                String downloadedContent = "hello";
                // write to local file
                System.out.println("Writing to local file");
                printWriter.write(downloadedContent);
                return Mono.just(fileName);
            }
        },
        printWriter -> {
            printWriter.close();
        })
        .doFinally(signalType -> {
            if (signalType == SignalType.ON_ERROR
                || signalType == SignalType.CANCEL) {
                System.out.println("Unable to complete download/write to file, deleting unfinished file.");
                (new File(fileName)).delete();
            }
        });

This works fine. The issue we're facing is when we convert this Mono to a Future like below:

fileDownloadMono.toFuture();

toFuture() operator cancels the Mono as soon as the Mono completes, which result in the removal of successfully completed file.

Code in toFuture() operator that cancel the Mono is here.

@simonbasle
Copy link
Member

Yeah it doesn't sound like this is needed at all for a Mono publisher. Note that it would be necessary if the MonoToCompletableFuture (which is a Subscriber) was subscribed to eg. a Flux...

What I can do to guide any potential future use of this subscriber with anything else than a Mono is to add a constructor parameter that drives whether or not to cancel in onNext. (and of course Mono.toFuture() would prevent such cancellation).

@anuchandy
Copy link
Author

thank you @simonbasle!

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

No branches or pull requests

2 participants