-
Notifications
You must be signed in to change notification settings - Fork 8.9k
bugfix: Resolve NullPointerException in EtcdRegistryServiceImplMockTest
#7349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 ReportAttention: Patch coverage is
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
🚀 New features to boost your workflow:
|
| if (this.watcher != null) { | ||
| this.watcher.close(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add a null check here. What do you think?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
It appears that, intermittently, the unsubscribe method is executed before the run method, leading to this issue.
- After adding a null check in the
stopmethod, the issue has not occurred again.
That's why i believe this piece of code is related to the NPE issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, the
watcheris initialized not in theconstructor, but within therunmethod of theEtcdWatcherclass. In the test code, we usedthenAnswerto return amockWatcher, ensuring that the watcher is properly initialized in the run method. However, when running the test10,000times using@RepeatedTest, we encountered 7 failures.All 7 failures were caused by a
NullPointerExceptiontriggered when thestopmethod was called while the watcher was still null.It appears that, intermittently, the unsubscribe method is executed before the run method, leading to this issue.
- After adding a null check in the
stopmethod, 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.
funky-eyes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM



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