Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@YongGoose
Copy link
Member

  • I have registered the PR changes.

Ⅰ. Describe what this PR did

fixes #7347

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@YongGoose YongGoose requested a review from Copilot May 19, 2025 10:48
Copy link
Contributor

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 NullPointerException in EtcdRegistryServiceImplMockTest by ensuring that the unsubscribe method in EtcdRegistryServiceImpl safely handles the scenario when no watcher exists for a cluster. It also includes minor code reordering, formatting improvements, and updated change log documentation.

  • Improved null checking in the unsubscribe method.
  • Reordered static imports and reformatted method chains for better readability.
  • Updated change log entries for both zh-cn and en-us.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
discovery/seata-discovery-etcd3/src/test/java/org/apache/seata/discovery/registry/etcd/EtcdRegistryServiceImplMockTest.java Minor import and formatting adjustments; added assertion to testUnsubscribe.
discovery/seata-discovery-etcd3/src/main/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImpl.java Refactored unsubscribe method to safely handle null watchers and reformatted several method calls.
changes/zh-cn/2.x.md, changes/en-us/2.x.md Updated change log entries to reflect the bugfix.
Comments suppressed due to low confidence (1)

discovery/seata-discovery-etcd3/src/main/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImpl.java:184

  • Replace the direct call to stop() on the result of watcherMap.remove(cluster) with a null check to ensure a NullPointerException does not occur when the watcher is absent, as done in the updated implementation.
watcherMap.remove(cluster).stop();

@codecov
Copy link

codecov bot commented May 19, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 6 lines in your changes missing coverage. Please review.

Project coverage is 54.90%. Comparing base (c5efbd9) to head (a0ed27f).
Report is 1 commits behind head on 2.x.

Files with missing lines Patch % Lines
...covery/registry/etcd3/EtcdRegistryServiceImpl.java 83.33% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                2.x    #7349      +/-   ##
============================================
+ Coverage     54.89%   54.90%   +0.01%     
- Complexity     7398     7399       +1     
============================================
  Files          1181     1181              
  Lines         42089    42106      +17     
  Branches       4934     4935       +1     
============================================
+ Hits          23103    23119      +16     
  Misses        16817    16817              
- Partials       2169     2170       +1     
Files with missing lines Coverage Δ
...covery/registry/etcd3/EtcdRegistryServiceImpl.java 71.52% <83.33%> (+1.37%) ⬆️

... and 1 file with indirect coverage changes

Impacted file tree graph

🚀 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.

Comment on lines +429 to +431
if (this.watcher != null) {
this.watcher.close();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@funky-eyes

I add a null check here. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't quite understand why the NPE in the unsubscribe is related to this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, the watcher is initialized not in the constructor, but within the run method of the EtcdWatcher class.
In the test code, we used thenAnswer to return a mockWatcher, ensuring that the watcher is properly initialized in the run method. However, when running the test 10,000 times using @RepeatedTest, we encountered 7 failures.

All 7 failures were caused by a NullPointerException triggered when the stop method was called while the watcher was still null.

image

It appears that, intermittently, the unsubscribe method is executed before the run method, leading to this issue.

  • After adding a null check in the stop method, the issue has not occurred again.
    That's why i believe this piece of code is related to the NPE issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, the watcher is initialized not in the constructor, but within the run method of the EtcdWatcher class. In the test code, we used thenAnswer to return a mockWatcher, ensuring that the watcher is properly initialized in the run method. However, when running the test 10,000 times using @RepeatedTest, we encountered 7 failures.

All 7 failures were caused by a NullPointerException triggered when the stop method was called while the watcher was still null.

image

It appears that, intermittently, the unsubscribe method is executed before the run method, leading to this issue.

  • After adding a null check in the stop method, the issue has not occurred again.
    That's why i believe this piece of code is related to the NPE issue.

I understand now, thank you for your explanation.

@YongGoose
Copy link
Member Author

image

@funky-eyes funky-eyes added this to the 2.5.0 milestone May 19, 2025
@funky-eyes funky-eyes added type: bug Category issues or prs related to bug. module/discovery discovery module type: test test case labels May 19, 2025
Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

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

LGTM

@funky-eyes funky-eyes merged commit 766be77 into apache:2.x May 19, 2025
9 checks passed
slievrly pushed a commit to slievrly/fescar that referenced this pull request Oct 21, 2025
YvCeung pushed a commit to YvCeung/incubator-seata that referenced this pull request Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module/discovery discovery module type: bug Category issues or prs related to bug. type: test test case

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EtcdRegistryServiceImplMockTest.testUnsubscribe:237 » NullPointer

2 participants