Skip to content

valkey: add ERR_REDIS_SERVER_ERROR for server error replies - #39576

Closed
alii wants to merge 1 commit into
ali/valkey-resp-parser-fixesfrom
ali/valkey-server-error-code
Closed

alii wants to merge 1 commit into
ali/valkey-resp-parser-fixesfrom
ali/valkey-server-error-code

Conversation

@alii

@alii alii commented Aug 18, 2026

Copy link
Copy Markdown
Member

Breaking

err.code for a server error reply changes from ERR_REDIS_INVALID_RESPONSE to ERR_REDIS_SERVER_ERROR. The message text is unchanged. ERR_REDIS_INVALID_RESPONSE stays the code for a reply the client could not parse.

Three public repos match on the old code for server errors. HazelChat/hazel (effect-bun/Redis.ts) maps it to a typed RedisInvalidResponseError. stella/stella (redis-client.ts) branches on it. petarzarkov/dunx (redis/errors.ts) documents that Bun files WRONGTYPE and unknown-command replies under it. The first two will stop matching those replies.

This needs a yes or no from a maintainer.

The problem

A -ERR or ! reply is an answer from the server. Today it carries ERR_REDIS_INVALID_RESPONSE. That code also means the client failed to parse a reply. Users cannot tell the two apart by code.

What changed

ERR_REDIS_SERVER_ERROR is appended at the tail of ErrorCode.ts. The Rust mirror is generated from that file, so existing discriminants do not move.

A - or ! reply that rejects one command carries the new code. So does such a reply nested inside an array reply.

In subscriber mode a - or ! reply still fails the whole connection. The rejection now carries ERR_REDIS_SERVER_ERROR instead of ERR_REDIS_INVALID_RESPONSE.

Handshake failures keep their specific codes. A - reply to HELLO stays ERR_REDIS_AUTHENTICATION_FAILED. A - reply to the internal SELECT stays ERR_REDIS_INVALID_COMMAND.

The docs list the new code as any error reply the server sent, whether it rejects one command or, in subscriber mode, closes the connection.

Tests

The mock server tests in test/js/valkey/valkey-incremental-scan.test.ts assert the new code for a top level -ERR, a top level !, the torn ! frames, and a - or ! element nested in an array.

A new test subscribes a client through the mock server, then sends a command that the server answers with -ERR. A second in-flight command rejects with ERR_REDIS_SERVER_ERROR and the server text. Before this change the code was ERR_REDIS_INVALID_RESPONSE.

Not in this PR

The command that drew the error in subscriber mode is not settled today. Its promise pair is consumed before the connection fails. That is pre-existing and belongs with the fail path work in #39542.

Whether a server error should fail a subscriber connection at all is a separate question.

@alii

alii commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

@robobun adopt

@robobun

robobun commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closed as a duplicate of #39573. The two points from the comment below are on #39573 in 5d26717.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed this PR and didn't find any bugs. Because it changes the user-visible err.code for server error replies — a breaking change the description explicitly flags as needing a maintainer yes/no — a human should sign off on the API decision.

What was reviewed:

  • Verified ServerError is appended (enum discriminants and ErrorCode.ts ordinals unchanged) and mapped in valkey_error_to_js.
  • Checked all RESPValue::Error sinks: HELLO stays AUTHENTICATION_FAILED, SELECT stays INVALID_COMMAND, subscriber and per-command paths now SERVER_ERROR.
  • Grepped tests for remaining ERR_REDIS_INVALID_RESPONSE assertions on server errors — none; the survivors are genuine parse-failure cases.
Extended reasoning...

Overview

The PR introduces ERR_REDIS_SERVER_ERROR so callers can distinguish a Redis -/! error reply (server rejected the command) from a client-side parse failure, both of which currently surface as ERR_REDIS_INVALID_RESPONSE. It touches the RedisError enum, the JS error-code table, the RESP→JS conversion for RESPValue::Error, the subscriber-mode fail path in valkey.rs, docs, and the mock-server test file. The mechanical changes are small and correct: the new variant is appended to both the Rust enum and ErrorCode.ts, the match in valkey_error_to_js gains one arm, and two call sites swap InvalidResponse for ServerError.

Security risks

None. This is an error-code label change; no input handling, allocation, or control flow is altered.

Level of scrutiny

The implementation itself is low-risk and well-tested (top-level -, top-level !, torn !, nested-in-array, and a new subscriber-mode test). What raises the bar is that it is an intentional breaking change to public API surface: the PR description names three public repos that branch on the old code and will stop matching, and explicitly asks for a maintainer yes/no. That is an API-design call, not something an automated review should approve.

Other factors

I confirmed no other tests in test/js/valkey/ still assert ERR_REDIS_INVALID_RESPONSE for a server-error path — the remaining hits cover a malformed RESP3 _ frame and nesting-depth limits, which are genuine parse failures. Handshake error paths (HELLO → AUTHENTICATION_FAILED, internal SELECT → INVALID_COMMAND) are deliberately left unchanged and covered by existing tests. The pre-existing subscriber-mode promise-leak is called out and deferred to #39542, which is reasonable scoping.

@alii

alii commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

Duplicate of #39573, which robobun opened on the same base with the same change. Closing this one. Two things from here worth carrying over there: the docs bullet should say the connection-failing subscriber-mode rejection also carries the new code, and the subscriber-mode test observes the code on a second in-flight PING because the command that drew the -ERR never settles (a hang that #39542 covers).

@robobun

robobun commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Both points are on #39573 now, in 5d26717. The docs bullet there uses the wording from this PR (an error reply rejects one command or, in subscriber mode, closes the connection). The subscriber-mode test comment there now says that the command that drew the -ERR never settles (#39542), so the code is read from the second in-flight PING. The src changes of the two branches were already identical.

robobun added a commit that referenced this pull request Aug 19, 2026
…onnection

This wording was added in 5d26717 at the request on #39576 and was
lost when the branch was rebuilt on top of the reduced #39544.
alii added a commit that referenced this pull request Aug 19, 2026
### Problem
- A server error reply (`-WRONGTYPE ...` or a `!` blob error) rejects
with `err.code === "ERR_REDIS_INVALID_RESPONSE"`.
`src/runtime/valkey_jsc/protocol_jsc.rs:94` passes
`RedisError::InvalidResponse` for `RESPValue::Error`.
- The same code means a reply the client cannot parse (`_junk`, nesting
depth). A caller cannot tell them apart.
- The subscriber arm at `src/runtime/valkey_jsc/valkey.rs:1164` passes
`InvalidResponse` too.

### Fix
- `ERR_REDIS_SERVER_ERROR` goes at the tail of `ErrorCode.ts`, so no
discriminant moves. The new `RedisError::ServerError` maps to it.
- Both `RESPValue::Error` sites now pass `ServerError`: the top-level
rejection, an Error element nested in an array (the `EXEC` shape), and
the subscriber arm. The message text is unchanged.
- Breaking: `err.code` for a server error reply changes. Three public
repos compare against the old code (see Notes). The docs and the
`send()` JSDoc name the new code.
- Verified: `test/js/valkey/valkey-incremental-scan.test.ts`, 41 pass
here, 11 fail with the `src/` of main. Also
`reliability/resp-nesting-depth.test.ts` and a real redis 8.0.2.

### Background
- RESP is the Redis wire protocol. A `-` reply is a simple error, a `!`
reply is a blob error. Since #39544 both parse to `RESPValue::Error`.
- `resp_value_to_js_with_options` (`protocol_jsc.rs`) turns a reply into
a JS value. For `RESPValue::Error` it calls `valkey_error_to_js`, which
maps a `RedisError` variant to an `ERR_*` code.
- A subscriber is a client that sent `SUBSCRIBE`. On an error reply
`handle_response` (`valkey.rs`) calls `fail`. `fail` rejects every
pending command with the given variant and closes the connection.
- The build generates the C++ and Rust `ERR_*` tables from
`ErrorCode.ts` in file order.

<details><summary>Notes</summary>

- The 11 tests that fail without the `src/` change: the `-ERR` and `!`
frames in both send modes, the two nested element tests, the
subscriber-mode test, and the four torn blob error tests. They all
assert the new code. The subscriber-mode test also fails with only the
`valkey.rs` line removed.
- Public repos that compare against `ERR_REDIS_INVALID_RESPONSE` for a
server error: HazelChat/hazel, stella/stella, petarzarkov/dunx.
- Real redis 8.0.2: `LPUSH` on a string key rejects with
`ERR_REDIS_SERVER_ERROR` and the unchanged `WRONGTYPE` message. An error
inside an `EXEC` reply resolves as an Error element with the same code.
- `ERR_REDIS_INVALID_RESPONSE` is still used for a reply the client
could not parse (`_junk`, nesting depth, line too long, a bad type
byte). Those paths are not changed.
- Not changed: a subscriber still fails the whole connection on an error
reply, and the command that reply answers is left unsettled. #32858
changes that path. The subscriber test here reads the code from the
command still in flight behind it, which holds before and after #32858.
- Docs: the bullet in `docs/runtime/redis.mdx` says that the reply
rejects one command or, in subscriber mode, closes the connection.
#39576 asked for the subscriber part. It was lost when the branch was
rebuilt on the reduced #39544, and commit 4 restores it.
`packages/bun-types/redis.d.ts` names the code in the `send()` JSDoc.
- History: this PR was stacked on #39544 and at first carried the
decoder fix too. #39544 is merged as 24c0063. The branch is now rebased
onto main. The rebase did not change the diff. Commit 1 is @alii's
commit from the earlier version of #39544, with its test hunks redone on
top of the table-driven tests. Commits 2 to 4 were added during
adoption.
</details>

<!-- robobun:evidence:begin -->

---

**[review]** gate passed · iteration 0 · 7 files touched

<details><summary>fails on main (without fix)</summary>

```console
ASAN without fix: 11 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/valkey/valkey-incremental-scan.test.ts
bun test v1.4.0 (4c68990)

test/js/valkey/valkey-incremental-scan.test.ts:
(pass) Valkey reply decoding, frame sent whole > RESP3 null with trailing bytes (_junk) [45.87ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array (*-1) [78.52ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array nested in an array [62.81ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null bulk string ($-1) [62.40ms]
(pass) Valkey reply decoding, frame sent whole > RESP3 null (_) [62.22ms]
162 |         expect(typeof (outcome as { value: unknown }).value).toBe(typeof expected.value);
163 |       } else {
164 |         expect(outcome).toHaveProperty("error");
165 |         const { error } = outcome as { error: Error & { code: string } };
166 |         expect(error).toBeInstanceOf(Error);
167 |         expect(error.code).toBe(expected.rejects.code);
                                 ^
error: expect(received).toBe(expected)

Expected: "ERR_REDIS_SERVER_ERROR"
Received: "ERR_
... (truncated)

release without fix: all passed
bun test v1.4.0-canary.1 (4c6ff0c)

test/js/valkey/valkey-incremental-scan.test.ts:
(pass) Valkey reply decoding, frame sent whole > RESP3 null with trailing bytes (_junk) [2.45ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array (*-1) [3.60ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array nested in an array [3.31ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null bulk string ($-1) [3.38ms]
(pass) Valkey reply decoding, frame sent whole > RESP3 null (_) [3.56ms]
(pass) Valkey reply decoding, frame sent whole > big number above 2^53 [3.52ms]
(pass) Valkey reply decoding, frame sent whole > negative big number [3.64ms]
(pass) Valkey reply decoding, frame sent whole > big number above 2^64 [3.66ms]
(pass) Valkey reply decoding, frame sent whole > big number with a non-integer payload [3.76ms]
(pass) Valkey reply decoding, frame sent whole > simple error (-ERR) [3.87ms]
(pass) Valkey reply decoding, frame sent whole > blob error (!) [3.90ms]
(pass) Valkey reply decoding > error reply (-) to HELLO rejects queued commands with the server text [1.96ms]
(pass) Valkey reply decoding > error reply (!) to HELLO rejects queued 
... (truncated)
```

</details>

<details><summary>passes on PR (with fix)</summary>

```console
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/valkey/valkey-incremental-scan.test.ts
bun test v1.4.0 (4c68990)

test/js/valkey/valkey-incremental-scan.test.ts:
(pass) Valkey reply decoding, frame sent whole > RESP3 null with trailing bytes (_junk) [49.15ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array (*-1) [84.60ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null array nested in an array [67.49ms]
(pass) Valkey reply decoding, frame sent whole > RESP2 null bulk string ($-1) [66.94ms]
(pass) Valkey reply decoding, frame sent whole > RESP3 null (_) [66.75ms]
(pass) Valkey reply decoding, frame sent whole > big number above 2^53 [41.64ms]
(pass) Valkey reply decoding, frame sent whole > negative big number [43.01ms]
(pass) Valkey reply decoding, frame sent whole > big number above 2^64 [44.06ms]
(pass) Valkey reply decoding, frame sent whole > big number with a non-integer payload [44.02ms]
(pass) Valkey reply decoding, frame sent whole > simple error (-ERR) [43.74ms]
(pass) Valkey reply decoding, frame sent whole > blob error (!) [40.86ms]
... (truncated)

release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped) in 647ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[1/80] gen ErrorCode+*.h
[2/24] gen generated_host_exports.rs
generated_host_exports.rs: 92 exports (host=3, lazy=10, generic=79, rust=0); 241 extern-C blocks audited
[3/24] gen JS modules (bundle-modules)
Preprocess modules (7828ms)
Bundle modules (54ms)
Postprocesss modules (33ms)
Bundle Functions (734ms)
Generate Code (26ms)

[8.69s] Bundled "src/js" for production
  2628 kb
  198 internal modules
  13 native modules
  92 internal functions across 17 files
[3/9] cargo bun_bin → libbun_rust.a (--target x86_64-unknown-linux-gnu)

  nightly-2026-07-20-x86_64-unknown-linux-gnu unchanged - rustc 1.99.0-nightly (9f36de775 2026-07-19)

�[1m�[92m   Compiling�[0m bun_core v0.0.0 (/workspace/bun/src/bun_core)
�[1m�[92m   Compiling�[0m bun_errno v0.0.0 (/workspace/bun/src/errno)
�[1m�[92m   Compiling�[0m bun_ptr v0.0.0 (/workspace/bun/src/ptr)
�[1m�[92m   Compiling�[0m bun_boringssl_sys v0.0.0 (/workspace/bun/src/boringssl_sys)
�[1m�[92m   Compiling�[0m bun_safety v0.0.0 (/workspace/bun/src/safety)
�[1m�[92m   C
... (truncated)
```

</details>

<details><summary>diff hotspot</summary>

```
docs/runtime/redis.mdx                         |   1 +
 packages/bun-types/redis.d.ts                  |   2 +-
 src/jsc/bindings/ErrorCode.ts                  |   1 +
 src/runtime/valkey_jsc/protocol_jsc.rs         |   7 +-
 src/runtime/valkey_jsc/valkey.rs               |   2 +-
 src/valkey/valkey_protocol.rs                  |   2 +
 test/js/valkey/valkey-incremental-scan.test.ts | 132 +++++++++++++++++--------
 7 files changed, 97 insertions(+), 50 deletions(-)
```

</details>

**gate history** · 2 passed · 0 rejected · iteration 0

<details><summary>evidence per changed file</summary>

```
file                                            reads  edits  tests
docs/runtime/redis.mdx                              1      1      0
packages/bun-types/redis.d.ts                       0      0      0
src/jsc/bindings/ErrorCode.ts                       0      0      0
src/runtime/valkey_jsc/protocol_jsc.rs              0      0      0
src/runtime/valkey_jsc/valkey.rs                    0      0      0
src/valkey/valkey_protocol.rs                       0      0      0
test/js/valkey/valkey-incremental-scan.test.ts      0      0      0
```

</details>

<!-- robobun:evidence:end -->

---------

Co-authored-by: Alistair Smith <hi@alistair.sh>
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.

2 participants