Skip to content

fix: handle NPE when Nacos NotifyCenter.INSTANCE is null during graceful shutdown#4283

Open
Senrian wants to merge 1 commit intoalibaba:2025.1.xfrom
Senrian:fix-nacos-shutdown-npe
Open

fix: handle NPE when Nacos NotifyCenter.INSTANCE is null during graceful shutdown#4283
Senrian wants to merge 1 commit intoalibaba:2025.1.xfrom
Senrian:fix-nacos-shutdown-npe

Conversation

@Senrian
Copy link

@Senrian Senrian commented Mar 20, 2026

What

When Spring context closes, NacosGracefulShutdownDelegate calls autoServiceRegistration.stop() which internally deregisters subscribers from Nacos NotifyCenter. However, if the Nacos client shutdown hooks have already destroyed NotifyCenter.INSTANCE, this results in:

java.lang.NullPointerException: Cannot read field "sharePublisher" because "com.alibaba.nacos.common.notify.NotifyCenter.INSTANCE" is null
  at com.alibaba.nacos.common.notify.NotifyCenter.deregisterSubscriber(NotifyCenter.java:242)
  at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.shutdown(NamingGrpcClientProxy.java:537)
  at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.shutdown(NamingClientProxyDelegate.java:212)
  at com.alibaba.nacos.client.naming.NacosNamingService.shutDown(NacosNamingService.java:637)
  at com.alibaba.cloud.nacos.NacosServiceManager.nacosServiceShutDown(NacosServiceManager.java:117)
  at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.close(NacosServiceRegistry.java:120)

Fix

Wrap the stop() call in an inner try-catch to gracefully handle this race condition by logging a warning instead of propagating the error.

Testing

  • Existing NacosGracefulShutdownDelegateTests continue to pass

Related Issues

Fixes #4247
Fixes #4081

…utdown

When Spring context closes, NacosGracefulShutdownDelegate calls autoServiceRegistration.stop()
which internally deregisters subscribers from Nacos NotifyCenter. However, if the Nacos
client shutdown hooks have already destroyed NotifyCenter.INSTANCE, this results in:

  NullPointerException: Cannot read field "sharePublisher" because "NotifyCenter.INSTANCE" is null

This fix wraps the stop() call in an inner try-catch to gracefully handle this race condition
by logging a warning instead of propagating the error.

Fixes alibaba#4247, Fixes alibaba#4081
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@uuuyuqi
Copy link
Collaborator

uuuyuqi commented Mar 21, 2026

Thanks for your contribution. Please note that you need to sign the CLA first. Before we proceed with the review, I'd like to confirm if you have verified this? Does the issue still persist in the current nacos-client:3.1.1?

Senrian added a commit to Senrian/spring-cloud-alibaba that referenced this pull request Mar 22, 2026
…utdown

When Spring context closes, if Nacos client shutdown hooks have already
destroyed NotifyCenter.INSTANCE, autoServiceRegistration.stop() throws NPE.
Catch NPE explicitly and log a warning instead of propagating.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

应用关闭时报错 Error occurred while performing Nacos client graceful shutdown

3 participants