-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Closing NATS connection may not close all running threads #26
Comments
Example application that cannot exit: package com.sookocheff.example
import io.nats.client.Connection;
import io.nats.client.ConnectionFactory;
import io.nats.client.Constants;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Main {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory cf = new ConnectionFactory(Constants.DEFAULT_URL);
Connection nc = cf.createConnection();
nc.subscribe("foo", m -> {
System.out.println("Received a message: " + new String(m.getData()));
});
nc.close();
System.out.println("Exiting application");
}
} |
I can confirm this behavior but haven't had the time to produce a PR I'm proud of since AFAIK Quoting:
|
TBH I'm scared as hell of managing manually threads. However, @mcqueary is much more of a Java developer than I'll ever be so I bet he has a solution or two under the sleeve. |
I could take a stab at this but I'm having trouble getting the dev environment setup (can't find parent pom.xml when checking out a fresh copy of this repo). |
@kevinsookocheff-wf you need to clone https://github.com/nats-io/nats-parent-pom and |
Currently, AsyncSubscriptionImpl leaks the msgfeeder thread on unsubscribe because Channel.get() is never unblocked. This overrides unsubscribe so that the thread properly terminates. I believe this resolves issue nats-io#26.
@pires you flatter and overestimate me, sir. @kevinsookocheff-wf I do believe that PR #31 closes this, but will confirm. |
There are actually two cases to consider here.
Thanks Kevin/Tyler/Pires. |
…e that the msgFeeder thread is terminated for async subscriptions. Resolves #26
Resolved this for the connection close scenario (described above) by ensuring that the connection close triggers the subscription close, which will shut down the msgFeeder thread for async subscriptions. Test(s) also added to guard against future regression. |
Hi,
I'm working on getting a simplified reproducible example. Will update this issue when that is ready.
The general problem is that closing a NATS connection may not close all threads if there is work being done in ConnectionImpl.deliverMsgs. I looks like if Channel.get() is called from deliverMsgs it calls q.take() (Channel.java:73), which blocks until data is available. I believe that calling interrupt() in Channel.close() will allow Channel.get() to unblock and exit correctly.
Below is a thread dump from
jstack
of an app that cannot exit because of hung NATS threads.The text was updated successfully, but these errors were encountered: