Skip to content

fix(jetstream): do not queue extra pulls while a consume iterator body runs - #529

Closed
Pushkar236 wants to merge 1 commit into
nats-io:mainfrom
Pushkar236:fix/consume-iterator-heartbeat-pulls
Closed

Pushkar236 wants to merge 1 commit into
nats-io:mainfrom
Pushkar236:fix/consume-iterator-heartbeat-pulls

Conversation

@Pushkar236

Copy link
Copy Markdown

Resolves #524.

In iterator mode (for await over consume()), a pull is only published when the loop asks for its next item: pull() pushes a function into the iterator's queue. While a slow loop body runs, the queued pull is not sent, so no request is open at the server and no idle heartbeats arrive. The idle heartbeat monitor then fired on every interval, and each firing:

  • notified heartbeats_missed,
  • asked for the consumer info,
  • zeroed pending and queued another pull for the full max_messages.

When the body returned, all the queued pulls were sent at once, so the consumer held many more than max_messages unacknowledged messages, which were redelivered after ack_wait. A reconnect during the body added one more pull the same way.

This tracks the pulls queued but not yet published (queued, incremented in pull() and decremented when the iterator runs the pushed function), as suggested in the issue:

  • A reset (resetPendingWithInfo / resetPendingNoInfo, used by the monitor and on reconnect) now sets pending to the queued pulls instead of zero, and only pulls again when none is queued, since the queued one is still going to be sent. This alone stops the extra pulls.
  • While the iterator is running (yielding) and a pull is waiting behind its loop body, the monitor callback does not count missed heartbeats: it calls monitor.work(), so misses are counted from when the pull goes out, and returns. That removes the per-interval CONSUMER.INFO requests and the heartbeats_missed notifications on a healthy server. Before the iterator is started, the monitor behaves as before (the existing consume - heartbeats test relies on that).
  • reset() of an ordered consumer clears the queued counts too, and a queued pull skipped because the consumer was reset since no longer counts down, so stale pulls are not counted for the new consumer.

In callback mode the pushed function runs immediately, so queued goes back to zero right away and nothing changes there.

Tests

Two new tests in jetstream/tests/consume_test.ts, with max_messages: 1 and expires: 1000 (heartbeats every 500 ms), counting the pull requests on $JS.API.CONSUMER.MSG.NEXT.A.a:

  • a 4 s loop body for the first message: when the loop takes the second message, 2 pulls were sent, num_ack_pending is 1, and no heartbeats_missed was notified (on main: num_ack_pending 7);
  • the server restarts during the first message's body: 2 pulls and num_ack_pending 1 after the reconnect (on main: 3).

Reverting only the monitor change brings back the heartbeats_missed notifications (6), and reverting only the reset change makes the reconnect test fail (num_ack_pending 2).

With Deno 2.9.0 and nats-server 2.15.3: consume_test.ts 20/20, deno task test-kv 98/98, test-obj 104/104, and test-jetstream 405/406, where the failure is jetstream - mirror alternates in jscluster_test.ts, which doesn't use consumers and also failed once in three runs on main (it passed three times in a row on this branch). deno fmt --check and deno task lint-jetstream are clean.

Signed-off-by: Pushkar Kirange 71223317+Pushkar236@users.noreply.github.com

…y runs

In iterator mode a pull is only published when the loop asks for its next
item. While a slow loop body runs, the queued pull is not sent, no request
is open at the server and no heartbeats arrive, so the idle heartbeat
monitor fired on every interval: each firing notified heartbeats_missed,
asked for the consumer info, zeroed pending and queued another full pull.
When the body returned they were all sent at once, so the consumer held
many more than max_messages unacknowledged messages, redelivered after
ack_wait. A reconnect during the body added one more the same way.

Track the pulls queued but not yet published. A reset keeps them in
pending and only pulls again when none is queued, and while the iterator
runs with a pull held up behind its loop body, the monitor does not count
missed heartbeats. An ordered consumer reset clears the queued counts.

Resolves nats-io#524

Signed-off-by: Pushkar Kirange <71223317+Pushkar236@users.noreply.github.com>
@aricart

aricart commented Nov 19, 2026

Copy link
Copy Markdown
Member

@Pushkar236 thank you for reporting this - I believe while your PR addresses the issue in some way there's a simpler fix without the added accounting - firstly this is an impedance problem for very long processing and other settings (perhaps this is how you are reporting the issue) - we do have 'next()andfetch()` where you can limit what you pull and delegate to a worker.

I think the fix is to ignore the heartbeats missed timer when the user callback/iterator is blocked - which is a simpler design. I'll put a PR up and evaluate

@aricart

aricart commented Nov 20, 2026

Copy link
Copy Markdown
Member

@Pushkar236 I have fixed this differently - also solves additional issues
Fixed by #530

@aricart aricart closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jetstream: consume() iterator queues one extra pull per missed idle heartbeat while the loop body is slow, so max_messages is exceeded when it returns

2 participants