Repository navigation
Fix three Windows CI flakes: replay tests, HTTP/2 body close, WebSocket 1009 - #2426
pavel-ptashyts wants to merge 3 commits into
Conversation
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>
| || (cause instanceof DecompressionException && ctx.pipeline().get(WS_DECODER_HANDLER) != null)) { | ||
| ctx.writeAndFlush(new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG)); | ||
| closeWithMessageTooBig(ctx, e); | ||
| return; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Good catch. Can you also assert the order in Http2BodyWriterTest? failingSourceFailsTheRequestWithItsErrorBeforeClosingTheStream looks like the right place.
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.
FailedReplayHttp2TestandFailedReplayTest(test-only)Motivation:
FailedReplayHttp2Test.aReplayThatCannotBeBuiltFailsTheRequest, added by #2415, fails onwindows-latestin everyBuild PRrun onmainsince that merge (runs 46036527320, 46050111003 and 46495870143), and on open pull requests too. The assertion atFailedReplayHttp2Test.java:151expects anIllegalArgumentExceptionas the cause and getsjava.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.deletethrows in the server handler, whoseexceptionCaughtcloses the stream. The stream reset reaches the client as anIOException, the filter replays it, and the request ends with anIOExceptioninstead of theIllegalArgumentExceptionof 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
ResponseFilterdeletes 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 aFilterException, 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, theIOExceptionFilterreplays it once, building that replay throwsIllegalArgumentException, 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.FailedReplayTeststill checks the permit handoff throughonThrowablewithmaxConnections1.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):FailedReplayHttp2TestFailedReplayTestWithout the load,
FailedReplayHttp2Testpassed 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-sideFileSystemExceptionone 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 wereExpected java.util.concurrent.ExecutionException to be thrown, but nothing was thrown.2.
Http2StreamingBodyFlowControlTest: close the body source before failing the requestFailure:
filePartThatShrinksFailsTheRequestOverHttp2failed on this pull request's ownwindows-latestJDK 17 job, in itsfinally:Files.deleteIfExists(tmp)threwFileSystemException: ... 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.pumpcalledfuture.abort(t)and only thenfinish(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(), whichfinishnow 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...andfilePartThatShrinks...300 times each): 5 of 600 failed with thatFileSystemExceptionbefore the change, 0 of 600 after.3.
WebSocketMessageTooBigTest: leave closing to the server after a 1009Failure:
aMessageOverTheBufferLimitClosesWith1009failed once onwindows-latestJDK 25 (on #2423):expected: <1009> but was: <1006>.Cause (product): on a message over the buffer or inflate limit,
WebSocketHandlersent the 1009 close frame and closed the connection right away (and then sent a second close frame, 1000, throughhandleException). 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'sonWebSocketErrorrecorded in a scratch copy, every failing run showed Jetty seeing the connection end (EofException, orClosedChannelExceptionwhile 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, thenonCloseonce 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.wstests pass, includingWebSocketInflateLimitTest.AI disclosure
Claude Code on behalf of Pavel Ptashyts. Each commit includes
Co-Authored-ByperAGENTS.md.Test plan
mvnw.cmd -B -ntp clean verify -Dgpg.skip=trueon 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.windows-latestones.🤖 Generated with Claude Code