Skip to content

fix(client): reconnect after a reply the decoder cannot parse - #3608

Merged
nkaradzhov merged 3 commits into
redis:masterfrom
yan-almeida:fix/decoder-error-desync
Nov 11, 2026
Merged

nkaradzhov merged 3 commits into
redis:masterfrom
yan-almeida:fix/decoder-error-desync

Conversation

@yan-almeida

@yan-almeida yan-almeida commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

This pull request fixes the permanent reply desync after the RESP decoder throws.

When decoder.write() threw (for example Unknown RESP type 68 "D" on a byte that is not a 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, and the connection never recovered.

The client now fails the connection with the decoder error, through a new internal RedisSocket#failConnection that destroys the socket with that error, as the socket timeout already does. From there it is the existing socket-error path:

  • commands waiting for a reply are rejected with the decoder error (flushWaitingForReply, which also resets the decoder);
  • commands not written yet are sent after the reconnect, or rejected when disableOfflineQueue is set or the client is closing, as on a socket error;
  • reconnectStrategy receives the decoder error as cause, and pub/sub channels are resubscribed on reconnect.

error is 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, then disconnect(true)).

Fixes #3607


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

The new test in index.spec.ts uses 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 the lib/client, lib/RESP and lib/cluster specs of @redis/client, test:types and npm run lint. The only failures were sentinel before all hooks, 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 on client@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(), RedisClient calls new RedisSocket#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.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T16:53:27.895585Z 9547118 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9547118. Configure here.

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@nkaradzhov
nkaradzhov merged commit 36be0ef into redis:master Nov 11, 2026
15 checks passed
nkaradzhov pushed a commit that referenced this pull request Nov 11, 2026
* 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)
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.

A reply the decoder cannot parse leaves the connection open and shifts every later reply to the wrong command

2 participants