fix: handle exceptions during Nacos service shutdown to prevent NPE propagation#4286
Open
daguimu wants to merge 1 commit intoalibaba:2025.1.xfrom
Open
fix: handle exceptions during Nacos service shutdown to prevent NPE propagation#4286daguimu wants to merge 1 commit intoalibaba:2025.1.xfrom
daguimu wants to merge 1 commit intoalibaba:2025.1.xfrom
Conversation
…ropagation Fixes alibaba#4081 Fixes alibaba#4247
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe what this PR does / why we need it
During application shutdown,
NacosServiceManager.nacosServiceShutDown()callsnamingService.shutDown()which internally invokesNotifyCenter.deregisterSubscriber(). If the JVM shutdown hooks have already destroyedNotifyCenter.INSTANCE, this throws aNullPointerException:This NPE escapes
NacosServiceRegistry.close()(which only catchesNacosException) and propagates toNacosGracefulShutdownDelegate, causing an alarming ERROR log during every application shutdown.Additionally, when
namingService.shutDown()throws,namingMaintainService.shutDown()never gets a chance to execute.Does this pull request fix one issue?
Fixes #4081
Fixes #4247
Describe how you did it
Wrapped each
shutDown()call inNacosServiceManager.nacosServiceShutDown()with independent try-catch blocks:NacosExceptionis caught and logged at ERROR level (real failures)namingServiceandnamingMaintainServicealways get their shutdown attempts, even if one failsDescribe how to verify it
NacosGracefulShutdownDelegateduring shutdownNacosServiceManagerTests— all 6 tests passTests Added
namingService.shutDown()NPE caught at WARNtestNamingServiceNpeShutdownShouldNotPropagate()namingMaintainService.shutDown()NPE caught at WARNtestNamingMaintainServiceNpeShutdownShouldNotPropagate()testBothServicesFailShouldStillCleanUp()NacosExceptioncaught and logged at ERRORtestNacosExceptionDuringShutdownShouldNotPropagate()testNormalShutdownShouldSucceed()testNullServicesShouldNotFail()Special notes for reviews
throws NacosExceptionis preserved for backward compatibility, though exceptions are now handled internallyNacosServiceRegistry.close()catch block becomes dead code but is left unchanged to minimize diffNacosGracefulShutdownDelegateTests(4 tests) continue to pass