Repository navigation
Fix three Windows CI flakes: replay tests, HTTP/2 body close, WebSocket 1009 #2426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import io.netty.channel.Channel; | ||
| import io.netty.channel.ChannelHandler.Sharable; | ||
| import io.netty.channel.ChannelHandlerContext; | ||
| import io.netty.channel.ChannelInboundHandlerAdapter; | ||
| import io.netty.handler.codec.TooLongFrameException; | ||
| import io.netty.handler.codec.compression.DecompressionException; | ||
| import io.netty.handler.codec.http.HttpHeaderValues; | ||
|
|
@@ -28,6 +29,8 @@ | |
| import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; | ||
| import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus; | ||
| import io.netty.handler.codec.http.websocketx.WebSocketFrame; | ||
| import io.netty.util.ReferenceCountUtil; | ||
| import io.netty.util.concurrent.ScheduledFuture; | ||
| import org.asynchttpclient.AsyncHandler.State; | ||
| import org.asynchttpclient.AsyncHttpClientConfig; | ||
| import org.asynchttpclient.HttpResponseStatus; | ||
|
|
@@ -40,6 +43,7 @@ | |
| import org.asynchttpclient.ws.WebSocketUpgradeHandler; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; | ||
| import static io.netty.handler.codec.http.HttpHeaderNames.SEC_WEBSOCKET_ACCEPT; | ||
|
|
@@ -54,6 +58,9 @@ | |
| @Sharable | ||
| public final class WebSocketHandler extends AsyncHttpClientHandler { | ||
|
|
||
| // How long the server gets to close the connection after a 1009, before the client closes it itself. | ||
| private static final long CLOSE_HANDSHAKE_TIMEOUT_SECONDS = 10; | ||
|
|
||
| public WebSocketHandler(AsyncHttpClientConfig config, ChannelManager channelManager, NettyRequestSender requestSender) { | ||
| super(config, channelManager, requestSender); | ||
| } | ||
|
|
@@ -164,15 +171,54 @@ public void handleRead(Channel channel, NettyResponseFuture<?> future, Object e) | |
| @Override | ||
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) { | ||
| // RFC 6455 section 7.4.1: 1009 for a message too big to process, whether the aggregator refused it or the | ||
| // inflater did. Queued before the close below. | ||
| // inflater did. | ||
| Throwable cause = getCause(e); | ||
| if ((cause instanceof TooLongFrameException && ctx.pipeline().get(WS_FRAME_AGGREGATOR) != null) | ||
| || (cause instanceof DecompressionException && ctx.pipeline().get(WS_DECODER_HANDLER) != null)) { | ||
| ctx.writeAndFlush(new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG)); | ||
| closeWithMessageTooBig(ctx, e); | ||
| return; | ||
| } | ||
| super.exceptionCaught(ctx, e); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| } | ||
|
|
||
| /** | ||
| * Sends the 1009 and leaves closing the connection to the server, as RFC 6455 section 7.1.1 asks of a client, | ||
| * discarding what the server still sends until then. Closing it here instead would leave that input unread, so | ||
| * the connection would be reset, and the server could lose the close frame and report 1006. | ||
| */ | ||
| private void closeWithMessageTooBig(ChannelHandlerContext ctx, Throwable e) { | ||
| if (ctx.pipeline().get(DiscardInbound.class) != null) { | ||
| // What the decoder still held when the close began. | ||
| return; | ||
| } | ||
| ctx.pipeline().addBefore(WS_DECODER_HANDLER, null, new DiscardInbound()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| Channel channel = ctx.channel(); | ||
| ctx.writeAndFlush(new CloseWebSocketFrame(WebSocketCloseStatus.MESSAGE_TOO_BIG)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| ScheduledFuture<?> forceClose = ctx.executor().schedule(() -> Channels.silentlyCloseChannel(channel), | ||
| CLOSE_HANDSHAKE_TIMEOUT_SECONDS, TimeUnit.SECONDS); | ||
| channel.closeFuture().addListener(f -> forceClose.cancel(false)); | ||
|
|
||
| Object attribute = Channels.getAttribute(channel); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| if (attribute instanceof NettyResponseFuture<?>) { | ||
| logger.warn("onError", e); | ||
| try { | ||
| NettyWebSocket webSocket = getNettyWebSocket((NettyResponseFuture<?>) attribute); | ||
| if (webSocket != null) { | ||
| webSocket.onError(e); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
| logger.error("onError", t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final class DiscardInbound extends ChannelInboundHandlerAdapter { | ||
| @Override | ||
| public void channelRead(ChannelHandlerContext ctx, Object msg) { | ||
| ReferenceCountUtil.release(msg); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void handleException(NettyResponseFuture<?> future, Throwable e) { | ||
| logger.warn("onError", e); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,7 @@ default void onResume(Runnable resume) { | |
| // writer until written; released by finish() if still set when the pump ends. | ||
| private ByteBuf pending; | ||
| private boolean done; | ||
| private boolean sourceClosed; | ||
|
|
||
| // flush() can synchronously fire channelWritabilityChanged. Prevent that callback from re-entering the | ||
| // pump, and remember permanently once the terminal frame has been emitted so later callbacks cannot write | ||
|
|
@@ -254,6 +255,8 @@ private void pump() { | |
| // First iteration (toWrite == null): nothing written yet, loop to read the second chunk. | ||
| } | ||
| } catch (Throwable t) { | ||
| // Released before the request fails, so that whoever learns of the failure can delete a file body. | ||
| closeSource(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| future.abort(t); | ||
| finish(t); | ||
| } finally { | ||
|
|
@@ -313,11 +316,7 @@ private void finish(Throwable cause) { | |
| resumeHandler = null; | ||
| } | ||
|
|
||
| try { | ||
| source.close(); | ||
| } catch (Throwable t) { | ||
| LOGGER.warn("Failed to close HTTP/2 request body source", t); | ||
| } | ||
| closeSource(); | ||
|
|
||
| if (cause != null) { | ||
| LOGGER.debug("HTTP/2 request body streaming failed; closing stream", cause); | ||
|
|
@@ -328,6 +327,18 @@ private void finish(Throwable cause) { | |
| } | ||
| } | ||
|
|
||
| private void closeSource() { | ||
| if (sourceClosed) { | ||
| return; | ||
| } | ||
| sourceClosed = true; | ||
| try { | ||
| source.close(); | ||
| } catch (Throwable t) { | ||
| LOGGER.warn("Failed to close HTTP/2 request body source", t); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resumes the parked pump when the stream channel becomes writable again. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
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?