fix(scripting): guard against stack overflow on deeply nested Lua reply#3547
Merged
git-hulk merged 4 commits intoJul 9, 2026
Merged
Conversation
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
…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
PragmaTwice
approved these changes
Jul 8, 2026
|
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


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