Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.

Keep error replies nested in a server.call reply as elements - #94

Closed
madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-lua-nested-error-reply
Closed

madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-lua-nested-error-reply

Conversation

@madolson

Copy link
Copy Markdown
Owner

When a script server.calls a command whose reply has an error nested inside an array, map or set, the Lua engine treats that error as if it were the whole reply. Depending on the shape, the script gets the wrong table, raises on a reply that is not an error, or raises from inside the reply callback, which leaves server.call permanently broken for every script until restart. In practice this needs a module command, and it affects 9.1.0 through 9.1.2 and 9.2.0-rc1. The fix makes errorCallback handle a nested error like any other element and only raise for a top-level error.

AI generated details

Problem

#3122 (0ec1733, backported to 9.1 as dcf8191e8 and first released in 9.1.0-rc2) moved server.call to ValkeyModule_CallArgv with streaming reply handlers. Every value callback does the collection bookkeeping (processCollectionElementBegin/End, script_lua.c:822-858): push the array index, then store the value into the enclosing table. errorCallback (script_lua.c:936-967) does neither, and sets call_ctx->error = 1 (:966) unconditionally, so a nested error both corrupts the stack layout and makes server.call raise.

The worst case is a nested error whose missing lua_settable shifts a later store onto a non-table, e.g. [[err]] or a RESP3 {k: err, k2: v}. That lua_settable (:851, :855) raises from inside the reply callback and longjmps out of ValkeyModule_CallArgv, skipping both moduleReleaseTempClient (module.c:7256) and inuse-- (script_lua.c:1219). inuse is a function static, so every later server.call from any script hits the recursion check at :1154 until restart.

Two more assumptions in the same callback only hold for a top-level error:

  • The message is formatted with "-%s" (:956) and len is unused (:937). The handler's msg is not terminated at the element, so a nested error's text runs on into the rest of the reply.
  • errno != 0 (:947) is how the callback detects a command rejected before it ran. Parsing an earlier element can also set it: valkey_strtod_n sets ERANGE on overflow (valkey_strtod.c:26), which a RESP3 LDBL_MAX double hits wherever long double is wider than double. A later nested error then goes through luaProcessReplyError instead.

Base behavior with a module command replying each shape, pcall(server.call, ...) followed by a fresh server.call('ping'):

Reply RESP2 script RESP3 script
[err] raises ERR e1 raises ERR e1
[err, 7] raises ERR e1 :7 raises ERR e1 :7
[[err]] attempt to index a number value, then ping gets recursive call detected same
{k: err, k2: 7} raises ERR e1 $2 k2 :7 attempt to index a string value, then wedged
~{err, 7} raises ERR e1 :7 attempt to index a string value, then wedged

After the fix every shape returns a table with {err="ERR e1"} in the element's place and nothing raises. That is the layout from before #3122, where callReplyToLuaType recursed into each element and only a top-level error reply raised. The patch cherry-picks cleanly onto 9.1.2.

No core command can trigger this from a script. The only core command that nests errors in its reply is EXEC, and it is NOSCRIPT.

Fix

errorCallback now calls processCollectionElementBegin/End, sets call_ctx->error only when in_collection < 0, formats with "-%.*s" and len, and only takes the errno branch for a top-level error.

Alternative considered

Catching the raise with a lua_pcall around the call, or resetting inuse on the way out, would stop the wedge but still return the wrong table and raise on non-error replies. The bookkeeping is the actual bug.

Testing

rw.nested_error in tests/modules/reply.c replies [err, [err], {k: err, k2: 1}, LDBL_MAX, err, 1], and the new test in tests/unit/moduleapi/reply.tcl reads every error back from both a RESP2 and a RESP3 script. The existing reply.tcl tests only call module commands directly from the client, and the scripting tests only see top-level errors, which is why this was not caught.

With script_lua.c reverted to the base:

[exception]: Executing test client: ERR attempt to index a number value script: on @user_script:2..
 in valkey::__dispatch__ at tests/support/valkey.tcl:116
 in r at tests/test_helper.tcl:196
 in r at tests/unit/moduleapi/reply.tcl:154

The LDBL_MAX element only overflows where long double is wider than double, so the errno part is only exercised on x86. On x86-64 Linux with just the errno condition reverted:

Expected '{ERR first} {ERR second} {ERR third} 1 {ERR fifth}' to be equal to '{ERR first} {ERR second} {ERR third} 1 {ERR fifth

Based on upstream/unstable 8f67ab136, so the base branch here is upstream-unstable-8f67ab136.

This was generated by AI but verified, with love, by a human.

The ValkeyModule_CallArgv reply handlers in the Lua engine treat every
error reply as if it were the whole reply. errorCallback skips the
collection element bookkeeping that every other callback does and sets
the raise flag even for nested errors. So when a command (in practice a
module command) returns an aggregate containing an error, the error
table is left on the Lua stack instead of being stored in the enclosing
table, later elements are stored into the wrong table, and the script
either raises on a reply that is not an error or hits a lua_settable on
a non-table value inside the reply callback. That raise longjmps out of
ValkeyModule_CallArgv and skips the inuse-- in luaServerGenericCommand,
so every later server.call from any script fails with "recursive call
detected" until restart.

Do the element bookkeeping in errorCallback and only raise for a
top-level error. Two more things in the same callback only hold for a
top-level error and are fixed with it: the message is formatted with
"%s" and runs on into the rest of the reply, so honor its length; and
the errno check that detects a rejected command is also tripped by an
earlier element, since parsing an out of range RESP3 double sets ERANGE,
so only trust errno for a top-level error.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson added the promoted Change has been promoted upstream label Oct 25, 2026
@madolson madolson closed this Oct 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

promoted Change has been promoted upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant