Program: 38899
Project: 38899
Buggy source:
@Override public <R>CompletableFuture<R> execute(final ExecutionEnvironment environment,final ExecutionHandler<R> handler){
  Objects.requireNonNull(environment);
  Objects.requireNonNull(handler);
class AsyncContext {
    WorkingDirectory workingDir;
    List<CommandExecution> parametersList;
    ExecutionReport report;
  }
  return CompletableFuture.completedFuture(new AsyncContext()).thenApplyAsync(ctxt -> {
    try {
      ctxt.workingDir=new WorkingDirectory(localDir,environment.getWorkingDirPrefix(),environment.isDebug());
    }
 catch (    IOException e) {
      throw new UncheckedIOException(e);
    }
    try {
      ctxt.parametersList=handler.before(ctxt.workingDir.toPath());
    }
 catch (    Throwable t) {
      try {
        ctxt.workingDir.close();
      }
 catch (      IOException e2) {
        throw new UncheckedIOException(e2);
      }
      throw new RuntimeException(t);
    }
    return ctxt;
  }
,executorContext.getComputationExecutor()).thenComposeAsync(ctxt -> {
    if (ctxt.parametersList.isEmpty()) {
      ctxt.report=new ExecutionReport(Collections.emptyList());
      return CompletableFuture.completedFuture(ctxt);
    }
 else {
      CompletableFuture<ExecutionReport> last=null;
      for (      CommandExecution execution : ctxt.parametersList) {
        ExecutionListener l=new DefaultExecutionListener(){
          @Override public void onExecutionStart(          int fromExecutionIndex,          int toExecutionIndex){
            try {
              for (int executionIndex=fromExecutionIndex; executionIndex <= toExecutionIndex; executionIndex++) {
                handler.onExecutionStart(execution,executionIndex);
              }
            }
 catch (            Exception e) {
              LOGGER.error(e.toString(),e);
            }
          }
          @Override public void onExecutionCompletion(          int executionIndex){
            try {
              handler.onExecutionCompletion(execution,executionIndex);
            }
 catch (            Exception e) {
              LOGGER.error(e.toString(),e);
            }
          }
        }
;
        if (last == null) {
          last=scheduler.execute(execution,ctxt.workingDir.toPath(),environment.getVariables(),l);
        }
 else {
          last=last.thenCompose(report -> {
            if (report.getErrors().isEmpty()) {
              return scheduler.execute(execution,ctxt.workingDir.toPath(),environment.getVariables(),l);
            }
 else {
              return CompletableFuture.completedFuture(report);
            }
          }
);
        }
      }
      return last.thenApply(report -> {
        ctxt.report=report;
        return ctxt;
      }
);
    }
  }
,executorContext.getComputationExecutor()).thenApplyAsync(ctxt -> {
    try {
      return handler.after(ctxt.workingDir.toPath(),ctxt.report);
    }
 catch (    IOException e) {
      throw new UncheckedIOException(e);
    }
 finally {
      try {
        ctxt.workingDir.close();
      }
 catch (      IOException e2) {
        throw new UncheckedIOException(e2);
      }
    }
  }
,executorContext.getComputationExecutor());
}

Fixed version (ground truth):
@Override public <R>CompletableFuture<R> execute(final ExecutionEnvironment environment,final ExecutionHandler<R> handler){
  Objects.requireNonNull(environment);
  Objects.requireNonNull(handler);
class AsyncContext {
    WorkingDirectory workingDir;
    List<CommandExecution> parametersList;
    ExecutionReport report;
  }
  return CompletableFuture.completedFuture(new AsyncContext()).thenApplyAsync(ctxt -> {
    try {
      ctxt.workingDir=new WorkingDirectory(localDir,environment.getWorkingDirPrefix(),environment.isDebug());
    }
 catch (    IOException e) {
      throw new UncheckedIOException(e);
    }
    try {
      ctxt.parametersList=handler.before(ctxt.workingDir.toPath());
    }
 catch (    Throwable t) {
      try {
        ctxt.workingDir.close();
      }
 catch (      IOException e2) {
        LOGGER.error(e2.toString(),e2);
      }
      throw new RuntimeException(t);
    }
    return ctxt;
  }
,executorContext.getComputationExecutor()).thenComposeAsync(ctxt -> {
    if (ctxt.parametersList.isEmpty()) {
      ctxt.report=new ExecutionReport(Collections.emptyList());
      return CompletableFuture.completedFuture(ctxt);
    }
 else {
      CompletableFuture<ExecutionReport> last=null;
      for (      CommandExecution execution : ctxt.parametersList) {
        ExecutionListener l=new DefaultExecutionListener(){
          @Override public void onExecutionStart(          int fromExecutionIndex,          int toExecutionIndex){
            try {
              for (int executionIndex=fromExecutionIndex; executionIndex <= toExecutionIndex; executionIndex++) {
                handler.onExecutionStart(execution,executionIndex);
              }
            }
 catch (            Exception e) {
              LOGGER.error(e.toString(),e);
            }
          }
          @Override public void onExecutionCompletion(          int executionIndex){
            try {
              handler.onExecutionCompletion(execution,executionIndex);
            }
 catch (            Exception e) {
              LOGGER.error(e.toString(),e);
            }
          }
        }
;
        if (last == null) {
          last=scheduler.execute(execution,ctxt.workingDir.toPath(),environment.getVariables(),l);
        }
 else {
          last=last.thenCompose(report -> {
            if (report.getErrors().isEmpty()) {
              return scheduler.execute(execution,ctxt.workingDir.toPath(),environment.getVariables(),l);
            }
 else {
              return CompletableFuture.completedFuture(report);
            }
          }
);
        }
      }
      return last.thenApply(report -> {
        ctxt.report=report;
        return ctxt;
      }
);
    }
  }
,executorContext.getComputationExecutor()).thenApplyAsync(ctxt -> {
    try {
      return handler.after(ctxt.workingDir.toPath(),ctxt.report);
    }
 catch (    IOException e) {
      throw new UncheckedIOException(e);
    }
 finally {
      try {
        ctxt.workingDir.close();
      }
 catch (      IOException e2) {
        LOGGER.error(e2.toString(),e2);
      }
    }
  }
,executorContext.getComputationExecutor());
}