From 7e3ecf6b469de1a840dd928541339e59e5325766 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 13 May 2009 01:08:40 +0000 Subject: [PATCH] Better fix for the SslHandler dead lock --- .../org/jboss/netty/handler/ssl/SslHandler.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java b/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java index 1ffc536ffc..277e0eaf38 100644 --- a/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java +++ b/src/main/java/org/jboss/netty/handler/ssl/SslHandler.java @@ -688,7 +688,6 @@ public class SslHandler extends FrameDecoder implements ChannelDownstreamHandler loop: for (;;) { SSLEngineResult result; - boolean needsNonAppWrap = false; synchronized (handshakeLock) { if (initialHandshake && !engine.getUseClientMode() && !engine.isInboundDone() && !engine.isOutboundDone()) { @@ -710,9 +709,7 @@ public class SslHandler extends FrameDecoder implements ChannelDownstreamHandler break loop; } case NEED_WRAP: - // wrapNonAppData must be called - // while handshakeLock is not hold. - needsNonAppWrap = true; + wrapNonAppData(ctx, channel); break; case NEED_TASK: runDelegatedTasks(); @@ -730,14 +727,15 @@ public class SslHandler extends FrameDecoder implements ChannelDownstreamHandler result.getHandshakeStatus()); } } - - if (needsNonAppWrap) { - wrapNonAppData(ctx, channel); - } } if (needsWrap) { - wrap(ctx, channel); + // wrap() acquires pendingUnencryptedWrites first and then + // handshakeLock. If handshakeLock is already hold by the + // current thread, calling wrap() will lead to a dead lock. + if (!Thread.holdsLock(handshakeLock)) { + wrap(ctx, channel); + } } outAppBuf.flip();