Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How do I cancel a ticker #1514

Closed
wind57 opened this issue Feb 12, 2024 · 4 comments
Closed

How do I cancel a ticker #1514

wind57 opened this issue Feb 12, 2024 · 4 comments

Comments

@wind57
Copy link

wind57 commented Feb 12, 2024

by far not an experienced mutiny user here, so my question is rather basic. Having some code like this (taken from https://quarkus.io/blog/mutiny-back-pressure/):

    private static Long canOnlyConsumeOneItemPerSecond(long x) {
        try {
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        return Long.valueOf(x);
    }

and

  public static void main(String[] args) throws Exception {

        Cancellable x = Multi.createFrom().ticks().every(Duration.ofMillis(10))
                .emitOn(Infrastructure.getDefaultExecutor())
                .onItem().transform(MyExample::canOnlyConsumeOneItemPerSecond)
                .subscribe().with(
                        item -> System.out.println("Got item: " + item),
                        failure -> System.out.println("Got failure: " + failure)
                );

        Thread.sleep(2_000);
        System.out.println("done");
        x.cancel();
 } 

I indeed get a "Got Failure" message, but then the program "hangs". How do I cancel/stop you name it, that created ticker and free all of its resources. The above code with : Cancellable::cancel does not work. May be its a bug? May be my version matters?

Thank you.

@cescoffier
Copy link
Contributor

Well, you gets the failure(sending more item than the capacity), so no need to cancel. It does not hang, it is just 'done'.

When there is no failure, cancel would stop everything. You can also use .select().first(x) to cancel after x items.

@wind57
Copy link
Author

wind57 commented Feb 12, 2024

May be we have a different meaning of "hang". What I mean is that the program does not end: the VM is not terminated.

If I issue a jstack against that running process, I see threads that are blocking the VM to exit. Unfortunately, their names (pool-2-thread-1) do not tell me what code does that for me to dig deeper.

Do I make sense now?

@jponge
Copy link
Member

jponge commented Feb 12, 2024

This is because the default executor is a Executors.newCachedThreadPool() and is used by emitOn and is still active and prevents a JVM shutdown.

If you used another executor, or if you wait long enough it will start to prune internal threads and your JVM will exit.

@jponge
Copy link
Member

jponge commented Feb 12, 2024

Moving to a discussion, this is not an issue.

@jponge jponge closed this as completed Feb 12, 2024
@smallrye smallrye locked and limited conversation to collaborators Feb 12, 2024
@jponge jponge converted this issue into discussion #1515 Feb 12, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants