# 运行脚本时出错（调用 f_<sha>）：@user_script: <line>：Lua 脚本堆栈溢出

- **ID:** `redis/lua-script-stack-overflow`
- **领域:** redis
- **类别:** runtime_error
- **错误码:** `ERR`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

Lua 脚本超过了最大调用堆栈深度，通常是由于深度递归或过多嵌套函数调用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| redis 7.0 | active | — | — |
| redis 7.2 | active | — | — |
| redis 6.2 | active | — | — |
| redis 5.0 | active | — | — |

## 解决方案

1. ```
   Refactor the Lua script to use iterative loops instead of recursion, e.g., replace recursive function with a while loop:
while condition do ... end
   ```
2. ```
   Reduce the depth of nested function calls by flattening logic or using tail-recursive calls if Lua version supports (Redis Lua 5.1 does not).
   ```

## 无效尝试

- **** — Redis does not expose a Lua stack size configuration; the limit is hardcoded. (95% 失败率)
- **** — The script execution mechanism is the same; the error is script logic, not caching. (50% 失败率)
- **** — Stack overflow is a call depth issue, not a memory allocation issue; more memory does not increase stack depth. (80% 失败率)
