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