Skip to content

Nacos Client: DefaultPublisher thread not destroyed while stop web application in tomcat #12444

Open
@FrioSea

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)

  1. deploy a nacos config featured web application to Tomcat
  2. stop this web application
  3. open jvm trace tool to observe threads

Tell us your environment

Tomcat 9
Naocos client 2.2.0

Anything else we need to know?

  1. How could i actually stop all nacos threads when stop a web application?
  2. Is there a feature to not register shutdown hook when in a web environment.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area/ClientRelated to Nacos Client SDKkind/enhancementCategory issues or prs related to enhancement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions