This repository was archived by the owner on Oct 25, 2026. It is now read-only.
Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 leavesserver.callpermanently 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 makeserrorCallbackhandle 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.calltoValkeyModule_CallArgvwith 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 setscall_ctx->error = 1(:966) unconditionally, so a nested error both corrupts the stack layout and makesserver.callraise.The worst case is a nested error whose missing
lua_settableshifts a later store onto a non-table, e.g.[[err]]or a RESP3{k: err, k2: v}. Thatlua_settable(:851,:855) raises from inside the reply callback and longjmps out ofValkeyModule_CallArgv, skipping bothmoduleReleaseTempClient(module.c:7256) andinuse--(script_lua.c:1219).inuseis a function static, so every laterserver.callfrom any script hits the recursion check at:1154until restart.Two more assumptions in the same callback only hold for a top-level error:
"-%s"(:956) andlenis unused (:937). The handler'smsgis 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_nsetsERANGEon overflow (valkey_strtod.c:26), which a RESP3LDBL_MAXdouble hits whereverlong doubleis wider thandouble. A later nested error then goes throughluaProcessReplyErrorinstead.Base behavior with a module command replying each shape,
pcall(server.call, ...)followed by a freshserver.call('ping'):[err]ERR e1ERR e1[err, 7]ERR e1 :7ERR e1 :7[[err]]attempt to index a number value, thenpinggetsrecursive call detected{k: err, k2: 7}ERR e1 $2 k2 :7attempt to index a string value, then wedged~{err, 7}ERR e1 :7attempt to index a string value, then wedgedAfter 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, wherecallReplyToLuaTyperecursed 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
errorCallbacknow callsprocessCollectionElementBegin/End, setscall_ctx->erroronly whenin_collection < 0, formats with"-%.*s"andlen, and only takes theerrnobranch for a top-level error.Alternative considered
Catching the raise with a
lua_pcallaround the call, or resettinginuseon 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_errorintests/modules/reply.creplies[err, [err], {k: err, k2: 1}, LDBL_MAX, err, 1], and the new test intests/unit/moduleapi/reply.tclreads every error back from both a RESP2 and a RESP3 script. The existingreply.tcltests 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.creverted to the base:The
LDBL_MAXelement only overflows wherelong doubleis wider thandouble, so the errno part is only exercised on x86. On x86-64 Linux with just the errno condition reverted:Based on upstream/unstable
8f67ab136, so the base branch here isupstream-unstable-8f67ab136.This was generated by AI but verified, with love, by a human.