From 887b2d2d44b1b718ad6641c1a8a32961ba7e572b Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Wed, 14 Oct 2026 08:36:00 -0700 Subject: [PATCH] Keep error replies nested in a server.call reply as elements 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 --- src/modules/lua/script_lua.c | 16 +++++++++++----- tests/modules/reply.c | 24 ++++++++++++++++++++++++ tests/unit/moduleapi/reply.tcl | 16 ++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/modules/lua/script_lua.c b/src/modules/lua/script_lua.c index 44af4f56b..e1d074c2c 100644 --- a/src/modules/lua/script_lua.c +++ b/src/modules/lua/script_lua.c @@ -934,8 +934,7 @@ static void simpleStringCallback(void *ctx, const char *str, size_t len) { } static void errorCallback(void *ctx, const char *msg, size_t len) { - VALKEYMODULE_NOT_USED(len); - + processCollectionElementBegin(ctx); callCtx *call_ctx = ctx; lua_State *lua = call_ctx->lua; if (!lua_checkstack(lua, 3)) { @@ -944,7 +943,11 @@ static void errorCallback(void *ctx, const char *msg, size_t len) { serverPanic("lua stack limit reach when parsing server.call reply"); } - if (errno != 0) { + /* errno is set when the command was rejected before it ran, in which case + * the error is the whole reply. Parsing an earlier element, such as an + * out of range double, can also leave errno set, so only trust it for a + * top-level error. */ + if (errno != 0 && call_ctx->in_collection < 0) { ValkeyModule_Log(call_ctx->module_ctx, "debug", "command returned an error: %s errno=%d", msg, errno); luaProcessReplyError(msg, lua); } else { @@ -953,7 +956,7 @@ static void errorCallback(void *ctx, const char *msg, size_t len) { * or "ERR DB index is out of range"). Re-add the '-' so * luaPushErrorBuff() goes through its "-CODE msg" branch and * preserves the code instead of double-prefixing with "ERR ". */ - char *msg_with_dash = lm_asprintf("-%s", msg); + char *msg_with_dash = lm_asprintf("-%.*s", (int)len, msg); luaPushErrorBuff(lua, msg_with_dash); ValkeyModule_Free(msg_with_dash); /* push a field indicate to ignore updating the stats on this error @@ -963,7 +966,10 @@ static void errorCallback(void *ctx, const char *msg, size_t len) { lua_settable(lua, -3); } - call_ctx->error = 1; + /* Only a top-level error reply raises. An error nested in an aggregate is + * just an element of it. */ + if (call_ctx->in_collection < 0) call_ctx->error = 1; + processCollectionElementEnd(ctx); } static void mapStartCallback(void *ctx, size_t len) { diff --git a/tests/modules/reply.c b/tests/modules/reply.c index dcb4a5ae2..e62fb7db4 100644 --- a/tests/modules/reply.c +++ b/tests/modules/reply.c @@ -3,6 +3,7 @@ */ #include "valkeymodule.h" +#include #include int rw_string(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { @@ -156,6 +157,27 @@ int rw_error(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { return ValkeyModule_ReplyWithError(ctx, "An error"); } +/* Replies with [err, [err], {k: err, k2: 1}, LDBL_MAX, err, 1]: errors nested in + * arrays and maps, and one after a RESP3 double that overflows when parsed + * (where long double is wider than double). */ +int rw_nested_error(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { + VALKEYMODULE_NOT_USED(argv); + if (argc != 1) return ValkeyModule_WrongArity(ctx); + + ValkeyModule_ReplyWithArray(ctx, 6); + ValkeyModule_ReplyWithError(ctx, "ERR first"); + ValkeyModule_ReplyWithArray(ctx, 1); + ValkeyModule_ReplyWithError(ctx, "ERR second"); + ValkeyModule_ReplyWithMap(ctx, 2); + ValkeyModule_ReplyWithCString(ctx, "k"); + ValkeyModule_ReplyWithError(ctx, "ERR third"); + ValkeyModule_ReplyWithCString(ctx, "k2"); + ValkeyModule_ReplyWithLongLong(ctx, 1); + ValkeyModule_ReplyWithLongDouble(ctx, LDBL_MAX); + ValkeyModule_ReplyWithError(ctx, "ERR fifth"); + return ValkeyModule_ReplyWithLongLong(ctx, 1); +} + int rw_error_format(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { if (argc != 3) return ValkeyModule_WrongArity(ctx); @@ -205,6 +227,8 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg return VALKEYMODULE_ERR; if (ValkeyModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != VALKEYMODULE_OK) return VALKEYMODULE_ERR; + if (ValkeyModule_CreateCommand(ctx,"rw.nested_error",rw_nested_error,"",0,0,0) != VALKEYMODULE_OK) + return VALKEYMODULE_ERR; if (ValkeyModule_CreateCommand(ctx,"rw.error_format",rw_error_format,"",0,0,0) != VALKEYMODULE_OK) return VALKEYMODULE_ERR; if (ValkeyModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != VALKEYMODULE_OK) diff --git a/tests/unit/moduleapi/reply.tcl b/tests/unit/moduleapi/reply.tcl index 3cf284de3..cf8af558a 100644 --- a/tests/unit/moduleapi/reply.tcl +++ b/tests/unit/moduleapi/reply.tcl @@ -146,6 +146,22 @@ start_server {tags {"modules"}} { r hello 2 } + test "Script server.call keeps error replies nested in aggregates as elements" { + set script { + local r = server.call('rw.nested_error') + return {r[1]['err'], r[2][1]['err'], r[3][1], r[3][2]['err'], r[3][3], r[3][4], r[5]['err']} + } + assert_equal {{ERR first} {ERR second} k {ERR third} k2 1 {ERR fifth}} [r eval $script 0] + + set script { + server.setresp(3) + local r = server.call('rw.nested_error') + local m = r[3]['map'] + return {r[1]['err'], r[2][1]['err'], m['k']['err'], m['k2'], r[5]['err']} + } + assert_equal {{ERR first} {ERR second} {ERR third} 1 {ERR fifth}} [r eval $script 0] + } + test "Unload the module - replywith" { assert_equal {OK} [r module unload replywith] }