Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A REST endpoint that builds a response from a CompletableFuture chain sometimes hangs forever, and no exception is logged. The chain uses thenApply and thenCompose, and one stage manually completes a CompletableFuture from a callback API. What are the likely causes?

A chain that hangs with nothing logged usually has one of three causes: the callback adapter has a path that never calls complete or completeExceptionally, so the future never finishes; an exception was stored in a future that nobody joins or handles, so it is never logged; or a stage blocks on another future from a pool that is already full, which deadlocks. The fixes are to wrap callback adapters so every path completes the future, including errors, thrown exceptions and timeouts, to add orTimeout at the end of the chain with logging in whenComplete, and to never block inside a stage.

Why this question is asked

A request that hangs forever is one of the worst failures to debug: there is no stack trace, no error log, just a client that eventually gives up. CompletableFuture chains are a frequent source of it, because a future that is never completed looks exactly like a slow one, and an exception stored in a future is invisible until somebody asks. Interviewers use this scenario to see whether we know how futures complete and where errors go. In this answer, we will recap how completion flows through a chain, examine each likely cause with code, and then apply fixes that make every hang either impossible or loud.

1A Quick Recap: How Completion Flows Through a Chain

Each stage of a chain, such as thenApply or thenCompose, creates a new CompletableFuture that depends on the previous one. When a future completes, it runs its dependent stages, which complete their own futures, and so on to the end. thenApply transforms a value with a function; thenCompose takes a function that returns another future and waits for that one, which is how asynchronous calls are chained.

Two rules follow. First, nothing happens downstream until the upstream future completes, so if one future is never completed, every stage after it waits forever. Second, if a stage throws, its future completes exceptionally and every later stage is skipped, passing the exception along, until a handler like exceptionally, handle or whenComplete sees it. If no handler exists and nobody calls join(), the exception sits in the future and is never logged.

2Cause 1: A Callback Path That Never Completes the Future

The question says one stage completes a CompletableFuture manually from a callback API. That adapter is the first suspect.

Java
CompletableFuture<Price> lookupPrice(String sku) {
CompletableFuture<Price> f = new CompletableFuture<>();
legacyClient.lookup(sku, new PriceCallback() {
public void onResult(Price p) {
if (p != null) {
f.complete(p); // a null price: nothing happens, f never completes
}
}
public void onError(Exception e) {
log.debug("lookup failed", e); // logged at debug, but f is never completed
}
});
return f;
}
// in the chain:
.thenCompose(order -> lookupPrice(order.sku())) // waits on f forever
Step throughA missing completion hangs the whole chain
ControllerChain stagesPrice adapterLegacy client
build response: thenApply, thenCompose
lookupPrice(sku): new CompletableFuture f
lookup(sku, callback)
onError(e): logged at debug only
f is never completed
later stages wait on f forever
no response, no exception, no log

7 messages in order. Play them, or step through them one at a time.

A future nobody completes looks exactly like a slow one.

There are three holes here. A null result completes nothing, an error is logged at a level nobody sees but does not complete the future, and if legacyClient.lookup throws synchronously, for example because the client is closed, the exception escapes before the callback is registered and f is returned incomplete. A fourth hole is a callback that is simply never called, for example when the legacy client drops a request on reconnect. In each case, thenCompose waits on f forever, the HTTP request hangs, and nothing is logged at a normal level.

3Cause 2: Exceptions Stored Where Nobody Looks

The second cause explains why nothing is logged even when something did fail. When a function inside thenApply throws, CompletableFuture catches the exception and stores it in that stage's future. It does not print it or log it. If the chain ends with thenAccept and nobody calls join() or attaches a handler, the exception lives in an unreachable future and is garbage collected with it.

Java
CompletableFuture.supplyAsync(() -> loadOrder(id), io)
.thenApply(order -> order.lines().get(0)) // IndexOutOfBoundsException for an empty order
.thenAccept(line -> response.write(line)); // skipped; the exception is stored, not logged
// No join(), no exceptionally(): the failure is silent, and the response is never written

So the endpoint hangs from the client's point of view, because nothing writes a response, even though the work failed in milliseconds. The cause is the missing terminal handler, not a slow call.

4Cause 3: Blocking Inside a Stage on a Full Pool

The third cause is a real deadlock. If a stage running on a pool's thread calls join() on another future whose task is queued on the same pool, and every thread of the pool is doing the same, no thread is free to run the tasks they are all waiting for.

Java
ExecutorService pool = Executors.newFixedThreadPool(4);
CompletableFuture<Report> report = CompletableFuture.supplyAsync(() -> {
CompletableFuture<Totals> totals = CompletableFuture.supplyAsync(this::computeTotals, pool);
return new Report(totals.join()); // blocks a pool thread waiting for a task in the same pool
}, pool);
// With 4 or more concurrent reports, all 4 threads block in join() and computeTotals never runs.

Under light load this works, because a free thread runs the inner task, which is why the hang appears only sometimes. A thread dump is the decisive diagnostic: here it shows all pool threads waiting in CompletableFuture.join, whereas in causes 1 and 2 no thread is waiting at all, because a future that is never completed occupies no thread. That difference tells us which kind of hang we have.

5Fixing It: Every Path Completes, and Hangs Become Loud

First, wrap the callback adapter so that every path completes the future: success, null results, errors, synchronous exceptions, and silence.

Java
CompletableFuture<Price> lookupPrice(String sku) {
CompletableFuture<Price> f = new CompletableFuture<>();
try {
legacyClient.lookup(sku, new PriceCallback() {
public void onResult(Price p) {
if (p != null) f.complete(p);
else f.completeExceptionally(new PriceNotFoundException(sku));
}
public void onError(Exception e) { f.completeExceptionally(e); }
});
} catch (RuntimeException e) {
f.completeExceptionally(e); // the call itself failed
}
return f.orTimeout(3, TimeUnit.SECONDS); // the callback never came
}
// End of the chain: a deadline and a log line for every outcome
return chain
.orTimeout(5, TimeUnit.SECONDS)
.whenComplete((result, ex) -> {
if (ex != null) log.warn("order response for {} failed", orderId, ex);
});

The adapter now completes on every path and gives up after three seconds if the legacy client goes silent. At the end of the chain, orTimeout guarantees that the endpoint answers within five seconds no matter which stage got stuck, and whenComplete logs the failure, including stored exceptions from cause 2, at a level we actually see. The web layer then turns the exceptional completion into an error response instead of hanging.

For cause 3, the rule is never to block inside a stage. Compose instead: thenCompose(x -> computeTotalsAsync()) or thenCombine let the chain wait without holding a thread. If blocking cannot be avoided, the inner work must run on a different executor from the one blocking on it, or on virtual threads, where a blocked thread does not hold a scarce pool slot. In Spring MVC, an async request timeout, configured with spring.mvc.async.request-timeout, is a last line of defence; check what the default is for your container.

6Follow-up questions

How can you find a never-completed future in production?

A thread dump shows no thread waiting, which itself is the clue. Metrics help more: time each chain from start to completion and count completions; a growing gap between started and completed requests reveals futures that never finish. Adding orTimeout everywhere converts these invisible hangs into TimeoutExceptions that show up in logs and error rates.

Why not just log inside every stage?

Logging in every stage duplicates the same error many times as it propagates, and still misses futures that never complete. One whenComplete at the end of the chain, plus a timeout, sees every outcome once, and logging inside adapters is reserved for context only the adapter knows.

7Conclusion

In this answer, we saw that a CompletableFuture chain hangs silently when a manually completed future has a path that never calls complete or completeExceptionally, when an exception is stored in a future that nobody joins or handles, or when a stage blocks on another future from the same full pool. Wrapping callback adapters so every path completes the future, adding orTimeout at the end of the chain with logging in whenComplete, and composing instead of blocking inside stages make every hang either impossible or visible, and a thread dump tells the deadlock apart from a missing completion.

What to say in the interviewAbout 90 seconds aloud

Open with

I would look at three things. First, the stage that completes a CompletableFuture manually from a callback. If any path does not call complete or completeExceptionally, like a null result that is ignored, an onError that only logs, the lookup call itself throwing before the callback is registered, or a callback that never fires, then that future never completes and every later stage waits forever, with nothing logged.

Then, if they want more

Second, exceptions in stages are stored in the future, not logged, so if the chain has no exceptionally, handle or whenComplete and nobody joins it, a failure just disappears and the response is never written.

Third, a stage that calls join on another future whose task is queued on the same pool can deadlock once all threads are blocked, which explains why it happens only under load. A thread dump separates the cases: threads waiting in join means the deadlock, no waiting threads means a future that is never completed.

To fix it, I wrap the callback adapter so every path completes the future, including a try/catch around the call and an orTimeout in case the callback never comes. At the end of the chain I add orTimeout and a whenComplete that logs failures, so the endpoint always answers.

And I never block inside a stage; I use thenCompose or thenCombine instead, or separate executors if blocking is unavoidable.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A REST endpoint that builds a response from a CompletableFuture chain sometimes hangs...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/8ed5e481a7/