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

Dispatcher unsubscribe should throw if the wrong method is used #1232

Open
OS-tariqabbasi opened this issue Oct 3, 2024 · 0 comments
Open
Labels
proposal Enhancement idea or proposal

Comments

@OS-tariqabbasi
Copy link

OS-tariqabbasi commented Oct 3, 2024

Proposed change

The Dispatcher interface provides overloaded unsubscribe methods, unfortunately it is quite easy for someone to choose the incorrect method and this doesn't produce any kind of error but has the potentially dangerous side-effect of not actually closing the subscription. Please consider throwing an exception or at least logging a warning in the library when unsubscribe is used this way and the subscription is not found on the default handler.

Options.Builder builder = new Options.Builder().server("nats://localhost:4222");
try (Connection conn = Nats.connect(builder.build())) {
    Dispatcher d = conn.createDispatcher();
    d.subscribe("test", msg -> {});
    ...
    d.unsubscribe("test");
}

Use case

In the example above, the unsubscribe call does not throw but also it does not actually unsubscribe. In applications that manage subscriptions dynamically, this can result in a huge number of subscriptions being quietly left open on the server resulting in performance degradation and eventual OOM. The mistake is also easily missed in code review and by static analysis tools. The behavior is also difficult to validate in tests because the library does not provide a way to check the number of active subscriptions.

Contribution

No response

@OS-tariqabbasi OS-tariqabbasi added the proposal Enhancement idea or proposal label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Enhancement idea or proposal
Projects
None yet
Development

No branches or pull requests

1 participant