Skip to content

fix(pubsub): respect batch limits when acks are added concurrently - #9771

Open
abhiramshaji-cyber wants to merge 2 commits into
googleapis:mainfrom
abhiramshaji-cyber:fix-pubsub-ack-batch-limit
Open

abhiramshaji-cyber wants to merge 2 commits into
googleapis:mainfrom
abhiramshaji-cyber:fix-pubsub-ack-batch-limit

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown

When a subscriber acks or modAcks several messages at the same moment, one request could carry more messages than the configured batch limit, and now each request stays within it.

What this code is for

MessageQueue in handwritten/pubsub/src/message-queues.ts collects acks (AckQueue) and modAcks (ModAckQueue) and sends them to the server in batches. Every ack or modAck goes through add(), which is supposed to send the current batch first with flush() when adding one more message would reach maxMessages or MAX_BATCH_BYTES. flush() takes this._requests and replaces it with an empty array before its first await, then sends the batch it took.

Before this PR

An app sets maxMessages: 1 and modAcks three messages at the same moment (the gist from @vienneraphael in #9303 does exactly this).

  1. The first add() sees this._requests.length + 1 >= maxMessages, calls flush(), and stops at await this.flush(reason).
  2. The second and third add() run while the first is waiting. They see the same empty this._requests, pass the same if, and also stop at await this.flush(reason).
  3. All three flushes finish, and all three calls continue to this._requests.push(...). Nobody checks the limit again.
  4. The timer sends this._requests as one batch.
  5. The server gets one modifyAckDeadline request with 3 ack IDs while maxMessages is 1. With maxMessages: 2 and six messages the batches are [1, 1, 1, 3], and the same thing happens to the byte limit.

After this PR

Same app, same three modAcks at the same moment.

  1. The first add() sees the limit is reached and calls flush() without waiting for it. flush() has already taken the old batch by the time it returns its promise.
  2. The first add() pushes its message into the new empty this._requests straight away, and only then waits for the flush and for its own response.
  3. The second add() now sees one message in this._requests, so the if is true and it flushes that batch before pushing its own.
  4. The third add() does the same.
  5. The server gets three requests with 1 ack ID each. When the calls arrive one at a time, add() takes the same path as before, so those batches do not change.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: 1 request with 3 ack IDs, maxMessages 1"]
direction TB
  B1["3 modAcks at the same moment"] --> B2["add() checks this._requests"]
  B2 --> B3["limit reached, call flush()"]
  B3 --> B4{"await flush, then push"}
  B4 -->|"all 3 wake up and push"| B5["1 request with 3 ack IDs"]
  B4 -->|"one call at a time"| B6["1 ack ID per request"]
end

subgraph AFTER["AFTER: 3 requests with 1 ack ID each"]
direction TB
  A1["3 modAcks at the same moment"] --> A2["add() checks this._requests"]
  A2 --> A3["limit reached, call flush()"]
  A3 --> A4{"THE CHANGE: push, then await flush"}
  A4 -->|"next add() sees 1 message"| A5["3 requests with 1 ack ID"]
  A4 -->|"one call at a time"| A6["1 ack ID per request, same as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

In MessageQueue.add(), await this.flush(reason) becomes flushed = this.flush(reason), and the method now ends with await Promise.all([flushed, responsePromise.promise]) instead of return responsePromise.promise. The check, the start of the flush, and the push now happen together before add() waits on anything, so no other add() can run between them. This covers maxMessages and MAX_BATCH_BYTES, because both are checked in that same if.

Promise.all attaches to responsePromise.promise straight away, so if the message's own batch fails while the earlier flush is still running, the rejection still has a handler and reaches the caller as before.

Three tests in test/message-queues.ts call add() six times with Promise.all and check every sent batch: maxMessages of 1 and 2, and the byte limit. On main they fail with batch sizes 0,0,0,0,0,0,6, 1,1,1,3, and a batch of 783360 bytes. With the change, they pass, and so do the other 979 unit tests.

What changes for a user

  • Acks and modAcks sent at the same moment now respect maxMessages and the byte limit. The reproduction from pubsub: concurrent ACK/modACK queue additions exceed maxMessages in 6.0.1 #9303 now gives respectsLimit: true for all six rows.
  • When acks arrive one at a time, the batches are the same as before ([0, 1, 0, 1, 0, 1] for AckQueue and [1, 1, 1] for ModAckQueue at maxMessages: 1).
  • add() still resolves only after the message is sent, and still waits for the flush it started.
  • Out of scope: the empty AckQueue requests that @vienneraphael pointed out. They come from the existing this._requests.length + 1 >= maxMessages check, which flushes an empty batch when maxMessages is 1, and existing tests expect that flush. I left it alone so this PR only fixes the limit.

Thank you @vienneraphael for the very clear report and the gist. The numbered steps in the issue pointed straight at the await, and the table made the fix easy to check. I would really value the maintainers' view on two things:

  1. Is starting the flush and pushing before the await the approach you prefer? The other option I looked at was checking the limit again in a loop after each await. With the current + 1 >= check and maxMessages: 1 that loop never ends, so I went with this one, but I am happy to change it.
  2. Would you like the empty AckQueue requests handled here, or in a separate PR? That change would need two existing tests updated, so I wanted to ask first.
  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #9303 🦕

MessageQueue.add() checked the batch count and byte limits, awaited
flush(), and only then pushed the message. Every concurrent caller passed
the check against the same batch, waited on its own flush, and then all of
them appended to the fresh batch, so one ack or modAck RPC could carry more
ack IDs than maxMessages, or more bytes than MAX_BATCH_BYTES.

flush() already takes the current batch synchronously, so add() now starts
the flush, pushes the message into the new batch in the same tick, and
waits for both the flush and its own response afterwards. The check and the
push can no longer be split by another caller, and the sequential path
sends the same batches as before.

Fixes googleapis#9303
@abhiramshaji-cyber
abhiramshaji-cyber requested a review from a team as a code owner October 29, 2026 09:50
@product-auto-label product-auto-label Bot added the api: pubsub Issues related to the Pub/Sub API. label Oct 29, 2026
@github-actions
github-actions Bot requested a review from shivanee-p October 29, 2026 09:50

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request resolves a concurrency issue in the MessageQueue class where concurrent add() calls could overfill a batch. By capturing the flush promise and awaiting it alongside the response promise at the end of the method, the queue avoids blocking during the addition phase. Unit tests have also been added to verify that batch sizes and byte limits are respected under concurrent load. No review comments were provided, and the implementation appears correct and complete.

@shivanee-p
shivanee-p removed their request for review October 29, 2026 14:50
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi, hope you're doing well! I know reviews here take time with so many packages, so no rush. Whenever someone has a moment, could the pubsub system tests be started and the change get a look? It keeps each ack and modAck request within the batch limit when several are added at once. Happy to change anything you'd prefer.

@vienneraphael

Copy link
Copy Markdown

Hi @abhiramshaji-cyber, I'm glad my issue/gist helped, thank you for putting up this PR!

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hey, hope you're all doing well! And thank you @vienneraphael, your issue and gist made this so much easier to track down. I know the review queue is long right now, so no pressure at all. If anything would make this easier to look at, like more tests, a smaller diff, or a different take on the flush ordering, I'm happy to change it. The pubsub system tests are still waiting on a maintainer to start them whenever someone has a spare minute.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi everyone, hope you're doing well! Hey, it's me again from my note here on the 22nd. The earlier workflow runs had expired, so I merged the latest main into the branch and fresh runs are now waiting for approval. I know there's a lot on everyone's plate, so whenever a maintainer has a spare minute, could they approve them? And if anything else would help this along, I'm happy to do it.

This branch has not been deployed

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

Labels

api: pubsub Issues related to the Pub/Sub API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pubsub: concurrent ACK/modACK queue additions exceed maxMessages in 6.0.1

2 participants