From a3c44f5a991cbaa562b6ca58cea86c8fa50acbc8 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 31 Aug 2021 12:49:30 +0200 Subject: [PATCH] Adjust usage of ChannelFutureListeners.CLOSE to use the ChannelHandlerContext (#11631) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation: Usually the outbound operation should start at the "current" ChanneöHandlercontext which was often not the case Modifications: Use the ChannelHandlerContext for closing the connection Result: Start the operation on the right position of the pipeline --- .../handler/codec/http/HttpServerKeepAliveHandler.java | 2 +- .../io/netty/handler/codec/http/cors/CorsHandler.java | 2 +- .../handler/codec/http/websocketx/Utf8FrameValidator.java | 2 +- .../codec/http/websocketx/WebSocket08FrameDecoder.java | 2 +- .../codec/http/websocketx/WebSocketServerHandshaker.java | 8 ++++---- .../http/websocketx/WebSocketServerProtocolHandler.java | 4 ++-- .../WebSocketServerProtocolHandshakeHandler.java | 4 ++-- .../java/io/netty/handler/codec/MessageAggregator.java | 2 +- .../java/io/netty/example/file/FileServerHandler.java | 2 +- .../io/netty/example/http/cors/OkResponseHandler.java | 2 +- .../example/http/file/HttpStaticFileServerHandler.java | 4 ++-- .../http/helloworld/HttpHelloWorldServerHandler.java | 2 +- .../netty/example/http/snoop/HttpSnoopServerHandler.java | 2 +- .../example/http/upload/HttpUploadServerHandler.java | 6 +++--- .../benchmarkserver/WebSocketServerHandler.java | 2 +- .../http/websocketx/server/WebSocketIndexPageHandler.java | 2 +- .../http2/helloworld/server/HelloWorldHttp1Handler.java | 2 +- .../netty/example/http2/tiles/FallbackRequestHandler.java | 2 +- .../io/netty/example/http2/tiles/Http1RequestHandler.java | 2 +- .../netty/example/stomp/websocket/StompChatHandler.java | 4 ++-- .../stomp/websocket/StompWebSocketClientPageHandler.java | 2 +- .../java/io/netty/example/telnet/TelnetServerHandler.java | 2 +- .../src/test/java/io/netty/handler/proxy/ProxyServer.java | 2 +- .../handler/ipfilter/AbstractRemoteAddressFilter.java | 2 +- .../netty/testsuite/autobahn/AutobahnServerHandler.java | 4 ++-- .../io/netty/testsuite/http2/HelloWorldHttp1Handler.java | 2 +- .../io/netty/testsuite/svm/HttpNativeServerHandler.java | 2 +- .../socket/CompositeBufferGatheringWriteTest.java | 4 ++-- .../testsuite/transport/socket/SocketHalfClosedTest.java | 2 +- .../test/java/io/netty/channel/ReentrantChannelTest.java | 2 +- .../io/netty/channel/socket/nio/NioSocketChannelTest.java | 2 +- 31 files changed, 42 insertions(+), 42 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerKeepAliveHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerKeepAliveHandler.java index 88d42cd13f..64216fbd0b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerKeepAliveHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerKeepAliveHandler.java @@ -87,7 +87,7 @@ public class HttpServerKeepAliveHandler implements ChannelHandler { boolean shouldClose = msg instanceof LastHttpContent && !shouldKeepAlive(); Future future = ctx.write(msg); if (shouldClose) { - future.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + future.addListener(ctx, ChannelFutureListeners.CLOSE); } return future; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java index b21adf67d5..a6996f5f37 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java @@ -244,7 +244,7 @@ public class CorsHandler implements ChannelHandler { Future future = ctx.writeAndFlush(response); if (!keepAlive) { - future.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + future.addListener(ctx, ChannelFutureListeners.CLOSE); } } } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/Utf8FrameValidator.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/Utf8FrameValidator.java index 3087dea9d4..437247b332 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/Utf8FrameValidator.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/Utf8FrameValidator.java @@ -92,7 +92,7 @@ public class Utf8FrameValidator implements ChannelHandler { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof CorruptedFrameException && ctx.channel().isOpen()) { - ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx, ChannelFutureListeners.CLOSE); } ctx.fireExceptionCaught(cause); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameDecoder.java index a37dddcaa3..5fe2874ecf 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameDecoder.java @@ -448,7 +448,7 @@ public class WebSocket08FrameDecoder extends ByteToMessageDecoder } closeMessage = new CloseWebSocketFrame(closeStatus, reasonText); } - ctx.writeAndFlush(closeMessage).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(closeMessage).addListener(ctx, ChannelFutureListeners.CLOSE); } throw ex; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java index 0e316c310a..61261610b0 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java @@ -332,7 +332,7 @@ public abstract class WebSocketServerHandshaker { */ public Future close(Channel channel, CloseWebSocketFrame frame) { requireNonNull(channel, "channel"); - return close0(channel, channel, frame); + return close0(channel, frame); } /** @@ -345,11 +345,11 @@ public abstract class WebSocketServerHandshaker { */ public Future close(ChannelHandlerContext ctx, CloseWebSocketFrame frame) { requireNonNull(ctx, "ctx"); - return close0(ctx, ctx.channel(), frame); + return close0(ctx, frame); } - private static Future close0(ChannelOutboundInvoker invoker, Channel channel, CloseWebSocketFrame frame) { - return invoker.writeAndFlush(frame).addListener(channel, ChannelFutureListeners.CLOSE); + private static Future close0(ChannelOutboundInvoker invoker, CloseWebSocketFrame frame) { + return invoker.writeAndFlush(frame).addListener(invoker, ChannelFutureListeners.CLOSE); } /** diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java index d6ba5e19be..37ca319c79 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java @@ -241,7 +241,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler { closeSent(promise); handshaker.close(ctx, (CloseWebSocketFrame) frame).cascadeTo(promise); } else { - ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx, ChannelFutureListeners.CLOSE); } return; } @@ -258,7 +258,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler { if (cause instanceof WebSocketHandshakeException) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes())); - ctx.channel().writeAndFlush(response).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.channel().writeAndFlush(response).addListener(ctx, ChannelFutureListeners.CLOSE); } else { ctx.fireExceptionCaught(cause); ctx.close(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index 87868f8e3b..2afa09e8cd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -125,9 +125,9 @@ class WebSocketServerProtocolHandshakeHandler implements ChannelHandler { } private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { - Future f = ctx.channel().writeAndFlush(res); + Future f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { - f.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + f.addListener(ctx, ChannelFutureListeners.CLOSE); } } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageAggregator.java b/codec/src/main/java/io/netty/handler/codec/MessageAggregator.java index 9ef363a37b..e83eb7e27f 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageAggregator.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageAggregator.java @@ -238,7 +238,7 @@ public abstract class MessageAggregator future = ctx.writeAndFlush(continueResponse).addListener(ctx, listener); if (closeAfterWrite) { - future.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + future.addListener(ctx, ChannelFutureListeners.CLOSE); return; } if (handlingOversizedMessage) { diff --git a/example/src/main/java/io/netty/example/file/FileServerHandler.java b/example/src/main/java/io/netty/example/file/FileServerHandler.java index d6896a3b92..6ecf728657 100644 --- a/example/src/main/java/io/netty/example/file/FileServerHandler.java +++ b/example/src/main/java/io/netty/example/file/FileServerHandler.java @@ -67,7 +67,7 @@ public class FileServerHandler extends SimpleChannelInboundHandler { ctx.writeAndFlush("ERR: " + cause.getClass().getSimpleName() + ": " + cause.getMessage() + '\n') - .addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + .addListener(ctx, ChannelFutureListeners.CLOSE); } } } diff --git a/example/src/main/java/io/netty/example/http/cors/OkResponseHandler.java b/example/src/main/java/io/netty/example/http/cors/OkResponseHandler.java index 01a787c395..4636b27625 100644 --- a/example/src/main/java/io/netty/example/http/cors/OkResponseHandler.java +++ b/example/src/main/java/io/netty/example/http/cors/OkResponseHandler.java @@ -34,6 +34,6 @@ public class OkResponseHandler extends SimpleChannelInboundHandler { final FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER); response.headers().set("custom-response-header", "Some value"); - ctx.writeAndFlush(response).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(response).addListener(ctx, ChannelFutureListeners.CLOSE); } } diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java index ccbc4108df..7340280d31 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java @@ -219,7 +219,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler f = ctx.write(response); if (!keepAlive) { - f.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + f.addListener(ctx, ChannelFutureListeners.CLOSE); } } } diff --git a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java index af171a0aa9..93eef6538b 100644 --- a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java +++ b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java @@ -127,7 +127,7 @@ public class HttpSnoopServerHandler extends SimpleChannelInboundHandler if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. - ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ctx, ChannelFutureListeners.CLOSE); } } } diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java index 3d184a005e..e2d4b32712 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java @@ -438,16 +438,16 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler future = ctx.channel().writeAndFlush(response); + Future future = ctx.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (!keepAlive) { - future.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + future.addListener(ctx, ChannelFutureListeners.CLOSE); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.log(Level.WARNING, responseContent.toString(), cause); - ctx.channel().close(); + ctx.close(); } } diff --git a/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServerHandler.java b/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServerHandler.java index 7bb4b3757c..d92ee41128 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServerHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/benchmarkserver/WebSocketServerHandler.java @@ -146,7 +146,7 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { // Tell the client we're going to close the connection. res.headers().set(CONNECTION, CLOSE); - ctx.writeAndFlush(res).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(res).addListener(ctx, ChannelFutureListeners.CLOSE); } else { if (req.protocolVersion().equals(HTTP_1_0)) { res.headers().set(CONNECTION, KEEP_ALIVE); diff --git a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java index acd616258e..56f0e94ca3 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/server/WebSocketIndexPageHandler.java @@ -101,7 +101,7 @@ public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler { StompFrame receiptFrame = new DefaultStompFrame(StompCommand.RECEIPT); receiptFrame.headers().set(RECEIPT_ID, receiptId); - ctx.writeAndFlush(receiptFrame).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(receiptFrame).addListener(ctx, ChannelFutureListeners.CLOSE); } private static void sendErrorFrame(String message, String description, ChannelHandlerContext ctx) { @@ -186,7 +186,7 @@ public class StompChatHandler extends SimpleChannelInboundHandler { errorFrame.content().writeCharSequence(description, CharsetUtil.UTF_8); } - ctx.writeAndFlush(errorFrame).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.writeAndFlush(errorFrame).addListener(ctx, ChannelFutureListeners.CLOSE); } private static StompFrame transformToMessage(StompFrame sendFrame, StompSubscription subscription) { diff --git a/example/src/main/java/io/netty/example/stomp/websocket/StompWebSocketClientPageHandler.java b/example/src/main/java/io/netty/example/stomp/websocket/StompWebSocketClientPageHandler.java index 1d41e33ae2..e1b633b85f 100644 --- a/example/src/main/java/io/netty/example/stomp/websocket/StompWebSocketClientPageHandler.java +++ b/example/src/main/java/io/netty/example/stomp/websocket/StompWebSocketClientPageHandler.java @@ -136,7 +136,7 @@ public final class StompWebSocketClientPageHandler extends SimpleChannelInboundH ctx.write(response); } else { response.headers().set(CONNECTION, CLOSE); - ctx.write(response).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + ctx.write(response).addListener(ctx, ChannelFutureListeners.CLOSE); } if (autoFlush) { diff --git a/example/src/main/java/io/netty/example/telnet/TelnetServerHandler.java b/example/src/main/java/io/netty/example/telnet/TelnetServerHandler.java index 574cf602a7..81c3f8da9d 100644 --- a/example/src/main/java/io/netty/example/telnet/TelnetServerHandler.java +++ b/example/src/main/java/io/netty/example/telnet/TelnetServerHandler.java @@ -59,7 +59,7 @@ public class TelnetServerHandler extends SimpleChannelInboundHandler { // Close the connection after sending 'Have a good day!' // if the client has sent 'bye'. if (close) { - future.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + future.addListener(ctx, ChannelFutureListeners.CLOSE); } } diff --git a/handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java b/handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java index b95e488786..68038cce33 100644 --- a/handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java +++ b/handler-proxy/src/test/java/io/netty/handler/proxy/ProxyServer.java @@ -273,7 +273,7 @@ abstract class ProxyServer { ctx.write(Unpooled.copiedBuffer("2\n", CharsetUtil.US_ASCII)); } else if ("C\n".equals(str)) { ctx.write(Unpooled.copiedBuffer("3\n", CharsetUtil.US_ASCII)) - .addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + .addListener(ctx, ChannelFutureListeners.CLOSE); } else { throw new IllegalStateException("unexpected message: " + str); } diff --git a/handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java b/handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java index 134db5390b..4c0eb0aae2 100644 --- a/handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java +++ b/handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java @@ -65,7 +65,7 @@ public abstract class AbstractRemoteAddressFilter imple } else { Future rejectedFuture = channelRejected(ctx, remoteAddress); if (rejectedFuture != null && !rejectedFuture.isDone()) { - rejectedFuture.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + rejectedFuture.addListener(ctx, ChannelFutureListeners.CLOSE); } else { ctx.close(); } diff --git a/testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java b/testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java index eb28e5c49e..b05761239d 100644 --- a/testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java +++ b/testsuite-autobahn/src/main/java/io/netty/testsuite/autobahn/AutobahnServerHandler.java @@ -130,9 +130,9 @@ public class AutobahnServerHandler implements ChannelHandler { } // Send the response and close the connection if necessary. - Future f = ctx.channel().writeAndFlush(res); + Future f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { - f.addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + f.addListener(ctx, ChannelFutureListeners.CLOSE); } } diff --git a/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp1Handler.java b/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp1Handler.java index d8315bec57..cdb0318531 100644 --- a/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp1Handler.java +++ b/testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp1Handler.java @@ -60,7 +60,7 @@ public class HelloWorldHttp1Handler extends SimpleChannelInboundHandler write(final ChannelHandlerContext ctx, Object msg) { - Future f = ctx.write(msg).addListener(ctx.channel(), ChannelFutureListeners.CLOSE); + Future f = ctx.write(msg).addListener(ctx, ChannelFutureListeners.CLOSE); ctx.channel().flush(); return f; } diff --git a/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java b/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java index aac02a8ab0..7a18469544 100644 --- a/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java +++ b/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelTest.java @@ -80,7 +80,7 @@ public class NioSocketChannelTest extends AbstractNioChannelTest