From e06fcdbc6a162f085b77e25bed1f847026cfd3ea Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 20 Jun 2013 16:07:36 +0200 Subject: [PATCH] [#1475] Correctly handle writes with empty MessageList in SslHandler --- .../java/io/netty/handler/ssl/SslHandler.java | 27 +++++++++++++------ .../transport/socket/SocketSslEchoTest.java | 5 +--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java index d845348d84..ee6ac5983e 100644 --- a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java +++ b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java @@ -392,17 +392,28 @@ public class SslHandler ctx.write(msgs, promise); return; } - for (int i = 0; i < msgs.size(); i++) { - ByteBuf msg = (ByteBuf) msgs.get(i); - ChannelPromise cp; - if (i + 1 == msgs.size()) { - cp = promise; + try { + if (msgs.isEmpty()) { + // if the MessageList is empty we need to add an empty buffer as pending write as otherwise + // the promise will never be notified. + // See https://github.com/netty/netty/issues/1475 + pendingUnencryptedWrites.add(new PendingWrite(Unpooled.EMPTY_BUFFER, promise)); } else { - cp = ctx.newPromise(); + for (int i = 0; i < msgs.size(); i++) { + ByteBuf msg = (ByteBuf) msgs.get(i); + ChannelPromise cp; + if (i + 1 == msgs.size()) { + cp = promise; + } else { + cp = ctx.newPromise(); + } + pendingUnencryptedWrites.add(new PendingWrite(msg, cp)); + } } - pendingUnencryptedWrites.add(new PendingWrite(msg, cp)); + flush0(ctx); + } finally { + msgs.recycle(); } - flush0(ctx); } private void flush0(ChannelHandlerContext ctx) throws SSLException { diff --git a/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketSslEchoTest.java b/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketSslEchoTest.java index f2e64d16c1..c113c0938a 100644 --- a/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketSslEchoTest.java +++ b/testsuite/src/test/java/io/netty/testsuite/transport/socket/SocketSslEchoTest.java @@ -111,10 +111,7 @@ public class SocketSslEchoTest extends AbstractSocketTest { for (int i = FIRST_MESSAGE_SIZE; i < data.length;) { int length = Math.min(random.nextInt(1024 * 64), data.length - i); ChannelFuture future = cc.write(Unpooled.wrappedBuffer(data, i, length)); - // TODO: Remove me as it seems like ChunkWriteHandler has a bug which let it hang here - if (!chunkWriteHandler) { - future.sync(); - } + future.sync(); i += length; }