YongGoose commented on code in PR #7349:
URL: https://github.com/apache/incubator-seata/pull/7349#discussion_r2095714511


##########
discovery/seata-discovery-etcd3/src/main/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImpl.java:
##########
@@ -401,20 +411,24 @@ public EtcdWatcher(String cluster, Watch.Listener 
listener) {
         @Override
         public void run() {
             Watch watchClient = getClient().getWatchClient();
-            WatchOption.Builder watchOptionBuilder = 
WatchOption.newBuilder().withPrefix(buildRegistryKeyPrefix(cluster));
+            WatchOption.Builder watchOptionBuilder =
+                    
WatchOption.newBuilder().withPrefix(buildRegistryKeyPrefix(cluster));
             Pair<Long /*revision*/, List<InetSocketAddress>> addressPair = 
clusterAddressMap.get(cluster);
             if (Objects.nonNull(addressPair)) {
                 // Maybe addressPair isn't newest now, but it's ok
                 watchOptionBuilder.withRevision(addressPair.getKey());
             }
-            this.watcher = watchClient.watch(buildRegistryKeyPrefix(cluster), 
watchOptionBuilder.build(), this.listener);
+            this.watcher =
+                    watchClient.watch(buildRegistryKeyPrefix(cluster), 
watchOptionBuilder.build(), this.listener);
         }
 
         /**
          * stop this task
          */
         public void stop() {
-            this.watcher.close();
+            if (this.watcher != null) {
+                this.watcher.close();
+            }

Review Comment:
   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](https://github.com/user-attachments/assets/718007ab-8307-45a1-88e2-f97242382ca4)
   
   
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to