d-wang-commit commented on code in PR #1710:
URL: https://github.com/apache/ratis/pull/1710#discussion_r4704844499


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/TransferLeadership.java:
##########
@@ -317,7 +317,9 @@ private CompletableFuture<RaftClientReply> start(Context 
context) {
     final Result result = tryTransferLeadership(context);
     final Result.Type type = result.getType();
     if (type != Result.Type.SUCCESS && type != Result.Type.NOT_UP_TO_DATE) {
-      pendingRequest.complete(result);
+      if (pending.compareAndSet(pendingRequest, null)) {
+        pendingRequest.complete(result);
+      }

Review Comment:
   If the CAS fails, the callback that took this request out of `pending` is 
responsible for completing its future. For example, the follower append-reply 
path calls `TransferLeadership.complete(result)`, which does 
`pending.getAndSet(null)` and then `r.complete(result)` on the request it just 
removed. `start()` only installs a new request when the slot is empty.
   
   There is a small window between those two operations: the callback has taken 
the request, but its future may still be incomplete. Calling 
`pendingRequest.complete(result)` unconditionally in that window could publish 
the immediate failure before the callback publishes its own result. Keeping the 
call inside the `if` lets the path that removed the request decide the reply. 
[Completion 
path](https://github.com/apache/ratis/blob/27cb36256709d8d77e0516a54a65f58a19a3503b/ratis-server/src/main/java/org/apache/ratis/server/impl/TransferLeadership.java#L360-L363)



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