Repository: netty/netty
File: codec-base/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java (Java)

Code before:
        ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
            int reentrancy;

            @Override
            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
                reentrancy++;
                if (reentrancy == 1) {
                    ByteBuf buf2 = channel.alloc().buffer();
                    buf2.writeLong(42); // Adding 8 bytes.
                    channel.writeInbound(buf2); // Reentrant call back into ByteToMessageDecoder
                    ctx.read(); // With LocalChannel this would be a reentrant call. For EmbeddedChannel it's harmless.
                }
                int bytes = in.readableBytes();
                out.add(bytes);
                in.skipBytes(bytes);
            }
        };
        channel.pipeline().addLast(decoder);
        ByteBuf buf1 = channel.alloc().buffer();
        buf1.writeInt(42); // Adding 4 bytes.
        channel.writeInbound(buf1);
        Integer first = channel.readInbound();
        Integer second = channel.readInbound();
        assertEquals(4, first);
        assertEquals(8, second);
        channel.finishAndReleaseAll();
    }
}


Review comment (suggestion):
```suggestion
        assertFalse(channel.finishAndReleaseAll());
```

Code after:
        ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
            int reentrancy;

            @Override
            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
                reentrancy++;
                if (reentrancy == 1) {
                    ByteBuf buf2 = channel.alloc().buffer();
                    buf2.writeLong(42); // Adding 8 bytes.
                    assertFalse(channel.writeInbound(buf2)); // Reentrant call back into ByteToMessageDecoder
                    ctx.read();
                }
                int bytes = in.readableBytes();
                out.add(bytes);
                in.skipBytes(bytes);
            }
        };
        channel.pipeline().addLast(decoder);
        ByteBuf buf1 = channel.alloc().buffer();
        buf1.writeInt(42); // Adding 4 bytes.
        assertTrue(channel.writeInbound(buf1));
        Integer first = channel.readInbound();
        Integer second = channel.readInbound();
        assertEquals(4, first);
        assertEquals(8, second);
        assertFalse(channel.finishAndReleaseAll());
    }

    @Test
    void reentrantReadThenRemoveSafety() throws Exception {
        EmbeddedChannel channel = new EmbeddedChannel();
        ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
            boolean removed;
            int reentrancy;

            @Override
            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
                assertFalse(removed);
                reentrancy++;
                if (reentrancy == 1) {
                    ByteBuf buf2 = channel.alloc().buffer();
                    buf2.writeLong(42); // Adding 8 bytes.
                    assertFalse(channel.writeInbound(buf2)); // Reentrant call back into ByteToMessageDecoder
                    ByteBuf buf3 = channel.alloc().buffer();
                    buf3.writeShort(42); // Adding 2 bytes.
                    assertFalse(channel.writeInbound(buf3)); // Reentrant call back into ByteToMessageDecoder
                    ctx.read();
                } else if (reentrancy == 2) {
                    ctx.pipeline().remove(this);
                }
                int bytes = in.readableBytes();