Skip to content

fix: treat non-cluster SELECT failures during handshake as fatal - #2260

Closed
helmipradita wants to merge 1 commit into
redis:mainfrom
helmipradita:fix/select-handshake-failure
Closed

helmipradita wants to merge 1 commit into
redis:mainfrom
helmipradita:fix/select-handshake-failure

Conversation

@helmipradita

Copy link
Copy Markdown
Contributor

Fixes #1213.

Summary

When configuring new Redis({ db: <invalid_or_out_of_range> }), the initial connection handshake sends SELECT <db> via getHandshakeCommands(). Previously, its error handler was:

errorHandler: (err) => self.silentEmit("error", err)

Because the error was swallowed without re-throwing, sendHandshake() treated the command as handled and proceeded with the ready check. The client transitioned to "ready" status while running commands against db 0, even though redis.options.db showed the configured target DB. Any subsequent read or write command was executed against the wrong database silently.

Fix

  • In lib/redis/event_handler.ts, replace the silent error swallowing with handleSelectError(self, err).
  • Non-cluster errors (such as ERR DB index is out of range or ERR invalid DB index) are re-thrown, allowing connectHandler to enter recoverFromFatalError(err), flush the queue, and trigger retryStrategy / disconnection.
  • Cluster mode compatibility is preserved: if the error contains select is not allowed in cluster mode, it continues to be emitted via silentEmit("error", err) without throwing.

Test

Added regression tests in test/functional/connection.ts across both RESP2 and RESP3:

  • Verifies that a failed SELECT during handshake causes connection failure (status === "end") and prevents "ready" from firing.
  • Verifies that SELECT is not allowed in cluster mode continues to be tolerated and allows the client to become "ready".

@PavelPashov

Copy link
Copy Markdown
Contributor

Thanks for the PR. Unfortunately this is a breaking change, so we can't take it.

Today, if the connect-time SELECT fails, ioredis emits an error event, becomes ready, and keeps working on db 0. With this change the same connection fails instead: queued commands are rejected, and with the default retryStrategy the client reconnects forever. Any app passing a db that its server or proxy rejects (apart from the one cluster error message this PR exempts) works today but would stop working after upgrading.

The unhandled rejection reported in #1213 is already fixed: the error now goes to the error event. If you want fail-fast behavior, disconnect from your error listener:

redis.on("error", (err) => {
  if (err.command?.name === "select") redis.disconnect();
});

@helmipradita

Copy link
Copy Markdown
Contributor Author

Unfortunately this is a breaking change, so we can't take it.
Today, if the connect-time SELECT fails, ioredis emits an error event, becomes ready, and keeps working on db 0. With this change the same connection fails instead...

Ah, totally fair point @PavelPashov! Apologies, I was so focused on the db fallback issue itself that I overlooked the backward-compat implications for existing proxy/custom setups and the retry loop side effect.

Makes complete sense why keeping it non-fatal is necessary here, and thanks for pointing out the fail-fast workaround via the error listener. Really appreciate the review and explanation!

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.

Invalid db option causes ReplyError: ERR invalid DB index

2 participants