fix remove handler cause ByteToMessageDecoder out disorder (#9670)

Motivation:

Data flowing in from the decoder flows out in sequence,Whether decoder removed or not.

Modification:

fire data in out and clear out when hander removed
before call method handlerRemoved(ctx)

Result:

Fixes #9668 .
This commit is contained in:
switchYello 2019-10-21 15:48:30 +08:00 committed by Norman Maurer
parent c25eec0658
commit 402537cab0
2 changed files with 29 additions and 0 deletions

View File

@ -499,6 +499,8 @@ public abstract class ByteToMessageDecoder extends ChannelHandlerAdapter impleme
boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING;
decodeState = STATE_INIT;
if (removePending) {
fireChannelRead(ctx, out, out.size());
out.clear();
handlerRemoved(ctx);
}
}

View File

@ -399,4 +399,31 @@ public class ByteToMessageDecoderTest {
}
assertFalse(channel.finish());
}
@Test
public void testDisorder() {
ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
int count;
//read 4 byte then remove this decoder
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
out.add(in.readByte());
if (++count >= 4) {
ctx.pipeline().remove(this);
}
}
};
EmbeddedChannel channel = new EmbeddedChannel(decoder);
assertTrue(channel.writeInbound(Unpooled.wrappedBuffer(new byte[]{1, 2, 3, 4, 5})));
assertEquals((byte) 1, (byte) channel.readInbound());
assertEquals((byte) 2, (byte) channel.readInbound());
assertEquals((byte) 3, (byte) channel.readInbound());
assertEquals((byte) 4, (byte) channel.readInbound());
ByteBuf buffer5 = channel.readInbound();
assertEquals((byte) 5, buffer5.readByte());
assertFalse(buffer5.isReadable());
assertTrue(buffer5.release());
assertFalse(channel.finish());
}
}