Description
Issue Description
Nacos-client 2.2.0
Tomcat 9.0
While stop web application in tomcat, a thread called "nacos.publisher-com.alibaba.nacos.common.notify.SlowEvent" still remain in JVM. This typical thread was created by DefaultPublisher class (in DefaultPublisher file line 65).
Describe what happened (or what feature you want)
I've tried to invoke shutdown() in contextDestroy but failed to remove it. This thread seems to be blocked by a BlockingQueue.take() method:
`
void openEventHandler() {
try {
// This variable is defined to resolve the problem which message overstock in the queue.
int waitTimes = 60;
// To ensure that messages are not lost, enable EventHandler when
// waiting for the first Subscriber to register
while (!shutdown && !hasSubscriber() && waitTimes > 0) {
ThreadUtils.sleep(1000L);
waitTimes--;
}
while (!shutdown) {
final Event event = queue.take();
receiveEvent(event);
UPDATER.compareAndSet(this, lastEventSequence, Math.max(lastEventSequence, event.sequence()));
}
} catch (Throwable ex) {
LOGGER.error("Event listener exception : ", ex);
}
}`
shutdown() method would set the shutdown parameter to true, but this while loop has been blocked by queue.take(), so it won't get a chance to execute next loop.
Additionally, there are serveral ShutdownHook that been registered but never unregistered:
ThreadUtils.addShutdownHook(NotifyCenter::shutdown);
ThreadUtils.addShutdownHook(HttpClientBeanHolder::shutdown);
In tomcat, i don't think these hooks will be executed, because those workload will be took over by contextDestory. so it will remain in JVM and keep a reference to those classes and prevent them from being garbage collected, correct me if wrong.
Describe what you expected to happen
nacos thread being garbage collected after web application stopped or share those thread instead of creating new one.
How to reproduce it (as minimally and precisely as possible)
- deploy a nacos config featured web application to Tomcat
- stop this web application
- open jvm trace tool to observe threads
Tell us your environment
Tomcat 9
Naocos client 2.2.0
Anything else we need to know?
- How could i actually stop all nacos threads when stop a web application?
- Is there a feature to not register shutdown hook when in a web environment.
Activity