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 — Calling ReadOnlyByteBufferBuf.getBytes(int, byte[], int, int) concurrently from multiple threads will fail · Issue #14070 · netty/netty

Calling ReadOnlyByteBufferBuf.getBytes(int, byte[], int, int) concurrently from multiple threads will fail #14070

New issue
Copy link
New issue
Copy link
Closed
#14072
Closed
Calling ReadOnlyByteBufferBuf.getBytes(int, byte[], int, int) concurrently from multiple threads will fail#14070
#14072
Copy link
Milestone
4.1.111.Final

Description

@lhotari
lhotari
opened on May 22, 2024
Issue body actions

Expected behavior

It is expected that getBytes can be safely called from multiple threads, in a similar way as PooledDirectByteBuf.getBytes(int, byte[], int, int) can be safely called from multiple threads.

This is the PooledDirectByteBuf.getBytes(int, byte[], int, int) implementation which duplicates that internalByteBuffer for each call:

netty/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java

Lines 126 to 131 in e5951d4

@Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
checkDstIndex(index, length, dstIndex, dst.length);
_internalNioBuffer(index, length, true).get(dst, dstIndex, length);
return this;
}

It seems that for most ByteBuf instances, this was addressed with #1865 changes in 2013 with commit a74149e.

Data corruption when duplicate/slice buffers and use on different EventLoops #1865

There is some possible data corruption when using duplicate slice buffers on different EventLoops. This is because all read* operations delegate to the get* operations of the original buffer, but the get* operation here use internalNioBuffer() in some cases which is created only once. The correct way to fix this is to have get* operations always create a new ByteBuffer if needed and only use the internalNioBuffer() if a read* operation is triggered. This is safe as read* operations will modify the readerIndex anyway and so are not thread-safe.

The expectation is well stated in #1865 description.
"The correct way to fix this is to have get* operations always create a new ByteBuffer if needed and only use the internalNioBuffer() if a read* operation is triggered. This is safe as read* operations will modify the readerIndex anyway and so are not thread-safe."

Actual behavior

Calling ReadOnlyByteBufferBuf.getBytes(int, byte[], int, int) concurrently from multiple threads will fail because it shares a mutableinternalNioBuffer instance:

netty/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java

Lines 194 to 202 in e5951d4

@Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
checkDstIndex(index, length, dstIndex, dst.length);
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index).limit(index + length);
tmpBuf.get(dst, dstIndex, length);
return this;
}

Steps to reproduce

  • call ReadOnlyByteBufferBuf.getBytes(int, byte[], int, int) concurrently from multiple threads

Minimal yet complete reproducer code (or URL to code)

Netty version

4.1.108

JVM version (e.g. java -version)

OS version (e.g. uname -a)

Activity

  1. lhotari commented on May 22, 2024

    @lhotari
    lhotari
    on May 22, 2024
    ContributorAuthor
    More actions

    Related to #1865 .

  2. lhotari commented on May 22, 2024

    @lhotari
    lhotari
    on May 22, 2024
    ContributorAuthor
    More actions

    real stack trace from Pulsar WIP PR apache/pulsar#22760 (commit apache/pulsar@b9ad8db):

    java.lang.IllegalArgumentException: newPosition > limit: (2094 > 88)
        at java.base/java.nio.Buffer.createPositionException(Buffer.java:341)
        at java.base/java.nio.Buffer.position(Buffer.java:316)
        at java.base/java.nio.ByteBuffer.position(ByteBuffer.java:1516)
        at java.base/java.nio.ByteBuffer.put(ByteBuffer.java:1012)
        at io.netty.buffer.ReadOnlyByteBufferBuf.getBytes(ReadOnlyByteBufferBuf.java:210)
        at io.netty.buffer.ReadOnlyByteBufferBuf.getBytes(ReadOnlyByteBufferBuf.java:185)
        at io.netty.buffer.ReadOnlyByteBuf.getBytes(ReadOnlyByteBuf.java:276)
        at io.netty.buffer.UnsafeByteBufUtil.setBytes(UnsafeByteBufUtil.java:546)
        at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:193)
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1104)
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1096)
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1087)
        at io.netty.channel.AbstractCoalescingBufferQueue.copyAndCompose(AbstractCoalescingBufferQueue.java:322)
        at io.netty.handler.ssl.SslHandler$SslHandlerCoalescingBufferQueue.compose(SslHandler.java:2407)
        at io.netty.channel.AbstractCoalescingBufferQueue.remove(AbstractCoalescingBufferQueue.java:189)
        at io.netty.handler.ssl.SslHandler.wrap(SslHandler.java:841)
        at io.netty.handler.ssl.SslHandler.wrapAndFlush(SslHandler.java:822)
        at io.netty.handler.ssl.SslHandler.flush(SslHandler.java:803)
        at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:941)
        at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:921)
        at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:907)
        at io.netty.channel.ChannelOutboundHandlerAdapter.flush(ChannelOutboundHandlerAdapter.java:125)
        at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:939)
        at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:957)
        at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:982)
        at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:950)
        at org.apache.pulsar.broker.service.PulsarCommandSenderImpl.lambda$sendMessagesToConsumer$1(PulsarCommandSenderImpl.java:286)
        at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:413)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Thread.java:840)
    
  3. normanmaurer
    added this to the 4.1.111.Final milestone on May 23, 2024
  4. normanmaurer
    added a commit that references this issue on May 23, 2024

    ReadOnlyByteBufferBuf | ReadOnlyUnsafeDirectByteBuf get*, copy*, dupl…

    8ce1702
  5. normanmaurer commented on May 23, 2024

    @normanmaurer
    normanmaurer
    on May 23, 2024
    Member
    More actions

    @lhotari PTAL #14072

  6. normanmaurer
    closed this as completedin #14072on May 23, 2024
  7. normanmaurer
    added 2 commits that reference this issue on May 23, 2024

    ReadOnlyByteBufferBuf | ReadOnlyUnsafeDirectByteBuf get*, copy*, dupl… (

    d19f902

    ReadOnlyByteBufferBuf | ReadOnlyUnsafeDirectByteBuf get*, copy*, dupl… (

    600697d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      • 4.1.111.Final

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.