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 — Fix concurrent ByteBuffer access issue in AdaptiveByteBuf.getBytes by chrisvest · Pull Request #15402 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Jun 24, 2025

Copy link
Copy Markdown
Member

Motivation:
The ByteBuf.getBytes methods should be thread-safe, as it is in theory non-mutating. However, their implementations access the internal NIO buffer and mutate the position and limit. The buffer is duplicated to try and counter this, but the duplication is done after the position and limit are changed on the potentially shared buffer.

Modification:
Make these methods duplicate the shared buffer first, then mutate the position and limit.

Result:
No more spooky position and limits on these buffers when AdaptiveByteBuf.getBytes is called concurrently on the same buffer by multiple threads.

Sorry, something went wrong.

Uh oh!

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

All reactions
@chrisvest chrisvest added this to the 4.2.3.Final milestone Jun 24, 2025
@chrisvest
chrisvest requested a review from normanmaurer June 24, 2025 19:31
normanmaurer
normanmaurer requested changes Jun 25, 2025
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

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.

I think there is a bug...

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

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

chrisvest and others added 2 commits June 25, 2025 10:58
@chrisvest
Fix concurrent ByteBuffer access issue in AdaptiveByteBuf.getBytes
d64513e
Motivation:
The ByteBuf.getBytes methods should be thread-safe, as it is in theory non-mutating.
However, their implementations access the internal NIO buffer and mutate the position and limit.
The buffer is duplicated to try and counter this, but the duplication is done after the position and limit are changed on the potentially shared buffer.

Modification:
Make these methods duplicate the shared buffer first, then mutate the position and limit.

Result:
No more spooky position and limits on these buffers when AdaptiveByteBuf.getBytes is called concurrently on the same buffer by multiple threads.
@chrisvest @normanmaurer
Fix issue spotted in review
a42abf0
Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer

normanmaurer commented Jun 25, 2025

Copy link
Copy Markdown
Member

@chrisvest there is still something wrong

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest
Fix typo bug in ByteBufUtil limit calculation
4c0c01e
@chrisvest
chrisvest force-pushed the 4.2-adaptivebytebuf-getbytes branch from e9e53dd to 4c0c01e Compare June 25, 2025 18:02
@chrisvest

chrisvest commented Jun 25, 2025

Copy link
Copy Markdown
Member Author

Yeah, I just pushed a fix.

normanmaurer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

normanmaurer
normanmaurer approved these changes Jun 25, 2025
View reviewed changes
@chrisvest
chrisvest merged commit 160137c into netty:4.2 Jun 25, 2025
@chrisvest
chrisvest deleted the 4.2-adaptivebytebuf-getbytes branch June 25, 2025 19:58
normanmaurer added a commit that referenced this pull request Jun 25, 2025
@chrisvest @normanmaurer
Fix concurrent ByteBuffer access issue in AdaptiveByteBuf.getBytes (#…
190a20a
…15402)

Motivation:
The ByteBuf.getBytes methods should be thread-safe, as it is in theory
non-mutating. However, their implementations access the internal NIO
buffer and mutate the position and limit. The buffer is duplicated to
try and counter this, but the duplication is done after the position and
limit are changed on the potentially shared buffer.

Modification:
Make these methods duplicate the shared buffer first, then mutate the
position and limit.

Result:
No more spooky position and limits on these buffers when
AdaptiveByteBuf.getBytes is called concurrently on the same buffer by
multiple threads.

---------

Co-authored-by: Norman Maurer <[email protected]>
@franz1981 franz1981 mentioned this pull request Sep 29, 2025
Adaptive's buffers setBytes performance is held back by NIO Buffers allocations #15723
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.3.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@chrisvest @normanmaurer
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.