pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[Bug] Java client can omit a batch-index ACK when acknowledgment races with flush

Open void-ptr974 opened this issue 1 month ago • 3 comments

User environment

Java client component validation using unchanged production sources at 17166c820bf011ce7dd8b2b6795cc9c62e734af7, macOS x86_64, Temurin JDK 25.0.2. The relevant grouping tracker, consumer and message sources were verified byte-for-byte unchanged at master commit e3dd37b6261dec99bd7d00816cc135c6e81bc59c.

Issue description

A grouped individual batch-index acknowledgment can be accepted locally but omitted from subsequent flushes when the application acknowledgment races with flush(), in the absence of further acknowledgments of that batch.

Conditions:

  • Batch-index acknowledgment enabled.
  • Acknowledgment grouping enabled.
  • ACK receipts disabled, so the optional tracker read/write locks are not acquired.

The checked revision defaults to batch-index ACK enabled, a 100 ms group time, and ACK receipts disabled. No message pooling or failed channel write is required.

Reproducing the issue

A deterministic component reproduction uses the real PersistentAcknowledgmentsGroupingTracker with a controlled connection/channel boundary. Use a four-message batch, so acknowledging indices 0 and 1 does not turn the operation into a full-entry ACK.

  1. Acknowledge index 0 and leave its bitmap in pendingIndividualBatchIndexAcks without flushing.
  2. Start acknowledging index 1 on an application thread. Pause it after computeIfAbsent has returned the existing bitmap, before bitSet.clear(msgId.getBatchIndex()) runs.
  3. Run flush() on the other thread. It removes the entry with pollFirstEntry() and serializes the bitmap, which still only acknowledges index 0.
  4. Resume the application thread. It clears index 1 on the bitmap that is already detached and serialized, then marks ACKs pending and completes locally.
  5. Flush twice more, decode the successful CommandAck frames, and compare their acknowledged indices.

The pause can be implemented with latches in a BatchMessageIdImpl.getBatchIndex() override at the final clear call. The reproduction checks the production stack and that the input ACK-set monitor is not held; it does not reflect into or force private tracker state. The normal grouping-tracker addAcknowledgment entry point is used; ConsumerImpl and the external connection/channel boundaries are mocked. This does not claim a full public-consumer or broker end-to-end reproduction.

Expected behavior

Both accepted partial acknowledgments eventually appear on successful protocol frames: acknowledged indices {0, 1}.

Actual behavior

acknowledged indices in successful frames: {0}
expected acknowledged indices:            {0, 1}
pending batch-ACK map after final flush:   empty
fixture/protocol errors:                   0
failed channel writes:                    0

The target interleaving reproduced in two independent JVM invocations. Four control schedules per invocation passed: no pause, pause without concurrent flush, flush entirely before the ACK, and flush entirely after the ACK. Separate randomized jcstress runs did not observe this narrow window; the evidence here is the controlled legal schedule.

Additional information

The bitmap lookup and clear are separate operations in doIndividualBatchAckAsync. flushAsync can detach and serialize the same value between them. Individual thread safety of the map and bitmap does not make this handoff atomic.

The proposed fix direction is to coordinate bitmap mutation with extraction/serialization, while preserving receipt behavior and checking hot-path contention. Regression coverage should include the default non-receipt mode and a later flush that cannot recover the omitted bit.

The demonstrated consequence is an omitted client ACK, which can cause additional redelivery; later acknowledgments of the same batch may cover it. Permanent message loss and distributed broker behavior were not established. Unlike the earlier concurrent-flush/double-recycle issue #24724, this interleaving is an application ACK versus one flush and does not depend on recycling.

void-ptr974 avatar Oct 23 '26 15:10 void-ptr974

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.

void-ptr974 avatar Oct 23 '26 15:10 void-ptr974

I can pick this up @void-ptr974

ManasBhole avatar Oct 24 '26 16:10 ManasBhole

  1. 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 have submitted a PR to fix it. Thank you for reviewing if you are free time! https://github.com/apache/pulsar/pull/27146

AuroraTwinkle avatar Oct 29 '26 04:10 AuroraTwinkle