On an application where I run high traffic, I observe that sometimes post the completion of CompletableFuture the thenAccept is called but whenComplete is not. This issue is intermittent and tends to occur randomly and in a testing environment. I can reproduce it consistently in the end when I kill the running traffic and observe this behaviour. Has anyone seen such behaviour before?
This is where I initially register the exceptionally and whenComplete
rspCompFuture = rspCompFuture.exceptionally(ex->{
logger.debug("Request: rspCompFuture is in exceptionally handling");
ResponseEntity<byte[]> rsp = poolObj.getContext().exceptionallyCleanUp(ex, poolObj);
return rsp;
}).whenComplete((rsp, exe) -> {
logger.debug("Request: rspCompFuture going in the when complete");
AppMetrics.getInstance().pegWhenCompleteCalled();
poolObj.resetContext();
PoolMgr.returnObject(poolObj);
logger.debug("Completable Future Final Result onCompletion");
});
return rspCompFuture;
}
This is registered in between the code flow
rspCompFuture
.orTimeout(getTimeout(poolObj), TimeUnit.MILLISECONDS)
.thenAccept(rsp->{
poolObj.setAvailable(false);
AppMetrics.getInstance().pegThenAcceptCalled();
logger.debug("Transaction is completed");
});
So, once the future is completed this thenAccept is executed but it somehow just ignores the whenComplete call alltogether.