|
27 | 27 | import io.netty.util.internal.logging.InternalLogger; |
28 | 28 | import io.netty.util.internal.logging.InternalLoggerFactory; |
29 | 29 |
|
| 30 | +import java.nio.channels.ClosedChannelException; |
30 | 31 | import javax.net.ssl.SSLException; |
31 | 32 |
|
32 | 33 | /** |
@@ -139,19 +140,24 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc |
139 | 140 | String protocol = sslHandler.applicationProtocol(); |
140 | 141 | configurePipeline(ctx, protocol != null ? protocol : fallbackProtocol); |
141 | 142 | } else { |
142 | | - try { |
143 | | - super.userEventTriggered(ctx, evt); |
144 | | - } finally { |
145 | | - this.notifyHandshakeFailure(ctx, handshakeEvent.cause()); |
| 143 | + // timeouts and early closes may not produce a decoder exception |
| 144 | + // preserve the existing exception and close handling for other TLS failures |
| 145 | + Throwable cause = handshakeEvent.cause(); |
| 146 | + if (cause instanceof SslHandshakeTimeoutException || cause instanceof ClosedChannelException) { |
| 147 | + try { |
| 148 | + super.userEventTriggered(ctx, evt); |
| 149 | + } finally { |
| 150 | + notifyHandshakeFailure(ctx, cause); |
| 151 | + } |
| 152 | + |
| 153 | + return; |
146 | 154 | } |
147 | | - |
148 | | - return; |
149 | 155 | } |
150 | 156 | } catch (Throwable cause) { |
151 | 157 | exceptionCaught(ctx, cause); |
152 | 158 | } finally { |
153 | | - // consume later SSL decoder exceptions to avoid downstream warnings |
154 | | - // see https://github.com/netty/netty/issues/10342; timeouts and early closes may only emit this event |
| 159 | + // keep the handler after failure to consume later SSL decoder exceptions |
| 160 | + // see https://github.com/netty/netty/issues/10342 |
155 | 161 | if (handshakeEvent.isSuccess()) { |
156 | 162 | removeSelfIfPresent(ctx); |
157 | 163 | } |
@@ -197,9 +203,14 @@ protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) thro |
197 | 203 | } |
198 | 204 |
|
199 | 205 | private void notifyHandshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
200 | | - if (!this.handshakeFailed) { |
201 | | - this.handshakeFailed = true; |
202 | | - this.handshakeFailure(ctx, cause); |
| 206 | + if (!handshakeFailed) { |
| 207 | + handshakeFailed = true; |
| 208 | + |
| 209 | + try { |
| 210 | + handshakeFailure(ctx, cause); |
| 211 | + } catch (Throwable innerCause) { |
| 212 | + exceptionCaught(ctx, innerCause); |
| 213 | + } |
203 | 214 | } |
204 | 215 | } |
205 | 216 |
|
@@ -208,13 +219,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E |
208 | 219 | Throwable wrapped; |
209 | 220 | if (cause instanceof DecoderException && ((wrapped = cause.getCause()) instanceof SSLException)) { |
210 | 221 | try { |
211 | | - this.notifyHandshakeFailure(ctx, wrapped); |
| 222 | + notifyHandshakeFailure(ctx, wrapped); |
212 | 223 | return; |
213 | 224 | } finally { |
214 | 225 | removeSelfIfPresent(ctx); |
215 | 226 | } |
216 | 227 | } |
217 | | - |
218 | 228 | logger.warn("{} Failed to select the application-level protocol:", ctx.channel(), cause); |
219 | 229 | ctx.fireExceptionCaught(cause); |
220 | 230 | ctx.close(); |
|