diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java index 31a3cf7e3e..b405bc447e 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java @@ -45,6 +45,7 @@ import io.netty.channel.ChannelInboundByteHandlerAdapter; public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter { private volatile boolean singleDecode; + private boolean removed; /** * If set then only one message is decoded on each {@link #inboundBufferUpdated(ChannelHandlerContext)} call. @@ -102,7 +103,7 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter */ private void callDecode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) { int oldOutSize = out.readableBytes(); - while (in.readable()) { + while (!removed && in.readable()) { int oldInSize = in.readableBytes(); try { decode(ctx, in, out); @@ -144,4 +145,10 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { decode(ctx, in, out); } + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java index 111417d4b4..f979629061 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java @@ -45,6 +45,7 @@ import io.netty.channel.PartialFlushException; * */ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapter { + private boolean removed; @Override public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { @@ -52,7 +53,7 @@ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapte ByteBuf out = ctx.nextOutboundByteBuffer(); boolean encoded = false; - while (in.readable()) { + while (!removed && in.readable()) { int oldInSize = in.readableBytes(); try { encode(ctx, in, out); @@ -89,4 +90,10 @@ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapte * @throws Exception is thrown if an error accour */ protected abstract void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception; + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java index d82e8c6b23..8a421c8b31 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java @@ -45,6 +45,7 @@ public abstract class ByteToMessageDecoder private ChannelHandlerContext ctx; private volatile boolean singleDecode; + private boolean removed; /** * If set then only one message is decoded on each {@link #inboundBufferUpdated(ChannelHandlerContext)} call. @@ -113,7 +114,7 @@ public abstract class ByteToMessageDecoder ByteBuf in = ctx.inboundByteBuffer(); boolean decoded = false; - while (in.readable()) { + while (!removed && in.readable()) { try { int oldInputLength = in.readableBytes(); Object o = decode(ctx, in); @@ -204,4 +205,10 @@ public abstract class ByteToMessageDecoder protected Object decodeLast(ChannelHandlerContext ctx, ByteBuf in) throws Exception { return decode(ctx, in); } + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java index fc1b545bd2..eccadea719 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java @@ -45,6 +45,7 @@ import io.netty.channel.ChannelPromise; public abstract class MessageToByteEncoder extends ChannelOutboundMessageHandlerAdapter { private final Class[] acceptedMsgTypes; + private boolean removed; /** * The types which will be accepted by the encoder. If a received message is an other type it will be just forwared @@ -59,7 +60,7 @@ public abstract class MessageToByteEncoder extends ChannelOutboundMessageHand MessageBuf in = ctx.outboundMessageBuffer(); ByteBuf out = ctx.nextOutboundByteBuffer(); - for (;;) { + while (!removed) { Object msg = in.poll(); if (msg == null) { break; @@ -105,4 +106,10 @@ public abstract class MessageToByteEncoder extends ChannelOutboundMessageHand * @throws Exception is thrown if an error accour */ protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception; + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java index c70eb8c167..7fd7af7a5a 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java @@ -49,6 +49,7 @@ public abstract class MessageToMessageDecoder extends ChannelInboundHandlerAdapter implements ChannelInboundMessageHandler { private final Class[] acceptedMsgTypes; + private boolean removed; /** * The types which will be accepted by the decoder. If a received message is an other type it will be just forwarded @@ -68,7 +69,7 @@ public abstract class MessageToMessageDecoder throws Exception { MessageBuf in = ctx.inboundMessageBuffer(); boolean notify = false; - for (;;) { + while (!removed) { try { Object msg = in.poll(); if (msg == null) { @@ -141,4 +142,10 @@ public abstract class MessageToMessageDecoder protected void freeInboundMessage(I msg) throws Exception { ChannelHandlerUtil.freeMessage(msg); } + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java index 932aa0d62f..2531988592 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java @@ -48,6 +48,7 @@ import io.netty.channel.PartialFlushException; public abstract class MessageToMessageEncoder extends ChannelOutboundMessageHandlerAdapter { private final Class[] acceptedMsgTypes; + private boolean removed; /** * The types which will be accepted by the decoder. If a received message is an other type it will be just forwared @@ -62,7 +63,7 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundMessageH MessageBuf in = ctx.outboundMessageBuffer(); boolean encoded = false; - for (;;) { + while (!removed) { try { Object msg = in.poll(); if (msg == null) { @@ -140,4 +141,10 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundMessageH protected void freeOutboundMessage(I msg) throws Exception { ChannelHandlerUtil.freeMessage(msg); } + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } } diff --git a/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java index b354dbdd40..097f094d11 100644 --- a/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java +++ b/transport/src/main/java/io/netty/channel/ChannelInboundMessageHandlerAdapter.java @@ -44,6 +44,7 @@ public abstract class ChannelInboundMessageHandlerAdapter extends ChannelInboundHandlerAdapter implements ChannelInboundMessageHandler { private final Class[] acceptedMsgTypes; + private boolean removed; /** * The types which will be accepted by the message handler. If a received message is an other type it will be just @@ -73,7 +74,7 @@ public abstract class ChannelInboundMessageHandlerAdapter try { MessageBuf in = ctx.inboundMessageBuffer(); - for (;;) { + while (!removed) { Object msg = in.poll(); if (msg == null) { break; @@ -164,4 +165,10 @@ public abstract class ChannelInboundMessageHandlerAdapter protected void freeInboundMessage(I msg) throws Exception { ChannelHandlerUtil.freeMessage(msg); } + + @Override + public void afterRemove(ChannelHandlerContext ctx) throws Exception { + super.afterRemove(ctx); + removed = true; + } }