Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/modules/lua/script_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions tests/modules/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "valkeymodule.h"
#include <float.h>
#include <math.h>

int rw_string(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/moduleapi/reply.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Loading