Skip to content

Fix three Windows CI flakes: replay tests, HTTP/2 body close, WebSocket 1009 - #2426

Open
pavel-ptashyts wants to merge 3 commits into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/fix-flaky-failed-replay-test
Open

pavel-ptashyts wants to merge 3 commits into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/fix-flaky-failed-replay-test

Conversation

@pavel-ptashyts

@pavel-ptashyts pavel-ptashyts commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

This fixes three Windows CI flakes, one commit each. All three come from the same Windows behaviour: a file or a connection that one side still holds when the other acts on it.

1. FailedReplayHttp2Test and FailedReplayTest (test-only)

Motivation: FailedReplayHttp2Test.aReplayThatCannotBeBuiltFailsTheRequest, added by #2415, fails on windows-latest in every Build PR run on main since that merge (runs 46036527320, 46050111003 and 46495870143), and on open pull requests too. The assertion at FailedReplayHttp2Test.java:151 expects an IllegalArgumentException as the cause and gets java.io.IOException: HTTP/2 stream reset by server, error code: 8. It passes on Linux and macOS.

Cause: the test's server deleted the request body file as soon as the request had ended, before it answered 307. The client closes the file only when its last write of the body completes (Http2BodyWriter.writeLastFrame, which mirrors the HTTP/1.1 path on purpose), and the server can read the end of that body before the client's write listener has run. Windows refuses to delete a file another handle still has open (FileSystemException: The process cannot access the file because it is being used by another process; checked on JDK 11, 21 and 25). When that happens, Files.delete throws in the server handler, whose exceptionCaught closes the stream. The stream reset reaches the client as an IOException, the filter replays it, and the request ends with an IOException instead of the IllegalArgumentException of a replay that cannot be built. Linux and macOS let a file be deleted while it is open, so the race never shows there.

FailedReplayTest, the HTTP/1.1 twin from #2415, has the same setup and the same race: there Jetty answers 500 and the request does not fail at all.

This is a race in the tests' setup, not a product bug: the client releases the file when it has finished writing it, which a real server cannot observe before it reads the end of the body.

Modification: the servers no longer delete the file. In both tests, a ResponseFilter deletes it on the client when the 307 arrives. Response filters run on the event loop that wrote the body, after that write completed and closed the file, so the delete no longer races the close. If the delete still failed, the filter would fail the request with a FilterException, so the test would fail loudly rather than pass for a wrong reason. The servers still answer only once the request body has ended.

What the tests still prove: unchanged. The redirect refuses to resend the missing file with an IOException, the IOExceptionFilter replays it once, building that replay throws IllegalArgumentException, the request fails promptly with that exception (assertInstanceOf(IllegalArgumentException.class, ...) is untouched), the filter is consulted exactly once, and the client still serves a later request. FailedReplayTest still checks the permit handoff through onThrowable with maxConnections 1.

Evidence (Windows 11, JDK 25, each test run as a @RepeatedTest(500) copy under CPU load from 24 busy loops on 10 cores, otherwise unchanged):

Test Before After
FailedReplayHttp2Test 22 of 500 failed 0 of 500
FailedReplayTest 32 of 500 failed 0 of 500

Without the load, FailedReplayHttp2Test passed 200 of 200 on JDK 21 on this machine, so the load is what widens the window enough to see it. Before the fix, every failure matched a server-side FileSystemException one for one (22 of 22 and 32 of 32). The HTTP/2 failures carried exactly the CI cause, HTTP/2 stream reset by server, error code: 8. The HTTP/1.1 failures were Expected java.util.concurrent.ExecutionException to be thrown, but nothing was thrown.

2. Http2StreamingBodyFlowControlTest: close the body source before failing the request

Failure: filePartThatShrinksFailsTheRequestOverHttp2 failed on this pull request's own windows-latest JDK 17 job, in its finally: Files.deleteIfExists(tmp) threw FileSystemException: ... being used by another process.

Cause (product): when reading the next chunk of an HTTP/2 body fails (here the file got shorter than its announced length), Http2BodyWriter.pump called future.abort(t) and only then finish(t), which closes the source. The caller is woken by the failure while the file is still open, so on Windows it cannot delete it yet. The same race applies to any application that deletes or moves its upload file once the request fails.

Change: the source is closed before the future fails (closeSource(), which finish now uses too, so it still runs exactly once). The future still fails with the source's error before the stream is closed, which #2401 relies on so the failure is neither replaced by the closed stream nor replayed.

Evidence (Windows 11, JDK 25, under load, fileBodyThatShrinks... and filePartThatShrinks... 300 times each): 5 of 600 failed with that FileSystemException before the change, 0 of 600 after.

3. WebSocketMessageTooBigTest: leave closing to the server after a 1009

Failure: aMessageOverTheBufferLimitClosesWith1009 failed once on windows-latest JDK 25 (on #2423): expected: <1009> but was: <1006>.

Cause (product): on a message over the buffer or inflate limit, WebSocketHandler sent the 1009 close frame and closed the connection right away (and then sent a second close frame, 1000, through handleException). The rest of the server's message was still unread, so closing reset the connection, and the server could lose the 1009 before reading it. With the server's onWebSocketError recorded in a scratch copy, every failing run showed Jetty seeing the connection end (EofException, or ClosedChannelException while it was still sending) and then closing with 1006; the passing runs read the 1009.

Change: the client sends the 1009 alone and, as RFC 6455 section 7.1.1 asks of a client, leaves closing the TCP connection to the server, discarding what the server still sends meanwhile. If the server has not closed it within 10 seconds, the client closes it itself. The listener still gets onError, then onClose once the connection is gone, as before.

Evidence (Windows 11, JDK 25, under load, 300 runs): 8 and 12 of 300 failed in two runs before the change, 0 of 300 after. The rest of the org.asynchttpclient.ws tests pass, including WebSocketInflateLimitTest.

AI disclosure

Claude Code on behalf of Pavel Ptashyts. Each commit includes Co-Authored-By per AGENTS.md.

Test plan

  • mvnw.cmd -B -ntp clean verify -Dgpg.skip=true on JDK 11.0.33: BUILD SUCCESS, 2,489 tests, 0 failures, 47 skipped (Docker tests among them); Revapi passed. No GPG key on this machine, hence -Dgpg.skip=true.
  • Before/after runs above on Windows 11, JDK 25.
  • CI of this pull request: all 13 jobs pass, including the four windows-latest ones.

🤖 Generated with Claude Code

pavel-ptashyts and others added 3 commits December 1, 2026 08:22
FailedReplayHttp2Test and FailedReplayTest had their server delete the
request body file as soon as it had read the body. The client closes
the file only when its last write of the body completes, and the server
can read that body before the client's write listener runs. Windows
refuses to delete a file that is still open, so the server's delete
failed now and then: over HTTP/2 the stream was reset and the request
failed with that IOException, over HTTP/1.1 Jetty answered 500. This
made FailedReplayHttp2Test fail on windows-latest in every Build PR
run on main since AsyncHttpClient#2415.

A response filter now deletes the file on the client when the 307
arrives. It runs on the event loop that wrote the body, after that
write has closed the file, so the delete no longer races it. The tests
still prove that a replay that cannot be built fails the request with
the IllegalArgumentException of the build, after one replay, and that
the client serves requests afterwards.

Claude Code on behalf of Pavel Ptashyts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When reading the next chunk of an HTTP/2 request body failed, for
example because a file body got shorter than its announced length,
Http2BodyWriter failed the request future first and closed the body
source only afterwards. A caller woken by that failure could then find
the file still open: on Windows, deleting it failed with "The process
cannot access the file because it is being used by another process".
Http2StreamingBodyFlowControlTest hit this in its cleanup on
windows-latest.

The source is now closed before the future fails. The future still
fails with the source's error before the stream is closed, so it is
neither replaced by the closed stream nor replayed.

Claude Code on behalf of Pavel Ptashyts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a WebSocket message went over the buffer or inflate limit, the
client sent a 1009 close frame and closed the connection at once. What
the server was still sending stayed unread, so closing reset the
connection, and the server could lose the close frame and report 1006.
WebSocketMessageTooBigTest failed this way on windows-latest. The
handler also sent a second close frame, 1000, after the 1009.

The client now sends the 1009 alone and, as RFC 6455 section 7.1.1
asks of a client, leaves closing the connection to the server. It
discards what the server still sends meanwhile, and closes the
connection itself if the server has not done so within 10 seconds. The
listener still gets onError, then onClose once the connection is gone.

Claude Code on behalf of Pavel Ptashyts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pavel-ptashyts pavel-ptashyts changed the title Delete the body file from the client in replay tests Fix three Windows CI flakes: replay tests, HTTP/2 body close, WebSocket 1009 Dec 1, 2026
|| (cause instanceof DecompressionException && ctx.pipeline().get(WS_DECODER_HANDLER) != null)) {
ctx.writeAndFlush(new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG));
closeWithMessageTooBig(ctx, e);
return;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change we never called onClose after a 1009, because closeChannel sets discard. Now we do. I think that is better, but the description says as before. Can you update it?

closeWithMessageTooBig(ctx, e);
return;
}
super.exceptionCaught(ctx, e);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other exception while we wait for the server still ends up here, so onError can be called twice and onClose not at all. Should we check if we are already closing first?

// What the decoder still held when the close began.
return;
}
ctx.pipeline().addBefore(WS_DECODER_HANDLER, null, new DiscardInbound());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only drops new reads. I think frames the decoder already holds still reach handleRead, so a listener can see onTextFrame after onError. We should drop them there as well.

}
ctx.pipeline().addBefore(WS_DECODER_HANDLER, null, new DiscardInbound());
Channel channel = ctx.channel();
ctx.writeAndFlush(new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to mark the NettyWebSocket as closing here. Otherwise isOpen() stays true and the user can still send frames after the close frame.

CLOSE_HANDSHAKE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
channel.closeFuture().addListener(f -> forceClose.cancel(false));

Object attribute = Channels.getAttribute(channel);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abort(...) releases the permit before it notifies the handler, and we skip that now. With maxConnections(1) a reconnect from onError can be refused. Can we call Channels.releasePermit(channel) first?

try {
NettyWebSocket webSocket = getNettyWebSocket((NettyResponseFuture<?>) attribute);
if (webSocket != null) {
webSocket.onError(e);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the big message arrives together with the 101 we are still in upgrade() and there are no listeners yet. The error is lost and we call onOpen after it. Should we fail the future and skip onOpen in this case?

}
} catch (Throwable t) {
// Released before the request fails, so that whoever learns of the failure can delete a file body.
closeSource();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Can you also assert the order in Http2BodyWriterTest? failingSourceFailsTheRequestWithItsErrorBeforeClosingTheStream looks like the right place.

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