Skip to content

fix(scripting): guard against stack overflow on deeply nested Lua reply#3547

Merged
git-hulk merged 4 commits into
apache:unstablefrom
git-hulk:claude/interesting-shirley-898da8
Jul 9, 2026
Merged

fix(scripting): guard against stack overflow on deeply nested Lua reply#3547
git-hulk merged 4 commits into
apache:unstablefrom
git-hulk:claude/interesting-shirley-898da8

Conversation

@git-hulk

@git-hulk git-hulk commented Jul 6, 2026

Copy link
Copy Markdown
Member

ReplyToRedisReply() converts a Lua return value into a RESP reply with
native C++ recursion that runs after lua_pcall() returns,
so Lua's own recursion guard does not apply. A deeply self-nested table
could recurse until the worker thread stack overflowed and crashed
the whole server (the Lua VM is shared per worker).

Add a lua_checkstack() guard so such a reply fails with "reached lua stack limit"
instead of crashing. Includes a Go regression test in the scripting suite.

Assistant-By Claude Fable 5

ReplyToRedisReply() converts a Lua return value into a RESP reply with native C++ recursion that runs after lua_pcall() returns, so Lua's own recursion guard does not apply. A deeply self-nested table could recurse until the worker thread stack overflowed and crashed the whole server (the Lua VM is shared per worker).

Add a lua_checkstack() guard so such a reply fails with "reached lua stack limit" instead of crashing. Includes a Go regression test in the scripting suite.

Assistant-By Claude Fable 5
@git-hulk git-hulk closed this Jul 6, 2026
@git-hulk git-hulk deleted the claude/interesting-shirley-898da8 branch July 6, 2026 13:22
@git-hulk git-hulk restored the claude/interesting-shirley-898da8 branch July 6, 2026 13:25
@git-hulk git-hulk reopened this Jul 6, 2026
@git-hulk git-hulk changed the title claude/interesting shirley 898da8 fix(scripting): guard against stack overflow on deeply nested Lua reply Jul 6, 2026
@git-hulk git-hulk marked this pull request as draft July 6, 2026 13:54
…l error

The previous lua_checkstack() guard was not enough to fix the crash:
it only fails once the Lua stack exceeds LUAI_MAXCSTACK (8000 slots),
which requires ~8000 native recursion frames of ReplyToRedisReply().
Worker threads may have small stacks (512KiB by default on macOS), so
the C stack still overflowed before the guard ever fired. And when the
guard did fire, the error string was wrapped by thousands of nested
array headers instead of being surfaced to the client as an error.

Convert the Lua reply via LuaTypeToRedisReply() which tracks the
nesting depth and returns StatusOr<std::string>: once the depth exceeds
100 levels, the error propagates up with the Lua stack kept balanced,
and ReplyToRedisReply() discards the partial reply to return a single
top-level "reached lua stack limit" error like Redis does.

The regression test now covers every recursive conversion site (array
element, map key, map value and set entry) and runs a correct script
after each error to confirm the Lua stack is left in a good state.

Assistant-By Claude Fable 5
@git-hulk git-hulk marked this pull request as ready for review July 7, 2026 10:21
@git-hulk git-hulk requested a review from PragmaTwice July 8, 2026 10:21
@git-hulk git-hulk enabled auto-merge (squash) July 9, 2026 01:54
@git-hulk git-hulk merged commit 567fcf6 into apache:unstable Jul 9, 2026
37 checks passed
@sonarqubecloud

sonarqubecloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 50%)

See analysis details on SonarQube Cloud

@git-hulk git-hulk deleted the claude/interesting-shirley-898da8 branch July 9, 2026 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants