[Bug] Java client getData() consumes pooled heap batch payloads
User environment
Java client component tests against unchanged production sources at 17166c820bf011ce7dd8b2b6795cc9c62e734af7. The relevant MessageImpl, ConsumerImpl and ZLIB codec sources were also verified byte-for-byte unchanged at master commit e3dd37b6261dec99bd7d00816cc135c6e81bc59c. Environment: macOS x86_64, Temurin JDK 25.0.2. No broker/cluster was involved; these payload-read checks are not jcstress tests.
Issue description
With poolMessages=true, calling getData() on a live message backed by a sliced heap buffer consumes its payload reader index. A second call returns an empty array, and size() changes to zero, even though the message has not been released.
This is reachable for ZLIB-compressed batches: the ZLIB decoder returns a heap buffer, and pooled messages retain slices of that buffer. An earlier interceptor/logging read can therefore leave the subsequent application read empty.
Expected: both reads return the same four bytes and size() remains four.
Observed in a component check using the real codec output and ConsumerImpl.newSingleMessage:
first getData().length = 4
second getData().length = 0
size before / after = 4 / 0
final source buffer refCnt = 0
Pooled direct-buffer and non-pooled heap-buffer controls both preserve the four bytes across repeated reads. No buffer leak is claimed here.
Reproducing the issue
The following minimal regression test isolates the faulty production-class branch. Add imports for io.netty.buffer.Unpooled, java.util.Optional, and org.apache.pulsar.common.api.proto.SingleMessageMetadata, then add this method to the existing MessageImplTest:
@Test
public void testRepeatedReadOfPooledHeapSlice() {
ByteBuf source = Unpooled.wrappedBuffer(new byte[]{0, 1, 2, 3, 4});
MessageImpl<byte[]> message = MessageImpl.create(
"persistent://public/default/repro",
new BatchMessageIdImpl(1, 2, -1, 0),
new MessageMetadata().setProducerName("repro")
.setSequenceId(0).setPublishTime(1).setNumMessagesInBatch(1),
new SingleMessageMetadata(), source.slice(1, 4),
Optional.empty(), null, Schema.BYTES, 0, true,
Commands.DEFAULT_CONSUMER_EPOCH);
try {
assertEquals(message.size(), 4);
byte[] first = message.getData();
byte[] second = message.getData();
assertEquals(second, first); // fails: empty versus four bytes
assertEquals(message.size(), 4);
} finally {
message.release();
source.release();
}
}
Run with the repository's supported JDK:
./gradlew :pulsar-client-original:test --tests 'MessageImplTest.testRepeatedReadOfPooledHeapSlice'
Additional information
The heap-copy branch of MessageImpl.getData() uses payload.readBytes(data), unlike the direct branch's absolute getBytes call. A bounded fix is to use payload.getBytes(payload.readerIndex(), data) there and cover repeated reads of pooled heap slices, including ZLIB batch output. This issue does not require concurrent access to one message.
Anyone interested is welcome to pick this up. Please leave a comment if you'd like to work on a fix so we can coordinate and avoid duplicate efforts.
Anyone interested is welcome to pick this up. Please leave a comment if you'd like to work on a fix so we can coordinate and avoid duplicate efforts.
I am interested for this and I have submitted a PR.Thanks!