Skip to content

fix(microservices): evict pending kafka requests on stop or rebalance - #18583

Merged
kamilmysliwiec merged 3 commits into
nestjs:masterfrom
mo74x:fix/kafka-client-routing-map-leaks
Nov 19, 2026
Merged

kamilmysliwiec merged 3 commits into
nestjs:masterfrom
mo74x:fix/kafka-client-routing-map-leaks

Conversation

@mo74x

@mo74x mo74x commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

In ClientKafka:

  1. registerConsumerEventListeners() never calls handleClose() when the reply consumer emits STOP or a non-restarting CRASH, leaving in-flight routingMap requests hanging indefinitely (nor does it ignore events from a stale replaced consumer).
  2. setConsumerAssignments() does not evict pending requests whose stamped REPLY_PARTITION was reassigned away during a consumer group rebalance.
  3. publish() awaits serializer.serialize() asynchronously before producer.send() without checking whether the caller already unsubscribed or the request was evicted, dispatching orphaned Kafka messages.

Issue Number: N/A

What is the new behavior?

  • Guards registerConsumerEventListeners() with consumer === this._consumer and calls this.handleClose() on STOP and non-restarting CRASH (!event?.payload?.restart).
  • Tracks (replyTopic, replyPartition) metadata per pending correlation id in pendingRequestPartitions and evicts only requests whose reply partition is missing from data.payload.memberAssignment during setConsumerAssignments().
  • Adds a disposal/routingMap guard in ClientKafka.publish() so cancelled or evicted requests skip producer.send() and late serialization rejections.
  • Adds unit tests in packages/microservices/test/client/client-kafka.spec.ts.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

None

@thekhegay

Copy link
Copy Markdown
Contributor

STOP handler calls handleClose(), so restart check in CRASH handler never helps. In kafkajs 2.2.4 onCrash does await disconnect() before it emits CRASH, and disconnect() calls stop() which emits STOP (consumer/index.js L98, L118, L257, L286). So on retriable crash with restart: true all in-flight requests fail with Connection closed first, then consumer restarts and rejoins. Spec emits events by hand so it doesnt show this order. Maybe keep handleClose() only for CRASH without restart and for close()

kafkajs stops the consumer (and leaves the group) before it reports a crash, restarting or not, so the stop event already covers a crash.
@kamilmysliwiec
kamilmysliwiec merged commit eb3f7ba into nestjs:master Nov 19, 2026
4 checks passed
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.

3 participants