Repository navigation
fix(client): reconnect after a reply the decoder cannot parse - #3608
Conversation
failConnection(err) destroys the current socket with err, so the connection goes through the same error and reconnect handling as a socket error.
When the decoder threw (e.g. `Unknown RESP type`), the client reset the decoder and emitted `error`, but kept the connection. The command waiting for the broken reply stayed at the head of the queue, so every later reply resolved the previous command's promise until the process restarted. Fail the connection with the decoder error instead: commands waiting for a reply are rejected with it and the client reconnects, as on a socket error.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 9547118. Configure here.
| // the replies left in the stream can't be matched to their commands anymore | ||
| this.#queue.resetDecoder(); | ||
| this.emit('error', err); | ||
| socket.failConnection(err as Error); |
There was a problem hiding this comment.
Handshake hides the decoder error
Medium Severity
When the decoder throws after handshake replies in the same chunk already resolved, those promises settle before failConnection delivers the socket 'error'. #connect then treats the dead socket as SocketClosedUnexpectedlyError, and the decoder error is never emitted or passed to reconnectStrategy as cause.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 9547118. Configure here.
* fix(client): let RedisSocket fail its connection with an error failConnection(err) destroys the current socket with err, so the connection goes through the same error and reconnect handling as a socket error. * fix(client): reconnect after a reply the decoder cannot parse When the decoder threw (e.g. `Unknown RESP type`), the client reset the decoder and emitted `error`, but kept the connection. The command waiting for the broken reply stayed at the head of the queue, so every later reply resolved the previous command's promise until the process restarted. Fail the connection with the decoder error instead: commands waiting for a reply are rejected with it and the client reconnects, as on a socket error. * test(client): cover a reply the decoder cannot parse (cherry picked from commit 36be0ef)


Description
This pull request fixes the permanent reply desync after the RESP decoder throws.
When
decoder.write()threw (for exampleUnknown RESP type 68 "D"on a byte that is not a RESP type), the client reset the decoder and emittederrorbut kept the connection. The command waiting for the broken reply stayed at the head of the queue, so every later reply resolved the previous command's promise, and the connection never recovered.The client now fails the connection with the decoder error, through a new internal
RedisSocket#failConnectionthat destroys the socket with that error, as the socket timeout already does. From there it is the existing socket-error path:flushWaitingForReply, which also resets the decoder);disableOfflineQueueis set or the client is closing, as on a socket error;reconnectStrategyreceives the decoder error ascause, and pub/sub channels are resubscribed on reconnect.erroris still emitted once, with the same error. A decoder error during the handshake now fails that connection attempt and retries it. This matches ioredis, which treats a parser error as fatal for the connection (recoverFromFatalError: reject the in-flight commands, thendisconnect(true)).Fixes #3607
Checklist
npm testpass with this change (including linting)?The new test in
index.spec.tsuses a mock server that prepends one invalid byte to a reply. It fails on master (the first PING resolves with the second PING's reply) and passes here. I ran thelib/client,lib/RESPandlib/clusterspecs of@redis/client,test:typesandnpm run lint. The only failures were sentinelbefore allhooks, which also fail on master in my environment (podman cannot read the sentinel config), so I couldn't run the sentinel specs. Against Redis 7.4 behind a proxy that injects one byte, the in-flight GETs are rejected, the client reconnects, and every later GET returns its own value. On master every later GET resolved with another GET's reply.The commits also apply cleanly on
v6.3, and the two source commits apply cleanly onclient@5.12.1, in case a backport is wanted.Note
Medium Risk
Touches core connection and command-queue error handling on decode failure; behavior change is intentional and aligned with other fatal socket errors, with a focused integration test.
Overview
When the RESP decoder throws on corrupt or unknown reply data, the client now treats the connection as dead instead of staying connected with a desynced reply stream.
After
resetDecoder(),RedisClientcalls newRedisSocket#failConnection(err), which destroys the socket with that error so the existing socket-error path runs: in-flight commands reject, offline queue behavior and reconnect strategy apply as on a normal drop, and later commands get correctly matched replies on a fresh connection.Adds a regression test with a mock server that injects an invalid RESP type byte; it asserts the broken command rejects, a follow-up command succeeds, and a reconnect occurs.
Reviewed by Cursor Bugbot for commit 9547118. Bugbot is set up for automated code reviews on this repo. Configure here.