Program: 13098
Project: 13098
Buggy source:
private CompletableFuture<BitSet> createAndSendRequests(){
  Map<ShardId,ShardUpsertRequest> requestsByShard=groupRequests();
  if (requestsByShard.isEmpty()) {
    return CompletableFuture.completedFuture(new BitSet(0));
  }
  CompletableFuture<BitSet> result=new CompletableFuture<>();
  AtomicInteger numRequests=new AtomicInteger(requestsByShard.size());
  AtomicReference<Throwable> lastFailure=new AtomicReference<>(null);
  final BitSet responses=new BitSet();
  for (Iterator<Map.Entry<ShardId,ShardUpsertRequest>> it=requestsByShard.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<ShardId,ShardUpsertRequest> entry=it.next();
    ShardUpsertRequest request=entry.getValue();
    it.remove();
    ActionListener<ShardResponse> listener=new ActionListener<ShardResponse>(){
      @Override public void onResponse(      ShardResponse shardResponse){
        Throwable failure=shardResponse.failure();
        if (failure == null) {
          processShardResponse(responses,shardResponse);
        }
 else {
          lastFailure.set(failure);
        }
        countdown();
      }
      @Override public void onFailure(      Exception e){
        lastFailure.set(e);
        countdown();
      }
      private void countdown(){
        if (numRequests.decrementAndGet() == 0) {
          Throwable throwable=lastFailure.get();
          if (throwable == null) {
            result.complete(responses);
          }
 else {
            throwable=SQLExceptions.unwrap(throwable,t -> t instanceof RuntimeException);
            if (!SQLExceptions.isDocumentAlreadyExistsException(throwable) && (updateAffectedNoRows(throwable) || partitionWasDeleted(throwable,request.index()) || mixedArgumentTypesFailure(throwable,request.items()))) {
              result.complete(responses);
            }
 else {
              result.completeExceptionally(throwable);
            }
          }
        }
      }
    }
;
    upsertAction.execute(request,new RetryListener<>(scheduler,actionListener -> upsertAction.execute(request,actionListener),listener,BACK_OFF_POLICY));
  }
  return result;
}

Fixed version (ground truth):
private CompletableFuture<BitSet> createAndSendRequests(){
  Map<ShardId,ShardUpsertRequest> requestsByShard;
  try {
    requestsByShard=groupRequests();
  }
 catch (  Throwable t) {
    return failedFuture(t);
  }
  if (requestsByShard.isEmpty()) {
    return CompletableFuture.completedFuture(new BitSet(0));
  }
  CompletableFuture<BitSet> result=new CompletableFuture<>();
  AtomicInteger numRequests=new AtomicInteger(requestsByShard.size());
  AtomicReference<Throwable> lastFailure=new AtomicReference<>(null);
  final BitSet responses=new BitSet();
  for (Iterator<Map.Entry<ShardId,ShardUpsertRequest>> it=requestsByShard.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<ShardId,ShardUpsertRequest> entry=it.next();
    ShardUpsertRequest request=entry.getValue();
    it.remove();
    ActionListener<ShardResponse> listener=new ActionListener<ShardResponse>(){
      @Override public void onResponse(      ShardResponse shardResponse){
        Throwable failure=shardResponse.failure();
        if (failure == null) {
          processShardResponse(responses,shardResponse);
        }
 else {
          lastFailure.set(failure);
        }
        countdown();
      }
      @Override public void onFailure(      Exception e){
        lastFailure.set(e);
        countdown();
      }
      private void countdown(){
        if (numRequests.decrementAndGet() == 0) {
          Throwable throwable=lastFailure.get();
          if (throwable == null) {
            result.complete(responses);
          }
 else {
            throwable=SQLExceptions.unwrap(throwable,t -> t instanceof RuntimeException);
            if (!SQLExceptions.isDocumentAlreadyExistsException(throwable) && (updateAffectedNoRows(throwable) || partitionWasDeleted(throwable,request.index()) || mixedArgumentTypesFailure(throwable,request.items()))) {
              result.complete(responses);
            }
 else {
              result.completeExceptionally(throwable);
            }
          }
        }
      }
    }
;
    upsertAction.execute(request,new RetryListener<>(scheduler,actionListener -> upsertAction.execute(request,actionListener),listener,BACK_OFF_POLICY));
  }
  return result;
}