[#1464] Make sure the ChannelPromise for writes is notified while using SslHandler

This commit is contained in:
Norman Maurer 2013-06-20 15:42:50 +02:00 committed by Trustin Lee
parent cfb3b977a1
commit ad73dce7a1
2 changed files with 18 additions and 10 deletions

View File

@ -406,16 +406,15 @@ public class SslHandler
} }
private void flush0(ChannelHandlerContext ctx) throws SSLException { private void flush0(ChannelHandlerContext ctx) throws SSLException {
boolean unwrapLater = false; boolean unwrapLater = false;
PendingWrite pending = null;
ByteBuf out = null; ByteBuf out = null;
ChannelPromise promise = null;
try { try {
for (;;) { for (;;) {
if (out == null) { if (out == null) {
out = ctx.alloc().buffer(); out = ctx.alloc().buffer();
} }
pending = pendingUnencryptedWrites.peek(); PendingWrite pending = pendingUnencryptedWrites.peek();
if (pending == null) { if (pending == null) {
break; break;
} }
@ -423,7 +422,10 @@ public class SslHandler
if (!pending.buf.isReadable()) { if (!pending.buf.isReadable()) {
pending.buf.release(); pending.buf.release();
promise = pending.promise;
pendingUnencryptedWrites.remove(); pendingUnencryptedWrites.remove();
} else {
promise = null;
} }
if (result.getStatus() == Status.CLOSED) { if (result.getStatus() == Status.CLOSED) {
@ -445,8 +447,9 @@ public class SslHandler
} else { } else {
switch (result.getHandshakeStatus()) { switch (result.getHandshakeStatus()) {
case NEED_WRAP: case NEED_WRAP:
if (!pending.buf.isReadable()) { if (promise != null) {
ctx.write(out, pending.promise); ctx.write(out, promise);
promise = null;
} else { } else {
ctx.write(out); ctx.write(out);
} }
@ -488,14 +491,14 @@ public class SslHandler
throw e; throw e;
} finally { } finally {
if (out != null && out.isReadable()) { if (out != null && out.isReadable()) {
if (pending != null && !pending.buf.isReadable()) { if (promise != null) {
ctx.write(out, pending.promise); ctx.write(out, promise);
} else { } else {
ctx.write(out); ctx.write(out);
} }
out = null; out = null;
} else if (pending != null && !pending.buf.isReadable()) { } else if (promise != null) {
pending.promise.setSuccess(); promise.trySuccess();
} }
if (out != null) { if (out != null) {
out.release(); out.release();

View File

@ -20,6 +20,7 @@ import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
@ -109,7 +110,11 @@ public class SocketSslEchoTest extends AbstractSocketTest {
for (int i = FIRST_MESSAGE_SIZE; i < data.length;) { for (int i = FIRST_MESSAGE_SIZE; i < data.length;) {
int length = Math.min(random.nextInt(1024 * 64), data.length - i); int length = Math.min(random.nextInt(1024 * 64), data.length - i);
cc.write(Unpooled.wrappedBuffer(data, i, length)); 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();
}
i += length; i += length;
} }