Repository navigation
fix(microservices): let the redis client retry after a failed connect - #17828
Merged
kamilmysliwiec merged 4 commits intoSep 22, 2026
Merged
Conversation
When retryAttempts is configured and the retries are exhausted, the failed attempt stays cached in connectionPromise, so every later connect() call replays the cached "connection lost" rejection instead of trying again, even once the server is reachable. The clients the failed attempt managed to create are also left behind without being closed. Reset the cached promise when the attempt fails and quit the clients it created, so the next call starts over, same as the kafka client got in nestjs#17816
Resetting the cached promise in a catch around the attempt missed how
the redis client manages its state: the "reconnecting", "ready" and
"end" listeners rewrite connectionPromise and the pub/sub clients.
- When ioredis gives up on a client ("end", retryAttempts not set or
exhausted), drop the pair, reset connectionPromise and
wasInitialConnectionSuccessful and disconnect the other client, so
the next connect() call starts over. With retryAttempts set, a
"Connection lost" rejection stayed cached forever, and without it
the new sub client never got the response listener.
- Only reset in the catch when no listener took over connectionPromise,
so ioredis keeps retrying a failed first attempt in the background
instead of the clients being quit right away.
- Ignore the events of clients that are no longer the current pair, so
a dropped or closed client shutting down late cannot re-cache a
rejection, drop the new clients, or throw on a null sub client.
- Keep the listeners registered with on() and attach them to every
pair.
Replace the no-op stubs in the tests with fakes that emit the
connection events, and the test that relied on a closed port with one
running a real ioredis client against a local server that drops every
connection.
ioredis leaves a client that is disconnected while waiting to reconnect in the "reconnecting" status, so wait for the connection attempts to stop instead of for every client to reach "end".
4 of 12 tasks
4 of 12 tasks
kamilmysliwiec
added a commit
that referenced
this pull request
Sep 24, 2026
…17865) * fix(microservices): let the rmq client retry after a failed connect When the first connection attempt fails, the rejection stays cached in connectionPromise, so every later connect() call replays the error of the first attempt until close() is called, even once the broker is reachable again. The connection manager the attempt created also stays behind and keeps retrying in the background Reset the cached promise when the attempt fails and close the manager it created, so the next call starts over, same as the kafka client got in #17816 and the redis client in #17828 * fix(microservices): keep a newer rmq connection when a stale one fails --------- Co-authored-by: Kamil Myśliwiec <mail@kamilmysliwiec.com>
4 of 12 tasks
This was referenced Sep 26, 2026
4 of 12 tasks
This was referenced Sep 28, 2026
Merged
kamilmysliwiec
pushed a commit
to nestjs/docs.nestjs.com
that referenced
this pull request
Sep 29, 2026
…3559) Listeners registered with the client's on() method stay registered until close() and are attached to each new socket (TCP, v12.1.1, nestjs/nest#17888) or pub/sub pair (Redis, v12.1, nestjs/nest#17828). Registrations are not deduplicated, so re-registering from within a callback accumulates them.
kamilmysliwiec
pushed a commit
that referenced
this pull request
Sep 29, 2026
the mqtt client drops the listeners registered through on() when the client that gave up reconnecting gets replaced. the next connect() only reattaches the listeners it kept from before the first connect, so a listener added after that never sees the events of the new client. this is the same defect #17888 fixed for the tcp client and #17828 fixed for the redis client keep the on() registrations until close() and reattach them to every client created later, including the listeners added after the initial connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
ClientRedis#connectcaches the connection promise and never clears it when the attempt fails:when
retryAttemptsis configured and the retries are exhausted, the end listener replaces the cached promise with a rejectedError: Connection lost. Trying to reconnect...promise, and every later connect() call replays that rejection instead of trying again, even once the server is reachable. Only close() clears it. The pub or sub client the failed attempt managed to create is also left behind without being closedprobed on untouched master with a real ioredis client against a closed port (127.0.0.1:59999, retryAttempts: 1):
the second connect() call made no new attempt (2 createClient calls in total) and rejected with the cached string
Issue Number: N/A
Steps to reproduce
npx vitest run packages/microservices/test/client/client-redis.spec.tson master, the three tests inconnect after a failed attempt(the last one uses a real ioredis client against a closed port, the other two stub it)What is the new behavior?
A rejected attempt resets
connectionPromiseto null and quits the pub/sub clients the attempt managed to create (Promise.allSettled, same shape as the kafka fix), so the next connect() starts over. The end and reconnect listeners keep the existing reconnect model after a drop untouched, the catch only runs when the attempt itself failsDoes this PR introduce a breaking change?
Other information
the three new tests fail on master (3 failed | 42 passed) and pass with the fix (45 passed), every microservices test file is green with the fix: 898 tests across 63 files. nats resets its promise in a catch and tcp resets when the socket closes, so this brings redis in line with them