GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — retained[Slice|Duplicate] buffer reference count bug by Scottmitch · Pull Request #6024 · netty/netty

Conversation

@Scottmitch

@Scottmitch Scottmitch commented Nov 15, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation:
Currently the ByteBuf created as a result of retained[Slice|Duplicate] maintains its own reference count, and when this reference count is depleated it will release the ByteBuf returned from unwrap(). The unwrap() buffer is designed to be the 'root parent' and will skip all intermediate layers of buffers. If the intermediate layers of buffers contain a retained[Slice|Duplicate] then these reference counts will be ignored during deallocation. This may lead to deallocating the 'root parent' before all derived pooled buffers are actually released. This same issue holds if a retained[Slice|Duplicate] is in the heirachy and a 'regular' slice() or duplicate() buffer is created.

Modifications:

  • AbstractPooledDerivedByteBuf must maintain a reference to the direct parent (the buffer which retained[Slice|Duplicate] was called on) and release on this buffer instead of the 'root parent' returned by unwrap()
  • slice() and duplicate() buffers created from AbstractPooledDerivedByteBuf must also delegate reference count operations to their immediate parent (or first ancestor which maintains an independent reference count).

Result:
Fixes #5999

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Scottmitch Scottmitch added the defect label Nov 15, 2016
@Scottmitch Scottmitch added this to the 4.1.7.Final milestone Nov 15, 2016
@Scottmitch Scottmitch self-assigned this Nov 15, 2016
@Scottmitch

Scottmitch commented Nov 15, 2016

Copy link
Copy Markdown
Member Author

@rkapsi - can you verify this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch Scottmitch mentioned this pull request Nov 15, 2016
IllegalReferenceCountException with DefaultHttp2ConnectionEncoder#writeData() and PooledSlicedByteBuf #5999
Closed
@rkapsi

rkapsi commented Nov 15, 2016

Copy link
Copy Markdown
Member

@Scottmitch sweet, doing it right away.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch from e6ff32e to ee63d01 Compare November 15, 2016 21:37
@rkapsi

rkapsi commented Nov 15, 2016

Copy link
Copy Markdown
Member

@Scottmitch unfortunately negative

2016-11-15 22:12:47,213 [WorkerThread-2] ReferenceCountUtil WARN : Failed to release a message: UnpooledSlicedByteBuf(freed)
 io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release0(AbstractReferenceCountedByteBuf.java:110) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:98) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractDerivedByteBuf.release(AbstractDerivedByteBuf.java:65) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.CompositeByteBuf$Component.freeIfNecessary(CompositeByteBuf.java:1675) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.CompositeByteBuf.deallocate(CompositeByteBuf.java:1940) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release0(AbstractReferenceCountedByteBuf.java:115) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:98) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractDerivedByteBuf.release(AbstractDerivedByteBuf.java:65) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:84) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.util.ReferenceCountUtil.safeRelease(ReferenceCountUtil.java:109) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.PendingWriteQueue.removeAndFailAll(PendingWriteQueue.java:186) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.ssl.SslHandler.flush(SslHandler.java:484) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:787) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:779) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:760) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelDuplexHandler.flush(ChannelDuplexHandler.java:117) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:787) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:779) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:760) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.codec.http2.Http2ConnectionHandler.flush(Http2ConnectionHandler.java:163) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.codec.http2.Http2ConnectionHandler.channelWritabilityChanged(Http2ConnectionHandler.java:385) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelWritabilityChanged(DefaultChannelPipeline.java:1357) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.DefaultChannelPipeline.fireChannelWritabilityChanged(DefaultChannelPipeline.java:938) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelOutboundBuffer.fireChannelWritabilityChanged(ChannelOutboundBuffer.java:599) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelOutboundBuffer.setWritable(ChannelOutboundBuffer.java:565) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelOutboundBuffer.decrementPendingOutboundBytes(ChannelOutboundBuffer.java:208) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelOutboundBuffer.remove(ChannelOutboundBuffer.java:273) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelOutboundBuffer.removeBytes(ChannelOutboundBuffer.java:352) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.socket.nio.NioSocketChannel.doWrite(NioSocketChannel.java:442) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.flush0(AbstractChannel.java:856) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.flush0(AbstractNioChannel.java:362) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.flush(AbstractChannel.java:823) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.flush(DefaultChannelPipeline.java:1296) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:787) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:779) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:760) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
...
io.netty.util.IllegalReferenceCountException: refCnt: 0
    at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1407) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1353) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.PooledUnsafeDirectByteBuf.nioBuffer(PooledUnsafeDirectByteBuf.java:324) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractUnpooledSlicedByteBuf.nioBuffer(AbstractUnpooledSlicedByteBuf.java:454) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.CompositeByteBuf.nioBuffers(CompositeByteBuf.java:1498) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractUnpooledSlicedByteBuf.nioBuffers(AbstractUnpooledSlicedByteBuf.java:460) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.buffer.AbstractByteBuf.nioBuffers(AbstractByteBuf.java:1203) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.ssl.SslHandler.wrap(SslHandler.java:676) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.ssl.SslHandler.wrap(SslHandler.java:513) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.ssl.SslHandler.flush(SslHandler.java:481) ~[netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:787) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:779) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:760) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelDuplexHandler.flush(ChannelDuplexHandler.java:117) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:787) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:779) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:760) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.codec.http2.Http2ConnectionHandler.flush(Http2ConnectionHandler.java:163) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.handler.codec.http2.Http2ConnectionHandler.channelWritabilityChanged(Http2ConnectionHandler.java:385) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:445) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelWritabilityChanged(AbstractChannelHandlerContext.java:427) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelWritabilityChanged(AbstractChannelHandlerContext.java:420) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
    at io.netty.channel.ChannelInboundHandlerAdapter.channelWritabilityChanged(ChannelInboundHandlerAdapter.java:119) [netty-all-4.1.7.Final-SQSP21.jar:4.1.7.Final-SNAPSHOT]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi

rkapsi commented Nov 15, 2016

Copy link
Copy Markdown
Member

Just wondering... If you look at the stack traces in #5999 there's this case:

java.lang.Exception
    at io.netty.buffer.Caller.<init>(Caller.java:41)
    at io.netty.buffer.AdvancedLeakAwareByteBuf.<init>(AdvancedLeakAwareByteBuf.java:62)
    at io.netty.buffer.AdvancedLeakAwareByteBuf.slice(AdvancedLeakAwareByteBuf.java:97)
    at io.netty.buffer.CompositeByteBuf.addComponent0(CompositeByteBuf.java:259)
    at io.netty.buffer.CompositeByteBuf.addComponent(CompositeByteBuf.java:195)
    at io.netty.buffer.WrappedCompositeByteBuf.addComponent(WrappedCompositeByteBuf.java:524)
    at io.netty.buffer.AdvancedLeakAwareCompositeByteBuf.addComponent(AdvancedLeakAwareCompositeByteBuf.java:939)
    at io.netty.channel.CoalescingBufferQueue.compose(CoalescingBufferQueue.java:161)
    at io.netty.channel.CoalescingBufferQueue.remove(CoalescingBufferQueue.java:137)

A plain PooledSlicedByteBuf.slice() is being called which yields to an UnpooledSlicedByteBuf. Is that correct? Wondering if we have also an error like this:

@Test
  public void testPooledReleasePlainSlice() {
    EmbeddedChannel channel = new EmbeddedChannel();
    try {
      ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
      ByteBuf cumulation = alloc.buffer();
      try {

        cumulation.writeBytes(new byte[64]);

        ByteBuf content = cumulation.readRetainedSlice(64);
        cumulation.release();

        ByteBuf slice = content.slice();
        System.out.println("slice *before* content release: " + slice + ", refCnt=" + slice.refCnt());

        content.release();

        System.out.println("slice *after* content release: " + slice + ", refCnt=" + slice.refCnt());

        // Kaboom!
        slice.release();

      } finally {
        ReferenceCountUtil.safeRelease(cumulation);
      }
    } finally {
      channel.close();
    }
  }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 15, 2016

Copy link
Copy Markdown
Member Author

@rkapsi - looking now

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi

rkapsi commented Nov 15, 2016

Copy link
Copy Markdown
Member

Forgot to say, if you look at the above Exception it's saying:

ReferenceCountUtil WARN : Failed to release a message: UnpooledSlicedByteBuf(freed)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 15, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@rkapsi - The CoalescingBufferQueue assumes reference count ownership of the buffer, and from that perspective I think the slice is OK ... bcz at this point the buffer is being removed from the CoalescingBufferQueue. To clarify what is the content.release(); and slice.release(); simulating? I'm guessing one is simulating a write and the other is simulating something in the pipeline?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi

rkapsi commented Nov 15, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Just kicking around some ideas as we're passing a PooledSlicedByteBuf into CoalescingBufferQueue but it's failing on an UnpooledSlicedByteBuf and my testing in #5999 says it is originating from plain slicing.

What about this...? Running on your branch...

@Test
  public void testPooledReleasePlainSlice() {
    EmbeddedChannel channel = new EmbeddedChannel();
    try {
      ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
      ByteBuf cumulation = alloc.buffer();
      try {

        cumulation.writeBytes(new byte[64]);

        // HttpObjectDecoder
        ByteBuf content = cumulation.readRetainedSlice(64);
        cumulation.release();

        ByteBuf toReturn = content.readRetainedSlice(32);
        ByteBuf slice = toReturn.slice();

        System.out.println("cumulation #1: " + cumulation + ", refCnt=" + cumulation.refCnt());
        System.out.println("content #1: " + content + ", refCnt=" + content.refCnt());
        System.out.println("toReturn #1: " + toReturn + ", refCnt=" + toReturn.refCnt());
        System.out.println("slice #1: " + slice + ", refCnt=" + slice.refCnt());
        System.out.println();

        // Channel#write()
        slice.release();

        // Notice how cumulation is freed but refCnt of content and toReturn are >= 1
        System.out.println("cumulation #2: " + cumulation + ", refCnt=" + cumulation.refCnt());
        System.out.println("content #2: " + content + ", refCnt=" + content.refCnt());
        System.out.println("toReturn #2: " + toReturn + ", refCnt=" + toReturn.refCnt());
        System.out.println("slice #2: " + slice + ", refCnt=" + slice.refCnt());
        System.out.println();

      } finally {
        // Kaboom!
        cumulation.release();
      }
    } finally {
      channel.close();
    }
  }
cumulation #1: PooledUnsafeDirectByteBuf(ridx: 64, widx: 64, cap: 256), refCnt=1
content #1: PooledSlicedByteBuf(ridx: 32, widx: 64, cap: 64/64, unwrapped: PooledUnsafeDirectByteBuf(ridx: 64, widx: 64, cap: 256)), refCnt=2
toReturn #1: PooledSlicedByteBuf(ridx: 0, widx: 32, cap: 32/32, unwrapped: PooledUnsafeDirectByteBuf(ridx: 64, widx: 64, cap: 256)), refCnt=1
slice #1: UnpooledSlicedByteBuf(ridx: 0, widx: 32, cap: 32/32, unwrapped: PooledUnsafeDirectByteBuf(ridx: 64, widx: 64, cap: 256)), refCnt=1

cumulation #2: PooledUnsafeDirectByteBuf(freed), refCnt=0
content #2: PooledSlicedByteBuf(ridx: 32, widx: 64, cap: 64/64, unwrapped: PooledUnsafeDirectByteBuf(freed)), refCnt=2
toReturn #2: PooledSlicedByteBuf(ridx: 0, widx: 32, cap: 32/32, unwrapped: PooledUnsafeDirectByteBuf(freed)), refCnt=1
slice #2: UnpooledSlicedByteBuf(freed), refCnt=0

FAILED: testPooledReleasePlainSlice
io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release0(AbstractReferenceCountedByteBuf.java:110)
...
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

what is the last cumulation.release(); simulating?

Here is how I interpret the test case ... let me know if this makes sense and please fill in the blanks

  1. 64 bytes are read from the socket
  2. ByteToMessageDecoder sets this to the cumulation buffer (cumulation.writeBytes(new byte[64]); represents 1 and 2)
  3. ByteToMessageDecoder calls decode, HttpObjectDecoder decodes something (ByteBuf content = cumulation.readRetainedSlice(64);)
  4. ByteToMessageDecoder releases the cumulation bcz the buffer is no longer readable [1].
  5. channelReadComplete is called, and flush is called as a result ... http/2 flow controller is allowed to write some amount of bytes (32 in the most recent example e.g ByteBuf toReturn = content.readRetainedSlice(32);)
  6. This is where it becomes less clear ... then I'm assuming http/2 flow controller is allowed to write at least 32 more bytes (ByteBuf slice = toReturn.slice();)
  7. slice is written but not toReturn?
  8. I'm not sure what the last cumulation.release(); correlates to.

[1] https://github.com/netty/netty/blob/4.1/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java#L256

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

@rkapsi - to update the unit test you provided in #5999 this seems like an "expected" flow ... now lets try to figure out the piece I'm not accounting for :)

    @Test
    public void testRemoveWithPooledByteBuf() {
        EmbeddedChannel channel = new EmbeddedChannel();

        ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
        ByteBuf cumulation = alloc.buffer();
        cumulation.writeBytes(new byte[64]);

        // HttpObjectDecoder
        ByteBuf content = cumulation.readRetainedSlice(64);
        cumulation.release();

        assertEquals(1, content.refCnt());
        assertEquals(1, cumulation.refCnt());

        CoalescingBufferQueue queue = new CoalescingBufferQueue(channel);

        // encoder.writeData
        queue.add(content);

        // Flow control allows us to write 32 bytes
        ByteBuf toReturn1 = queue.remove(32, channel.newPromise());
        System.out.println("toReturn #1: " + toReturn1 + ", refCnt=" + toReturn1.refCnt());

        // Channel#write()
        toReturn1.release();

        // Flow control allows us to write 16 bytes
        ByteBuf toReturn2 = queue.remove(16, channel.newPromise());
        System.out.println("toReturn #2: " + toReturn2 + ", refCnt=" + toReturn2.refCnt());

        // Channel#write()
        toReturn2.release();

        // Flow control allows us to write 16 bytes ... this is the entirety of the original buffer!
        ByteBuf toReturn3 = queue.remove(16, channel.newPromise());
        System.out.println("toReturn #3: " + toReturn3 + ", refCnt=" + toReturn3.refCnt());

        // Channel#write()
        toReturn3.release();

        assertEquals(0, content.refCnt());
        assertEquals(0, cumulation.refCnt());
        assertEquals(0, toReturn1.refCnt());
        assertEquals(0, toReturn2.refCnt());
        assertEquals(0, toReturn3.refCnt());

        ByteBuf toReturn4 = queue.remove(1, channel.newPromise());

        assertFalse(toReturn4.isReadable());
    }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi

rkapsi commented Nov 16, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@Scottmitch - I think we're missing queue.compose(...) step. The asserts will start failing but it's not triggering the IllegalRefCntEx (yet).

@Test
  public void testRemoveWithPooledByteBuf() {
    EmbeddedChannel channel = new EmbeddedChannel();

    ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
    ByteBuf cumulation1 = alloc.buffer();
    cumulation1.writeBytes(new byte[16]);

    ByteBuf cumulation2 = alloc.buffer();
    cumulation2.writeBytes(new byte[48]);

    // HttpObjectDecoder
    ByteBuf content1 = cumulation1.readRetainedSlice(16);
    cumulation1.release();

    ByteBuf content2 = cumulation2.readRetainedSlice(48);
    cumulation2.release();

    assertEquals(1, content1.refCnt());
    assertEquals(1, cumulation1.refCnt());

    assertEquals(1, content2.refCnt());
    assertEquals(1, cumulation2.refCnt());

    CoalescingBufferQueue queue = new CoalescingBufferQueue(channel);

    // encoder.writeData
    queue.add(content1);
    queue.add(content2);

    // Flow control allows us to write 32 bytes
    ByteBuf toReturn1 = queue.remove(32, channel.newPromise());
    System.out.println("toReturn #1: " + toReturn1 + ", refCnt=" + toReturn1.refCnt() 
        + ", cumulation1=" + cumulation1 + ", cumulation2=" + cumulation2);

    // Channel#write()
    toReturn1.release();

    // Flow control allows us to write 16 bytes
    ByteBuf toReturn2 = queue.remove(16, channel.newPromise());
    System.out.println("toReturn #2: " + toReturn2 + ", refCnt=" + toReturn2.refCnt() 
        + ", cumulation1=" + cumulation1 + ", cumulation2=" + cumulation2);

    // Channel#write()
    toReturn2.release();

    // Flow control allows us to write 16 bytes ... this is the entirety of the
    // original buffer!
    ByteBuf toReturn3 = queue.remove(16, channel.newPromise());
    System.out.println("toReturn #3: " + toReturn3 + ", refCnt=" + toReturn3.refCnt()
        + ", cumulation1=" + cumulation1 + ", cumulation2=" + cumulation2);

    // Channel#write()
    toReturn3.release();

    assertEquals(0, content1.refCnt());
    assertEquals(0, cumulation1.refCnt());
    assertEquals(0, content2.refCnt());
    assertEquals(0, cumulation2.refCnt());

    assertEquals(0, toReturn1.refCnt());
    assertEquals(0, toReturn2.refCnt());
    assertEquals(0, toReturn3.refCnt());

    ByteBuf toReturn4 = queue.remove(1, channel.newPromise());

    assertFalse(toReturn4.isReadable());
  }
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch Scottmitch mentioned this pull request Nov 16, 2016
LzmaFrameEncoderTest leak due to LzmaInputStream close behavior #6026
Closed
@rkapsi

rkapsi commented Nov 16, 2016

Copy link
Copy Markdown
Member

... and we're missing an actual consumption of bytes (release is not good enough). I hope I didn't make a mistake here but I tried to take CoalescingBufferQueue out of the equation.

I receive 16 + 48 bytes and then I'm trying to consume 32+16+16 bytes.

@Test
  public void testPooledReleasePlainSlice() {
    ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;

    // HttpObjectDecoder
    ByteBuf cumulation1 = alloc.buffer();
    cumulation1.writeBytes(new byte[16]);

    ByteBuf content1 = cumulation1.readRetainedSlice(16);
    cumulation1.release();

    ByteBuf cumulation2 = alloc.buffer();
    cumulation2.writeBytes(new byte[48]);

    ByteBuf content2 = cumulation1.readRetainedSlice(48);
    cumulation2.release();

    // queue.add(content1)
    // queue.add(content2)

    // queue.remove()
    ByteBuf current = content1;
    ByteBuf next = content2;

    // Flow control allows us to write 32 but current has only 16. 
    // Need to get an another 16 from next.
    CompositeByteBuf composite = alloc.compositeBuffer();
    composite.addComponent(true, current); // 16
    composite.addComponent(true, next.readRetainedSlice(16)); // 16

    ByteBuf toReturn1 = composite;
    System.out.println("toReturn #1: " + toReturn1 + ", refCnt=" + toReturn1.refCnt());

    // Channel.write()
    toReturn1.readBytes(toReturn1.readableBytes());
    toReturn1.release();

    // Flow control allows us to write 16 and next has 32 left.
    ByteBuf toReturn2 = next.readRetainedSlice(16);
    System.out.println("toReturn #2: " + toReturn2 + ", refCnt=" + toReturn2.refCnt());

    // Channel.write()
    toReturn2.readBytes(toReturn2.readableBytes());
    toReturn2.release();

    // Flow control allows us to write 16 and next has 16 left.
    ByteBuf toReturn3 = next;
    System.out.println("toReturn #3: " + toReturn3 + ", refCnt=" + toReturn3.refCnt());

    // Channel.write()
    toReturn3.readBytes(toReturn3.readableBytes());
    toReturn3.release();
  }
toReturn #1: CompositeByteBuf(ridx: 0, widx: 32, cap: 32, components=2), refCnt=1
toReturn #2: PooledSlicedByteBuf(ridx: 0, widx: 16, cap: 16/16, unwrapped: PooledUnsafeDirectByteBuf(freed)), refCnt=1
FAILED: testPooledReleasePlainSlice
io.netty.util.IllegalReferenceCountException: refCnt: 0
    at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1407)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.memoryAddress(PooledUnsafeDirectByteBuf.java:358)
    at io.netty.buffer.PooledSlicedByteBuf.memoryAddress(PooledSlicedByteBuf.java:81)
    at io.netty.buffer.UnsafeByteBufUtil.setBytes(UnsafeByteBufUtil.java:552)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:260)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1069)
    at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:829)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

@rkapsi - I do see something interesting in your revised example ... tracking this down ... then I'll look at your second one 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi

rkapsi commented Nov 16, 2016

Copy link
Copy Markdown
Member

I mentioned in #5999 briefly an exception in SslHandler which seems to trigger removeAndFailAll() and you asked if I knew what it was. I don't know what the exception is but the way SslHandler's flush() is written... I'd be potentially problematic for the above error. It'd try to consume a ByteBuf which may throw IllegalRefCntEx, it'll then go into the catch/finally and attempt to release it. Just speculating here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch from ee63d01 to 6b77f5d Compare November 16, 2016 03:44
@Scottmitch

Scottmitch commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

@rkapsi - There was another issue with the independent reference count maintained by AbstractPooledDerivedByteBuf ... if a "regular" slice() or duplicate() buffer was created on top of an instance of AbstractPooledDerivedByteBuf then the reference counting updates would all skip directly to the "root parent" instead of going through the "parent". The issue was uncovered when the content was split between two buffers because CompositeByteBuf makes a slice of the component that is added, and when this was released (toReturn1.release();) that was releasing the "root parent" instead of the "parent" ... which was a premature release.

Starting to go down the rabbit hole in a sense ... I need to think if there are any other cases where the reference count can "escape" in the same way ... but please try this new code I pushed and see if that changes behavior. /cc @normanmaurer

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch Scottmitch mentioned this pull request Nov 16, 2016
00fc239995ce7b683993c88bb82b30dbd5fe7982 HashedWheelTimer introduced … #6028
Closed
@rkapsi

rkapsi commented Nov 16, 2016

Copy link
Copy Markdown
Member

@Scottmitch recent change looks good. No errors.

In terms of this change/bug/looking into the depths of ByteBuf derivatives and reference counting. Isn't this some tree'ish but simple data structure?

Something like this:

https://i.imgsafe.org/c71008d957.jpg

Children have a reference of their parent and all retain/release operations are also (recursively) applied to the parents. Parents don't hold references of their children though and erroneous free'ing of parents doesn't propagate to the children. Each node has it's own refCnt that is not shared with its peers.

In regards to the first sentence. Netty applies reference counting only to the immediate parent and only during allocation/deallocation of a child. I'm on the fence but it feels incomplete. Here's an example of the current implementation and what it maybe should be instead:

@Test
public void testReferenceCountingWithParents() {
  ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;

  ByteBuf a = alloc.buffer();
  a.writeBytes(new byte[64]);

  ByteBuf b = a.retainedSlice();
  ByteBuf c = b.retainedSlice();

  assertEquals(2, a.refCnt()); // One can argue this should be 3
  assertEquals(2, b.refCnt());
  assertEquals(1, c.refCnt());

  c.retain();
  assertEquals(2, a.refCnt()); // One can argue this should be 4
  assertEquals(2, b.refCnt()); // and this should be 3
  assertEquals(2, c.refCnt());

  a.release(3); // One can argue I should be able to do this
  a.refCnt(); // This should be 1
  b.refCnt(); // This should be 3
  c.refCnt(); // This should be 2
}

In regards to the last sentence, incompleteness and your last change. There is something about that AbstractPooledDerivedByteBuf#slice() returning an anonymous instance of UnpooledSlicedByteBuf and sharing the parents reference count that puts me at unease. It works but breaks IMHO the tree'ish nature. I think there is a flaw in the API/class hirarchy because it's a relatevly easy problem and it should be easy to follow and reason about. Instead it's like a rabbit hole with special cases.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch from 6b77f5d to 59a5dc9 Compare November 16, 2016 17:43
@Scottmitch

Scottmitch commented Nov 16, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Isn't this some tree'ish but simple data structure

The diagram you provided generally illustrates the pooled behavior, but some of the text following the link may not be accurate. Here is a bit more details:

For pooled buffers:

  • retained* maintain their own reference count.
  • non retained* buffers with only non retained* buffers in the hierarchy share the reference count of the "root parent".
  • non retained* buffers with a retained* ancestor share the reference count of their immediate parent
  • if a retained* is in the hierarchy then all reference count operations go to the "parent" and when the parent is deallocated then the grandparent is also released.

for unpooled buffers:

  • retained* and non retained* both share the reference count of the "root parent".
  • reference count operations go directly to the "root parent"

flaw in the API/class hierarchy ... Instead it's like a rabbit hole with special cases.

This is primarily for 2 reasons:

  • Historically the reference count of derived buffers "slice()/duplicate()" was all shared with the "root parent". The API didn't explicitly guarantee this, but this is how it was implemented. Because the API didn't restrict to this we were able to implement the retained[Slice|Duplicate] variants which maintained an independent reference count.
  • Performance. If possible we can avoid keeping reference to the immediate immediate parent and unrolling the entire tree on each operation. However this PR demonstrates we cannot always do that once a retained element enters the hierarchy (for reference count operations).

There is something about that AbstractPooledDerivedByteBuf#slice() returning an anonymous instance of UnpooledSlicedByteBuf

I mentioned above that I needed to think of more cases and I agree the code as of last night was incomplete ... this code has since been changed. See new code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch 4 times, most recently from 490f17d to ec16942 Compare November 16, 2016 18:14
@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch from b9a4fc3 to 448ff0a Compare November 16, 2016 20:57
@Scottmitch

Scottmitch commented Nov 16, 2016

Copy link
Copy Markdown
Member Author

@rkapsi @normanmaurer - FYI ... I pushed some more code related to unpooled derived buffers generated from pooled derived buffers.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch
retained[Slice|Duplicate] buffer reference count bug
9fe094d
Motivation:
Currently the ByteBuf created as a result of retained[Slice|Duplicate] maintains its own reference count, and when this reference count is depleated it will release the ByteBuf returned from unwrap(). The unwrap() buffer is designed to be the 'root parent' and will skip all intermediate layers of buffers. If the intermediate layers of buffers contain a retained[Slice|Duplicate] then these reference counts will be ignored during deallocation. This may lead to deallocating the 'root parent' before all derived pooled buffers are actually released. This same issue holds if a retained[Slice|Duplicate] is in the heirachy and a 'regular' slice() or duplicate() buffer is created.

Modifications:
- AbstractPooledDerivedByteBuf must maintain a reference to the direct parent (the buffer which retained[Slice|Duplicate] was called on) and release on this buffer instead of the 'root parent' returned by unwrap()
- slice() and duplicate() buffers created from AbstractPooledDerivedByteBuf must also delegate reference count operations to their immediate parent (or first ancestor which maintains an independent reference count).

Result:
Fixes netty#5999
@Scottmitch
Scottmitch force-pushed the pooled_duplicate_ref_cnt branch from 448ff0a to 9fe094d Compare November 17, 2016 01:15
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java

@Override
public ByteBuf retainedDuplicate() {
return PooledDuplicatedByteBuf.newInstance(unwrap(), this, readerIndex(), writerIndex());

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java

@Override
public ByteBuf retainedDuplicate() {
return PooledDuplicatedByteBuf.newInstance(unwrap(), this, readerIndex(), writerIndex());

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java

@Override
public ByteBuf retainedSlice(int index, int length) {
return PooledSlicedByteBuf.newInstance(unwrap(), this, idx(index), length);

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java

@Override
public ByteBuf retainedSlice(int index, int length) {
return PooledSlicedByteBuf.newInstance(unwrap(), this, index, length);

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java
@Override
public ByteBuf slice(int index, int length) {
checkIndex0(index, length);
return new PooledNonRetainedSlicedByteBuf(referenceCountDelegate, unwrap(), idx(index), length);

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
netkins
netkins reviewed Nov 17, 2016
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AbstractPooledDerivedByteBuf.java
@Override
public ByteBuf slice(int index, int length) {
checkIndex0(index, length);
return new PooledNonRetainedSlicedByteBuf(referenceCountDelegate, unwrap(), index, length);

@netkins netkins Nov 17, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Prefix this call to "unwrap" with "super.". rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rkapsi

rkapsi commented Nov 17, 2016

Copy link
Copy Markdown
Member

@Scottmitch like the prior two commits, commit 9fe094d is looking good with no observable errors on our end.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Nov 17, 2016

Copy link
Copy Markdown
Member

@Scottmitch LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnou

johnou commented Nov 17, 2016

Copy link
Copy Markdown
Contributor

@Scottmitch @rkapsi great detective work!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch Scottmitch mentioned this pull request Nov 17, 2016
PooledByteBufAllocatorTest#testConcurrentUsage ByteBuf leak #6034
Closed
@Scottmitch

Scottmitch commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

Build leak looks unrelated and another issue was filed to track it #6034

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch

Scottmitch commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

4.1 (4bba752)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Scottmitch Scottmitch closed this Nov 17, 2016
@Scottmitch
Scottmitch deleted the pooled_duplicate_ref_cnt branch November 17, 2016 17:35
@Scottmitch

Scottmitch commented Nov 17, 2016

Copy link
Copy Markdown
Member Author

Thanks for your help @rkapsi !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rkapsi rkapsi mentioned this pull request Nov 17, 2016
~2GB of objects tied up in SslHandler's pendingUnencryptedWrites #5856
Closed
@johnou

johnou commented Nov 17, 2016

Copy link
Copy Markdown
Contributor

@Scottmitch anything to back port to 4.0?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Nov 17, 2016

Copy link
Copy Markdown
Member

@johnou nope

Scottmitch and johnou reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Oct 4, 2026
Fix retainedSlice() of non-retained pooled derived buffers lhotari/netty#4
Closed
Fix retainedSlice() of non-retained pooled derived buffers #17759
Merged
normanmaurer pushed a commit that referenced this pull request Oct 5, 2026
@lhotari
Fix retainedSlice() of non-retained pooled derived buffers (#17759)
d84dc6b
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with #6024 (4bba752, a fix for
#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.
This was referenced Oct 5, 2026
Auto-port 5.0: Fix retainedSlice() of non-retained pooled derived buffers #17760
Merged
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buffers #17761
Merged
normanmaurer pushed a commit that referenced this pull request Oct 5, 2026
@netty-project-bot @lhotari
Auto-port 5.0: Fix retainedSlice() of non-retained pooled derived buf…
134adc1
…fers (#17760)

Auto-port of #17759 to 5.0
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with #6024 (4bba752, a fix for
#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 5, 2026
@netty-project-bot @lhotari
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buf…
cf6c467
…fers (#17761)

Auto-port of #17759 to 4.1
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with #6024 (4bba752, a fix for
#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@netty-project-bot @lhotari @lltopk
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buf…
9889e33
…fers (netty#17761)

Auto-port of netty#17759 to 4.1
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with netty#6024 (4bba752, a fix for
netty#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer requested changes
+1 more reviewer
@netkins netkins netkins left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@Scottmitch Scottmitch

Labels

defect

Projects

None yet

Milestone

4.1.7.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@Scottmitch @rkapsi @normanmaurer @johnou @netkins
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.