Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weak error handling in ByteToMessageDecoder? #9812

Closed
martinfurmanski opened this issue Nov 26, 2019 · 5 comments · Fixed by #9822
Closed

Weak error handling in ByteToMessageDecoder? #9812

martinfurmanski opened this issue Nov 26, 2019 · 5 comments · Fixed by #9822
Milestone

Comments

@martinfurmanski
Copy link
Contributor

martinfurmanski commented Nov 26, 2019

I have this stack trace from 4.1.32 for a leak report, but the code is not significantly changed in this area in 4.1.43 as far as I can see.

io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:331)
io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185)
io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:176)
io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:113)
io.netty.handler.codec.ByteToMessageDecoder.expandCumulation(ByteToMessageDecoder.java:529)
io.netty.handler.codec.ByteToMessageDecoder$1.cumulate(ByteToMessageDecoder.java:89)
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:656)
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:591)
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:508)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:470)
io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:909)
java.lang.Thread.run(Thread.java:748)
org.neo4j.helpers.NamedThreadFactory$2.run(NamedThreadFactory.java:122

My contention is that the error handling is weak around expandCumulation because for example:

    static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) {
        ByteBuf oldCumulation = cumulation;
        cumulation = alloc.buffer(oldCumulation.readableBytes() + readable);
        cumulation.writeBytes(oldCumulation);
        oldCumulation.release();
        return cumulation;
    }

if this fails after alloc.buffer then that buffer gets completely lost. Even up the stack in ByteToMessageDecoder$1.cumulate (MERGE_CUMULATOR) if writeBytes fails then the returned buffer gets lost.

Thoughts?

@njhill
Copy link
Member

njhill commented Nov 26, 2019

@martinfurmanski I think you are right. How about using a newCumulation var instead of oldCumulation like this:

    static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) {
        ByteBuf newCumulation = alloc.buffer(cumulation.readableBytes() + readable);
        try {
            newCumulation.writeBytes(cumulation);
            cumulation.release();
            return cumulation = newCumulation;
        } finally {
            if (newCumulation != cumulation) { 
                newCumulation.release();
            }
        }
    }

@normanmaurer
Copy link
Member

Sounds like a plan... @martinfurmanski do you want to provide a PR ?

@martinfurmanski
Copy link
Contributor Author

@njhill @normanmaurer Thanks for confirming the issue. I'll attempt a PR shortly!

martinfurmanski added a commit to martinfurmanski/netty that referenced this issue Nov 28, 2019
Motivation:

The buffer which the decoder allocates for the expansion can be
leaked if there is a subsequent issue writing to it.

Modifications:
The error handling has been improved so that the new buffer always
is released on failure in the expand.

Result:
The decoder will not leak in this scenario any more.

Fixes: netty#9812
normanmaurer pushed a commit that referenced this issue Nov 28, 2019
Motivation:

The buffer which the decoder allocates for the expansion can be
leaked if there is a subsequent issue writing to it.

Modifications:
The error handling has been improved so that the new buffer always
is released on failure in the expand.

Result:
The decoder will not leak in this scenario any more.

Fixes: #9812
@normanmaurer normanmaurer added this to the 4.1.44.Final milestone Nov 28, 2019
@normanmaurer
Copy link
Member

@martinfurmanski thanks again for the patch

normanmaurer pushed a commit that referenced this issue Nov 28, 2019
Motivation:

The buffer which the decoder allocates for the expansion can be
leaked if there is a subsequent issue writing to it.

Modifications:
The error handling has been improved so that the new buffer always
is released on failure in the expand.

Result:
The decoder will not leak in this scenario any more.

Fixes: #9812
@martinfurmanski
Copy link
Contributor Author

@martinfurmanski thanks again for the patch

Thank you both for the review and acceptance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants