From 137a3e7137c5bb0df03bb67448b267dfaf169255 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 23 May 2019 08:24:03 +0200 Subject: [PATCH] Do not use static exceptions for websocket handshake timeout (#9174) Motivation: f17bfd0f64189d91302fbdd15103788bf9eabaa2 removed the usage of static exception instances to reduce the risk of OOME due addSupressed calls. We should do the same for exceptions used to signal handshake timeouts. Modifications: Do not use static instances Result: No risk of OOME due addSuppressed calls --- .../WebSocketClientProtocolHandshakeHandler.java | 8 +------- .../WebSocketServerProtocolHandshakeHandler.java | 9 ++------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientProtocolHandshakeHandler.java index 1cbc3fb7d2..701af1b46d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientProtocolHandshakeHandler.java @@ -24,18 +24,12 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler.ClientHandshakeStateEvent; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.FutureListener; -import io.netty.util.concurrent.ScheduledFuture; -import io.netty.util.internal.ThrowableUtil; import java.util.concurrent.TimeUnit; import static io.netty.util.internal.ObjectUtil.*; class WebSocketClientProtocolHandshakeHandler extends ChannelInboundHandlerAdapter { - private static final WebSocketHandshakeException HANDSHAKE_TIMED_OUT_EXCEPTION = ThrowableUtil.unknownStackTrace( - new WebSocketHandshakeException("handshake timed out"), - WebSocketClientProtocolHandshakeHandler.class, - "channelActive(...)"); private static final long DEFAULT_HANDSHAKE_TIMEOUT_MS = 10000L; private final WebSocketClientHandshaker handshaker; @@ -112,7 +106,7 @@ class WebSocketClientProtocolHandshakeHandler extends ChannelInboundHandlerAdapt return; } - if (localHandshakePromise.tryFailure(HANDSHAKE_TIMED_OUT_EXCEPTION)) { + if (localHandshakePromise.tryFailure(new WebSocketHandshakeException("handshake timed out"))) { ctx.flush() .fireUserEventTriggered(ClientHandshakeStateEvent.HANDSHAKE_TIMEOUT) .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 ade3991f61..08a82ce3d8 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 @@ -30,8 +30,6 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.Ser import io.netty.handler.ssl.SslHandler; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.FutureListener; -import io.netty.util.concurrent.ScheduledFuture; -import io.netty.util.internal.ThrowableUtil; import java.util.concurrent.TimeUnit; @@ -45,10 +43,6 @@ import static io.netty.util.internal.ObjectUtil.*; * Handles the HTTP handshake (the HTTP Upgrade request) for {@link WebSocketServerProtocolHandler}. */ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapter { - private static final WebSocketHandshakeException HANDSHAKE_TIMED_OUT_EXCEPTION = ThrowableUtil.unknownStackTrace( - new WebSocketHandshakeException("handshake timed out"), - WebSocketServerProtocolHandshakeHandler.class, - "channelRead(...)"); private static final long DEFAULT_HANDSHAKE_TIMEOUT_MS = 10000L; @@ -180,7 +174,8 @@ class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapt final Future timeoutFuture = ctx.executor().schedule(new Runnable() { @Override public void run() { - if (localHandshakePromise.tryFailure(HANDSHAKE_TIMED_OUT_EXCEPTION)) { + if (!localHandshakePromise.isDone() && + localHandshakePromise.tryFailure(new WebSocketHandshakeException("handshake timed out"))) { ctx.flush() .fireUserEventTriggered(ServerHandshakeStateEvent.HANDSHAKE_TIMEOUT) .close();