Skip to content

Conversation

@QiuYucheng2003
Copy link

What is the purpose of the change

Fixes #15886

This PR fixes a thread leak issue in NacosRegistry.java.

The method scheduleServiceNamesLookup had a race condition where multiple threads could pass the if (scheduledExecutorService == null) check simultaneously, leading to the creation of multiple ScheduledExecutorService instances that were never shut down.

Brief changelog

  • Applied Double-Checked Locking in scheduleServiceNamesLookup to ensure the executor service is instantiated safely and only once.

Verifying this change

  • Verified via code logic analysis (fixing the race condition).
  • Existing unit tests should pass.

@codecov-commenter
Copy link

codecov-commenter commented Dec 24, 2025

Codecov Report

❌ Patch coverage is 0% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.75%. Comparing base (a3a35b5) to head (502e794).

Files with missing lines Patch % Lines
...org/apache/dubbo/registry/nacos/NacosRegistry.java 0.00% 17 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #15915      +/-   ##
============================================
+ Coverage     60.73%   60.75%   +0.01%     
- Complexity    11702    11704       +2     
============================================
  Files          1938     1938              
  Lines         88694    88697       +3     
  Branches      13387    13388       +1     
============================================
+ Hits          53869    53884      +15     
+ Misses        29294    29288       -6     
+ Partials       5531     5525       -6     
Flag Coverage Δ
integration-tests-java21 32.36% <0.00%> (-0.01%) ⬇️
integration-tests-java8 32.53% <0.00%> (+<0.01%) ⬆️
samples-tests-java21 32.00% <0.00%> (-0.02%) ⬇️
samples-tests-java8 29.73% <0.00%> (+0.04%) ⬆️
unit-tests-java11 59.04% <0.00%> (-0.01%) ⬇️
unit-tests-java17 58.53% <0.00%> (?)
unit-tests-java21 58.53% <0.00%> (?)
unit-tests-java25 58.48% <0.00%> (+0.01%) ⬆️
unit-tests-java8 59.04% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@QiuYucheng2003
Copy link
Author

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

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 scheduleServiceNamesLookup method 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();
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential race condition. This assignment to scheduledExecutorService is visible to other threads before the subsequent statements are executed.

Copilot uses AI. Check for mistakes.
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.

[Bug] Thread leak in NacosRegistry.scheduleServiceNamesLookup due to race condition

2 participants