Correctly fix problem in ByteToMessageDecoder and ReplayingDecoder which could let to have a released buffer passed to the decode methods.

This fixes #1664 and revert also the original commit which was meant to fix it 3b1881b523 . The problem with the original commit was that it could delay handlerRemove(..) calls and so mess up the order or forward bytes to late.
This commit is contained in:
Norman Maurer 2013-08-01 09:54:07 +02:00
parent e3410680de
commit a06295fe0a
4 changed files with 48 additions and 113 deletions

View File

@ -229,6 +229,15 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
int outSize = out.size();
int oldInputLength = in.readableBytes();
decode(ctx, in, out);
// Check if this handler was removed before try to continue the loop.
// If it was removed it is not safe to continue to operate on the buffer
//
// See https://github.com/netty/netty/issues/1664
if (ctx.isRemoved()) {
break;
}
if (outSize == out.size()) {
if (oldInputLength == in.readableBytes()) {
break;

View File

@ -358,6 +358,15 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
int oldInputLength = in.readableBytes();
try {
decode(ctx, replayable, out);
// Check if this handler was removed before try to continue the loop.
// If it was removed it is not safe to continue to operate on the buffer
//
// See https://github.com/netty/netty/issues/1664
if (ctx.isRemoved()) {
break;
}
if (outSize == out.size()) {
if (oldInputLength == in.readableBytes() && oldState == state) {
throw new DecoderException(
@ -371,6 +380,15 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
}
} catch (Signal replay) {
replay.expect(REPLAY);
// Check if this handler was removed before try to continue the loop.
// If it was removed it is not safe to continue to operate on the buffer
//
// See https://github.com/netty/netty/issues/1664
if (ctx.isRemoved()) {
break;
}
// Return to the checkpoint (or oldPosition) and retry.
int checkpoint = this.checkpoint;
if (checkpoint >= 0) {

View File

@ -34,7 +34,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
private final DefaultChannelPipeline pipeline;
private final String name;
private final ChannelHandler handler;
private int callDepth;
private boolean removed;
// Will be set to null if no child executor should be used, otherwise it will be set to the
@ -78,10 +77,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
}
boolean isRunning() {
return callDepth != 0;
}
/** Invocation initiated by {@link DefaultChannelPipeline#teardownAll()}}. */
void teardown() {
EventExecutor executor = executor();
@ -159,13 +154,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelRegistered() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelRegistered(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -189,13 +179,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelUnregistered() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelUnregistered(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -219,13 +204,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelActive() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelActive(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -249,13 +229,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelInactive() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelInactive(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -292,13 +267,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeExceptionCaught(final Throwable cause) {
try {
callDepth ++;
try {
handler.exceptionCaught(this, cause);
} finally {
callDepth --;
}
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn(
@ -330,13 +300,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeUserEventTriggered(Object event) {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).userEventTriggered(this, event);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -364,13 +329,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelRead(Object msg) {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelRead(this, msg);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -398,13 +358,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelReadComplete() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelReadComplete(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -432,13 +387,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeChannelWritabilityChanged() {
try {
callDepth ++;
try {
((ChannelInboundHandler) handler).channelWritabilityChanged(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -498,13 +448,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeBind(SocketAddress localAddress, ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).bind(this, localAddress, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -541,13 +486,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeConnect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).connect(this, remoteAddress, localAddress, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -581,13 +521,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeDisconnect(ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).disconnect(this, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -614,13 +549,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeClose(ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).close(this, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -647,13 +577,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeDeregister(ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).deregister(this, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -682,13 +607,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeRead() {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).read(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}
@ -723,13 +643,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeWrite(Object msg, ChannelPromise promise) {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).write(this, msg, promise);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyOutboundHandlerException(t, promise);
}
@ -758,13 +673,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeFlush() {
try {
callDepth ++;
try {
((ChannelOutboundHandler) handler).flush(this);
} finally {
callDepth --;
}
} catch (Throwable t) {
notifyHandlerException(t);
}

View File

@ -506,8 +506,7 @@ final class DefaultChannelPipeline implements ChannelPipeline {
}
private void callHandlerRemoved(final DefaultChannelHandlerContext ctx) {
if (ctx.channel().isRegistered()) {
if (!ctx.executor().inEventLoop() || ctx.isRunning()) {
if (ctx.channel().isRegistered() && !ctx.executor().inEventLoop()) {
ctx.executor().execute(new Runnable() {
@Override
public void run() {
@ -516,7 +515,6 @@ final class DefaultChannelPipeline implements ChannelPipeline {
});
return;
}
}
callHandlerRemoved0(ctx);
}