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 #14070DescriptionExpected 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:
|
@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:
|
@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)
Activitylhotari commented on May 22, 2024 ContributorAuthor More actions lhotari commented 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)
normanmaurer commented on May 23, 2024
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|