Skip to content

Fail a request whose replay throws before it is sent - #2415

Merged
hyperxpro merged 1 commit into
AsyncHttpClient:mainfrom
mkurz:fix/abort-failed-replay
Nov 27, 2026
Merged

hyperxpro merged 1 commit into
AsyncHttpClient:mainfrom
mkurz:fix/abort-failed-replay

Conversation

@mkurz

@mkurz mkurz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Motivation: When a filter asks for a replay, NettyRequestSender.replayRequest hands the previous channel over and then calls sendNextRequest, which builds the replayed request and can throw, for example when the request's body file is gone. replayRequest let that exception through.

For a replay that an IOExceptionFilter asks for from a handler's catch block, after an IOException, the exception then left that catch block and reached exceptionCaught:

  • Over HTTP/1.1, the previous channel's drain callback still names the future, so exceptionCaught failed the request.
  • Over HTTP/2, the stream channel is discarded before the replay (since Stabilize HTTP/2: fix resource leaks and RFC conformance #2197), so exceptionCaught found no future, and the request waited until its request timeout, 60 seconds by default.

Example: a POST with a File body over HTTP/2 gets a 307 after the file was deleted. The redirect refuses to resend the missing file with an IOException, an IOExceptionFilter replays the request, and building that replay throws IllegalArgumentException from NettyFileBody.

Modification: replayRequest aborts the future itself when sendNextRequest throws, as retry() does for the same call. It passes the previous channel, as replayRequest already does when rebinding cookies or onRetry fails. That way an HTTP/1.1 connection's permit is back before the caller learns of the failure. The HTTP/2 stream channel holds no permit and is already closed at that point.

Result: In that case, the request now fails at once with the exception that stopped the replay, over HTTP/2 as over HTTP/1.1. Over HTTP/1.1 the outcome is the same as before: the request fails with the same exception, and the previous connection is closed with it.

Tests:

  • FailedReplayHttp2Test: the example above.
    • Server: a Netty HTTP/2 server that deletes the body file once it has received the request, then answers 307.
    • Expected: the request fails with the IllegalArgumentException within 10 seconds, after exactly one replay, and the client still serves a later request.
    • Without the fix, it times out.
  • FailedReplayTest: the same over HTTP/1.1, through TLS, because over plain HTTP a filter does not replay while the connection is open.
    • Setup: maxConnections is 1, and the first request's onThrowable sends the next request. That request needs the permit of the connection the replay came from, at the moment the caller learns of the failure.
    • Without the fix, it passes too, since exceptionCaught aborted with the channel.
    • Aborting with null instead of the channel fails it with TooManyConnectionsException.

The request fails with the exception that stopped the replay, not with the IOException that led to it, as with retry().

AI disclosure

Claude Code on behalf of Matthias Kurz. The commit includes Co-Authored-By per AGENTS.md.

Test plan

All on JDK 11.

  • ./mvnw -B -ntp -Dgpg.skip=true clean verify: 2,493 tests, 0 failures, 22 skipped; Revapi passed against 3.0.15. Docker was not running, so the skipped tests include the Testcontainers proxy tests.
  • FailedReplayHttp2Test and FailedReplayTest 5 times: 2 of 2 each time.
  • Without the fix: FailedReplayHttp2Test fails with a TimeoutException after 10 seconds; FailedReplayTest passes.
  • Aborting with null instead of the channel: FailedReplayTest fails with TooManyConnectionsException; FailedReplayHttp2Test passes.

When a filter asks for a replay, NettyRequestSender.replayRequest
hands the previous channel over and then calls sendNextRequest, which
builds the replayed request and can throw, for example when the
request's body file is gone. replayRequest let that exception through.
For a replay that an IOExceptionFilter asked for from a handler's
catch block, the exception then left that catch block and reached
exceptionCaught. Over HTTP/1.1, the previous channel's drain callback
still names the future, so exceptionCaught failed the request. Over
HTTP/2, the stream channel is discarded before the replay (since
AsyncHttpClient#2197), so exceptionCaught found no future, and the request waited
until its request timeout.

Abort the future in replayRequest instead, as retry() does for the
same call. Pass the previous channel, as replayRequest's earlier
aborts do, so that an HTTP/1.1 connection's permit is back before the
caller learns of the failure.

Claude Code on behalf of Matthias Kurz

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hyperxpro
hyperxpro merged commit 961baca into AsyncHttpClient:main Nov 27, 2026
13 checks passed
@mkurz
mkurz deleted the fix/abort-failed-replay branch November 27, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants