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] }