Skip to content

fix(microservices): keep a newer kafka connection when a stale one fails - #17869

Merged
kamilmysliwiec merged 1 commit into
masterfrom
claude/reverent-mendeleev-2141e3
Sep 24, 2026
Merged

kamilmysliwiec merged 1 commit into
masterfrom
claude/reverent-mendeleev-2141e3

Conversation

@kamilmysliwiec

Copy link
Copy Markdown
Member

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?

Issue Number: N/A

When a ClientKafka.connect() attempt fails, its .catch handler sets this.initialized = null and calls discardPartialConnection(). That clears _consumer, _producer and client and disconnects whatever they currently point to. It never checks whether the failing attempt is still the current one. So if close() and then connect() run while attempt #1 is still pending, and #1 fails afterwards, it drops the newer attempt's initialized promise and disconnects the newer consumer and producer.

What is the new behavior?

This uses the same guard as ClientRedis and ClientRMQ (#17865):

  • connect() keeps the attempt's promise in a local variable and passes it to discardPartialConnection(). The shared state (initialized, client, _consumer, _producer) is only reset while this.initialized is still that promise.
  • initializeClientAndConnections() now also records the consumer and producer it creates in a per-attempt object. A stale attempt uses it to disconnect only its own instances, even after a newer attempt has replaced them on the client.

The new test, "should not discard a newer connection when a stale attempt fails", fails without the fix and passes with it.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

A stale attempt can still overwrite client, _consumer or _producer if it progresses past an await after close() + connect(), or even resolves successfully. This PR doesn't cover that; ClientRMQ has the same gap, and it can be handled separately.

A connect() attempt that failed after close() and a new connect() cleared the newer attempt's initialized promise and disconnected its consumer and producer. Only reset the shared state when the failing attempt is still the current one, and have it disconnect just the instances it created itself, as ClientRedis and ClientRMQ already do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant