GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — retained[Slice|Duplicate] buffer reference count bug by Scottmitch · Pull Request #6024 · netty/netty Conversation
|
|
@rkapsi - can you verify this? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Scottmitch sweet, doing it right away. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
e6ff32e to
ee63d01
Compare
|
@Scottmitch unfortunately negative |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just wondering... If you look at the stack traces in #5999 there's this case: 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(); } } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@rkapsi - looking now |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Forgot to say, if you look at the above Exception it's saying: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@rkapsi - The |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Just kicking around some ideas as we're passing a 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(); } } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
what is the last Here is how I interpret the test case ... let me know if this makes sense and please fill in the blanks
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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()); } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@Scottmitch - I think we're missing @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()); } } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
... 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(); } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@rkapsi - I do see something interesting in your revised example ... tracking this down ... then I'll look at your second one 👍 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I mentioned in #5999 briefly an exception in SslHandler which seems to trigger |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
ee63d01 to
6b77f5d
Compare
|
@rkapsi - There was another issue with the independent reference count maintained by 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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
6b77f5d to
59a5dc9
Compare
There was an error while loading. Please reload this page.
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:
for unpooled buffers:
This is primarily for 2 reasons:
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
490f17d to
ec16942
Compare
b9a4fc3 to
448ff0a
Compare
|
@rkapsi @normanmaurer - FYI ... I pushed some more code related to unpooled derived buffers generated from pooled derived buffers. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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
448ff0a to
9fe094d
Compare
|
|
||
| @Override | ||
| public ByteBuf retainedDuplicate() { | ||
| return PooledDuplicatedByteBuf.newInstance(unwrap(), this, readerIndex(), writerIndex()); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| @Override | ||
| public ByteBuf retainedDuplicate() { | ||
| return PooledDuplicatedByteBuf.newInstance(unwrap(), this, readerIndex(), writerIndex()); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| @Override | ||
| public ByteBuf retainedSlice(int index, int length) { | ||
| return PooledSlicedByteBuf.newInstance(unwrap(), this, idx(index), length); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| @Override | ||
| public ByteBuf retainedSlice(int index, int length) { | ||
| return PooledSlicedByteBuf.newInstance(unwrap(), this, index, length); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @Override | ||
| public ByteBuf slice(int index, int length) { | ||
| checkIndex0(index, length); | ||
| return new PooledNonRetainedSlicedByteBuf(referenceCountDelegate, unwrap(), idx(index), length); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @Override | ||
| public ByteBuf slice(int index, int length) { | ||
| checkIndex0(index, length); | ||
| return new PooledNonRetainedSlicedByteBuf(referenceCountDelegate, unwrap(), index, length); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Scottmitch like the prior two commits, commit 9fe094d is looking good with no observable errors on our end. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Scottmitch LGTM |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Scottmitch @rkapsi great detective work! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Build leak looks unrelated and another issue was filed to track it #6034 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
4.1 (4bba752) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for your help @rkapsi ! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Scottmitch anything to back port to 4.0? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@johnou nope |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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.
…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]>
…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]>
…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]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.