FrankYang0529 commented on code in PR #16753:
URL: https://github.com/apache/kafka/pull/16753#discussion_r1703070639


##########
clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java:
##########
@@ -1196,16 +1196,31 @@ private long maybeDrainPendingCalls(long now) {
             long pollTimeout = Long.MAX_VALUE;
             log.trace("Trying to choose nodes for {} at {}", pendingCalls, 
now);
 
-            Iterator<Call> pendingIter = pendingCalls.iterator();
-            while (pendingIter.hasNext()) {
-                Call call = pendingIter.next();
+            List<Call> toRemove = new ArrayList<>();
+            // Using for-loop instead of iterator to avoid 
ConcurrentModificationException with following sequence:
+            // 1. In maybeDrainPendingCalls, pendingCalls.iterator() create a 
iterator.
+            // 2. In maybeDrainPendingCall, call.nodeProvider.provide() throws 
UnsupportedVersionException error.
+            // 3. In call.fail, runnable.pendingCalls.add modify the 
pendingCalls list.
+            // Using pendingCalls.size() to get the list size before the 
for-loop to avoid infinite loop.
+            // If call.fail keeps adding the call to pendingCalls,
+            // the loop like for (int i = 0; i < pendingCalls.size(); i++) 
can't stop.
+            int pendingSize = pendingCalls.size();
+            for (int i = 0; i < pendingSize; i++) {
+                Call call = pendingCalls.get(i);
                 // If the call is being retried, await the proper backoff 
before finding the node
                 if (now < call.nextAllowedTryMs) {
                     pollTimeout = Math.min(pollTimeout, call.nextAllowedTryMs 
- now);
-                } else if (maybeDrainPendingCall(call, now)) {
-                    pendingIter.remove();
+                    continue;
+                }
+                if (maybeDrainPendingCall(call, now)) {

Review Comment:
   Updated it. Thanks.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to