fix: deliver request-adaptation failures through executeAsync's future#119
Merged
Conversation
Both transports adapted the request synchronously at the top of executeAsync, so a failure raised while adapting (a CONNECT request the JDK client rejects, or a method/body mismatch OkHttp rejects) was thrown on the caller's thread instead of completing the returned future exceptionally. A caller composing on the future with .exceptionally or .handle would never observe it. executeAsync now catches adaptation failures and returns a future completed exceptionally, matching its documented "completes exceptionally on error" contract and giving async callers a single error channel. The OkHttp module targets Java 8, so it completes the future manually rather than using CompletableFuture.failedFuture (JDK 9+). Closes #118
…ilures In executeAsync, request adaptation runs synchronously on the caller's thread before the future is returned. Catching Exception (rather than Throwable) routes every recoverable adaptation failure into the future while letting JVM-fatal Errors (OutOfMemoryError, StackOverflowError) propagate up the caller's stack, where they belong, instead of packaging them into a future that may never be awaited. The OkHttp onResponse callback intentionally keeps catching Throwable: it runs on a dispatcher thread where an escaped Throwable would strand the future uncompleted, so there it is the only way to guarantee completion.
This was referenced Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both transports adapt the request synchronously at the top of
executeAsync, before returning theCompletableFuture<Response>:A failure raised while adapting the request was therefore thrown on the caller's thread, not delivered through the returned future. A caller composing on the future never observes it:
Reachable adaptation failures today:
IllegalArgumentExceptionfor aCONNECTrequest (the JDK client reserves CONNECT for tunnelling).IllegalArgumentExceptionfromRequest.Builder.methodfor a method/body mismatch (e.g. a body on aGET).Both methods already document that they "complete exceptionally with the transport failure on error", so the synchronous throw violated their own contract.
Change
executeAsyncnow catches an adaptation failure and returns a future completed exceptionally, so every post-call error flows through one channel. The synchronousexecutepath is unchanged — it keeps throwing, honouring its@Throws(IOException)contract.The OkHttp module targets Java 8, where
CompletableFuture.failedFuture(JDK 9+) is unavailable, so it completes a fresh future manually via a smallfailedFuturehelper; the JDK module (Java 11) usesCompletableFuture.failedFuture.Tests
GET(which OkHttp rejects during adaptation) now yields a future that completes exceptionally with theIllegalArgumentExceptionas its cause, instead of throwing synchronously.CONNECTrequest (rejected during adaptation) likewise surfaces through the future.Both tests fail against the pre-change code (the synchronous throw escapes before the future is returned) and pass after.
Internal change to the transport modules only — no public API change.
Closes #118