[Fix-15886] Fix thread leak in NacosRegistry using Double-Checked Locking#15915
[Fix-15886] Fix thread leak in NacosRegistry using Double-Checked Locking#15915QiuYucheng2003 wants to merge 4 commits intoapache:3.3from
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #15915 +/- ##
=========================================
Coverage 60.74% 60.74%
+ Complexity 11754 11746 -8
=========================================
Files 1949 1949
Lines 88898 88901 +3
Branches 13407 13408 +1
=========================================
+ Hits 53998 54001 +3
- Misses 29337 29342 +5
+ Partials 5563 5558 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Regarding the Codecov report: Since this PR fixes a race condition via Double-Checked Locking, it is difficult to reproduce the concurrency scenario deterministically in unit tests, hence the 0% coverage on the new lines |
There was a problem hiding this comment.
Pull request overview
This PR fixes a thread leak issue in NacosRegistry caused by a race condition during lazy initialization of the scheduledExecutorService. The fix implements double-checked locking to ensure the executor service is created only once, even when multiple threads attempt concurrent initialization.
Key Changes:
- Applied double-checked locking pattern in
scheduleServiceNamesLookupmethod to prevent multiple executor service instances from being created - Added synchronized block around executor initialization to ensure thread-safe singleton instantiation
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| TimeUnit.SECONDS); | ||
| synchronized (this) { | ||
| if (scheduledExecutorService == null) { | ||
| scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); |
There was a problem hiding this comment.
Potential race condition. This assignment to scheduledExecutorService is visible to other threads before the subsequent statements are executed.
There was a problem hiding this comment.
Verified that scheduledExecutorService is declared as volatile, so this DCL pattern is thread-safe.
What is the purpose of the change
Fixes #15886
This PR fixes a thread leak issue in
NacosRegistry.java.The method
scheduleServiceNamesLookuphad a race condition where multiple threads could pass theif (scheduledExecutorService == null)check simultaneously, leading to the creation of multipleScheduledExecutorServiceinstances that were never shut down.Brief changelog
scheduleServiceNamesLookupto ensure the executor service is instantiated safely and only once.Verifying this change