Repository navigation
Description
Versions: ioredis 6.1.0 (main at aa7fb9a), Node.js 26.13.1, Redis 8.12.0, standalone
What happens
When Redis rejects a SELECT (for example an index above the server's databases setting), ioredis still records the rejected db as the client's current db. The connection itself stays on the db it was on. Nothing goes wrong until the next reconnect. Then the handshake selects the recorded db, Redis rejects it again, and the new connection is left on db 0. From then on, commands run against db 0 instead of the db the client was configured with.
So a single rejected select() call, followed by an ordinary network blip, silently moves all later reads and writes to db 0.
const redis = new Redis({ db: 2 });
redis.on("error", (err) => console.log("error:", err.message));
await redis.set("before", "x"); // db 2
await redis.select(99).catch((err) => console.log(err.message));
// ERR DB index is out of range
redis.condition.select; // 99, but the connection is still on db 2
redis.stream.destroy(); // any reconnect
await new Promise((resolve) => redis.once("ready", resolve));
// error: ERR DB index is out of range (the handshake SELECT 99)
await redis.set("after", "y"); // lands in db 0, not db 2Output of KEYS * afterwards: db 2 holds only before, db 0 holds after. CLIENT INFO confirms db=2 before the reconnect and db=0 after it. The same thing happens on every later reconnect, each time with an error event.
This only needs an app that calls select() with an index the server can reject (for example a db number taken from config or per tenant) and then reconnects.
Cause
Redis#sendCommand sets this.condition.select (and emits select) as soon as the SELECT is sent, before the reply comes back, and nothing resets it when the reply is an error. Reconnects and the offline queue then use that value.
Proposed fix
Keep updating condition.select when the command is sent, so commands issued right after select() keep recording the new db. But if Redis answers the SELECT with an error reply, set condition.select back to the previous db, as long as no later SELECT has changed it in the meantime. The restore only happens for server error replies (ReplyError). Other rejections, such as commandTimeout or a closed connection, keep the current behaviour, because there the SELECT may still have run.
This does not change what happens when the connect-time SELECT from the db option fails (the error event plus falling back to db 0, kept as discussed in #2260). It only stops a rejected select() call from being replayed on later reconnects.
I have this working locally (about 15 lines in lib/Redis.ts) with a functional test in test/functional/select.ts. The test fails on main and passes with the fix. Would you be OK with a PR along these lines?
Activity
PavelPashov commentedon Nov 24, 2026
@Johannes7549 Thanks for the detailed report! I reproduced it on 6.1.0 with both RESP2 and RESP3, and the proposed fix (restore the previous db on a
ReplyError, unless a laterSELECThas changed it) is the right approach. Please go ahead with the PR, together with a small functional test.fix: keep the current db when Redis rejects a SELECT
fix: keep the current db when Redis rejects a SELECT (redis#2308)
chore(release): 6.1.1 [skip ci]
github-actions commentedon Dec 2, 2026
🎉 This issue has been resolved in version 6.1.1 🎉
The release is available on:
v6.1.1Your semantic-release bot 📦🚀