[Bug] Releasing a pooled batch message clears broker metadata of live sibling messages
User environment
Java client component validation against unchanged production sources at 17166c820bf011ce7dd8b2b6795cc9c62e734af7, macOS x86_64, Temurin JDK 25.0.2. The relevant MessageImpl and ConsumerImpl sources were verified byte-for-byte unchanged at master commit e3dd37b6261dec99bd7d00816cc135c6e81bc59c.
Issue description
With poolMessages=true, messages from one batch share the same BrokerEntryMetadata instance. Releasing one message calls MessageImpl.recycle(), which clears that shared instance. Another message in the batch can still be live, but its broker timestamp and index have disappeared.
This requires a batch with broker entry metadata. The message body and producer getPublishTime() remain intact; this report concerns the broker timestamp/index stored in the shared metadata object. Message pooling is disabled by default.
Reproducing the issue
A local component check exercises the real MessagePayloadContextImpl.getMessageAt -> ConsumerImpl.newSingleMessage path, with only the external client/executor boundary mocked:
- Enable message pooling and construct a normal two-message batch.
- Give the batch a
BrokerEntryMetadatawithbrokerTimestamp=123andindex=456. - Obtain both messages with
context.getMessageAt(0, 2, payload, true, Schema.BYTES)andcontext.getMessageAt(1, 2, payload, true, Schema.BYTES). - Recycle the payload context and release the source payload, as after synchronous payload processing. Each delivered pooled message still owns its retained payload reference.
- Verify the second message has broker publish time 123 and index 456.
- Release only the first message on another thread. Wait for it to finish, then inspect the still-live second message again.
- Finally release the second message. Repeat with
poolMessages=falseas a control.
The observable application sequence is:
// first and second are live messages from the same metadata-bearing batch
first.release();
// second has not been released:
second.hasBrokerPublishTime(); // becomes false with pooling enabled
second.hasIndex(); // becomes false with pooling enabled
Expected behavior
Releasing one message must leave the metadata of every other live message unchanged. The second message should retain broker publish time 123 and index 456 until it is itself released.
Actual behavior
poolMessages=true poolMessages=false
second metadata valid before release true true
second metadata valid after first.release false true
second payload intact true true
final source buffer refCnt 0 0
release exception none none
A latch establishes completion of the first release before inspecting the second message. There is no double release or access to a released message. The component reproduction needs neither timing luck nor concurrent access to the same message. This is not a broker end-to-end or jcstress result.
Additional information
ConsumerImpl.newSingleMessage passes the batch's metadata reference to each message. MessageImpl.setBrokerEntryMetadata stores the reference directly; recycle() then clears it.
The fix should give this metadata a clear ownership rule: either detach a shared reference during recycling without mutating the shared instance, or give messages independently owned metadata. A regression test should release siblings in both orders and verify both broker metadata fields on the remaining live 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.
Hello @void-ptr974 I want to pick this up, can you please assign this issue to me?
Hi! I was able to reproduce this locally and opened a draft PR: https://github.com/apache/pulsar/pull/27098
The issue happens because all messages of a batch share the same BrokerEntryMetadata instance, and MessageImpl.recycle() called clear() on it. Releasing one pooled message therefore removed the broker publish time and index of the other messages from the batch that were still in use.
The PR changes recycle() to drop its reference to the metadata instead of clearing the shared instance. I added a regression test that releases the two messages of a batch in both orders and checks the broker publish time and index of the one that is still live. It fails without the change and passes with it.
I couldn't find another open PR for this after checking the issue timeline and a PR search. Feedback is welcome, and I'm happy to switch to giving each message its own copy of the metadata if you'd prefer that.