Skip to content

Auto-port 5.0: Fix ByteBuf.setBytes(int, ByteBuffer) when the source is the buffer's own internal NIO buffer - #17624

Merged
normanmaurer merged 1 commit into
5.0from
auto-port-pr-17618-to-5.0
Sep 25, 2026
Merged

normanmaurer merged 1 commit into
5.0from
auto-port-pr-17618-to-5.0

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port of #17618 to 5.0
Cherry-picked commit: a06df5b


Motivation:

ByteBuf.setBytes(int, ByteBuffer) may be called with the buffer's own internal NIO buffer as
the source, which callers can obtain through ByteBuf.internalNioBuffer(int, int):

Implementations obtain their destination through internalNioBuffer(), which calls
clear() on the cached instance. When the source is that cached instance, the call
resets the caller's position and limit before they are read, so the copy is done with the
wrong offsets.

UnpooledDirectByteBuf already gets this right — it duplicates before calling
internalNioBuffer(...), we should align with it:

if (src == tmpNioBuf) {
src = src.duplicate();
}

Modification:

  • PooledDirectByteBuf.setBytes(int, ByteBuffer): move the src == tmpNioBuf check before
    internalNioBuffer().
  • AdaptiveByteBuf.setBytes(int, ByteBuffer): add the same check, before internalNioBuffer().

Result:

setBytes(int, ByteBuffer) copies the requested range for every allocator, whether or not the
source is the buffer's own internal NIO buffer.

The new test fails before this change on AdaptiveByteBufAllocatorTest and on
PooledByteBufAllocatorTest when run with -Dio.netty.noUnsafe=true; it passes on both afterwards.

…'s own internal NIO buffer (#17618)

### Motivation:

`ByteBuf.setBytes(int, ByteBuffer)` may be called with the buffer's own
internal NIO buffer as
the source, which callers can obtain through
`ByteBuf.internalNioBuffer(int, int)`:

Implementations obtain their destination through `internalNioBuffer()`,
which calls
`clear()` on the cached instance. When the source is that cached
instance, the call
resets the caller's position and limit before they are read, so the copy
is done with the
wrong offsets.

`UnpooledDirectByteBuf` already gets this right — it duplicates before
calling
internalNioBuffer(...), we should align with it:
https://github.com/netty/netty/blob/cd967a41cc8b930c21286348a526196e8e10a7e1/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java#L610-L612

### Modification:

* `PooledDirectByteBuf.setBytes(int, ByteBuffer)`: move the `src ==
tmpNioBuf` check before
  `internalNioBuffer()`.
* `AdaptiveByteBuf.setBytes(int, ByteBuffer)`: add the same check,
before `internalNioBuffer()`.

### Result:

`setBytes(int, ByteBuffer)` copies the requested range for every
allocator, whether or not the
source is the buffer's own internal NIO buffer.

The new test fails before this change on `AdaptiveByteBufAllocatorTest`
and on
`PooledByteBufAllocatorTest` when run with `-Dio.netty.noUnsafe=true`;
it passes on both afterwards.

(cherry picked from commit a06df5b)
@testlens-app

testlens-app Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 2 failed tests 🚨

Here is what you can do:

  1. Inspect the test failures carefully.
  2. If you are convinced that some of the tests are flaky, you can mute them below.
  3. Finally, trigger a rerun by checking the rerun checkbox.

Test Summary

Build PR / linux-x86_64-java25-adaptive build > Netty/Common

Test Runs Flakiness
SingleThreadEventExecutorTest > testSuspendDoesNotRaceWithCancelledScheduledTask() ❌ 15% 🔴

Build PR / macos-x86_64-java25-boringssl build > Netty/Transport/Native/KQueue

Test Runs Flakiness
KQueueDatagramUnicastIPv6MappedTest > testSimpleSendCompositeMixedByteBuf(TestInfo) ❌ 5% 🟠

Build PR / windows-x86_64-java25-boringssl > Netty/Buffer

Test Runs Flakiness
AdaptiveBigEndianHeapByteBufTest > testToStringMultipleThreads() ⚠️ 0% 🟢

🏷️ Commit: 9b1c520
▶️ Tests: 164733 executed
⚪️ Checks: 12/12 completed

Test Failures

SingleThreadEventExecutorTest > testSuspendDoesNotRaceWithCancelledScheduledTask() (Netty/Common in Build PR / linux-x86_64-java25-adaptive build)
org.opentest4j.AssertionFailedError: worker thread did not terminate at iteration 408 ==> expected: <false> but was: <true>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertFalse.failNotFalse(AssertFalse.java:63)
	at org.junit.jupiter.api.AssertFalse.assertFalse(AssertFalse.java:36)
	at org.junit.jupiter.api.Assertions.assertFalse(Assertions.java:239)
	at io.netty.util.concurrent.SingleThreadEventExecutorTest.testSuspendDoesNotRaceWithCancelledScheduledTask(SingleThreadEventExecutorTest.java:243)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
expected actual
false true
KQueueDatagramUnicastIPv6MappedTest > testSimpleSendCompositeMixedByteBuf(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-x86_64-java25-boringssl build)
org.opentest4j.AssertionFailedError
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSend0(DatagramUnicastTest.java:367)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSend(DatagramUnicastTest.java:194)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendCompositeMixedByteBuf(DatagramUnicastTest.java:167)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest$5.run(DatagramUnicastTest.java:158)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest$5.run(DatagramUnicastTest.java:155)
	at io.netty.testsuite.transport.AbstractComboTestsuiteTest.run(AbstractComboTestsuiteTest.java:52)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendCompositeMixedByteBuf(DatagramUnicastTest.java:155)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
AdaptiveBigEndianHeapByteBufTest > testToStringMultipleThreads() (Netty/Buffer in Build PR / windows-x86_64-java25-boringssl)
java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:259)
	at java.base/java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:437)
	at io.netty.buffer.AbstractByteBufTest$30.run(AbstractByteBufTest.java:2316)
	at java.base/java.lang.Thread.run(Thread.java:1474)
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:252)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:210)
		... 3 more
	Suppressed: java.util.concurrent.BrokenBarrierException
		at java.base/java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:210)
		... 3 more

Rerun Controls

Select tests to mute in this pull request:

  • AdaptiveBigEndianHeapByteBufTest > testToStringMultipleThreads()
  • KQueueDatagramUnicastIPv6MappedTest > testSimpleSendCompositeMixedByteBuf(TestInfo)
  • SingleThreadEventExecutorTest > testSuspendDoesNotRaceWithCancelledScheduledTask()

Reuse successful test results:

  • ♻️ Only rerun the tests that failed or were muted before

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit ae99c0a into 5.0 Sep 25, 2026
10 of 13 checks passed
@normanmaurer
normanmaurer deleted the auto-port-pr-17618-to-5.0 branch September 25, 2026 16:38
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Sep 25, 2026
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 this pull request may close these issues.

3 participants