-
Notifications
You must be signed in to change notification settings - Fork 158
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
NATS client leaves dangling threads after close, if NATS server was down when drain & close were invoked #324
Comments
@adamkotwasinski , thanks for submitting this issue. I have verified it and am now weighing my options for a fix. |
@sasbury @ColinSullivan1 , this leak happens because the fetch inside drain fails (
When flush throws an exception, I see two options:
I'm relatively new to java's futures api, do you have a preference? |
I see the comment that exceptions in flush were intended to be thrown back to the caller... So I suggest wrapping the flush in a try/catch, then in the catch cleanup up resources ( e.g. something like: try {
this.flush(timeout); // Flush and wait up to the timeout, if this fails, let the caller know
} catch (Exception e) {
this.close(false);
throw e;
} There may be more cleanup to do but I think this could resolve the problem and keep the original behavior (and the authors intent). |
Fixeds #324 Signed-off-by: Matthias Hanel <mh@synadia.com>
Fixeds #324 Signed-off-by: Matthias Hanel <mh@synadia.com>
Hey, first and foremost thanks for creating NATS Java client.
I found that NATS connection leaves dangling non-daemon threads after
.drain
&.close
methods have been invoked, potentially leaking resources.Very simple example (run it with NATS server running):
what results in main thread finishing, but us having non-daemon
NATS Connection Timer
thread and one of (potentially daemon) worker threads:After some time, the worker thread finally finishes, but it turns out that Connection Timer was never cleaned up:
I think the cause is that it's possible for
close()
to end early in https://github.com/nats-io/nats.java/blob/2.6.8/src/main/java/io/nats/client/impl/NatsConnection.java#L624 (after all, we wanted to drain, but just failed).Changing worker threads to daemons would not help, as the timer itself is not a daemon (https://github.com/nats-io/nats.java/blob/2.6.8/src/main/java/io/nats/client/impl/NatsConnection.java#L462). And anyways, that would not be viable for users that want to initialize NATS connection multiple times over lifetime of VM (e.g. in application containers).
The text was updated successfully, but these errors were encountered: