# ERR Error running script (call to f_<sha>): @user_script: <line>: Stack overflow in Lua script

- **ID:** `redis/lua-script-stack-overflow`
- **Domain:** redis
- **Category:** runtime_error
- **Error Code:** `ERR`
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

Lua script exceeded the maximum call stack depth, typically due to deep recursion or excessive nested function calls.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| redis 7.0 | active | — | — |
| redis 7.2 | active | — | — |
| redis 6.2 | active | — | — |
| redis 5.0 | active | — | — |

## Workarounds

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** (85% success)
   ```
   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).** (70% success)
   ```
   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).
   ```

## Dead Ends

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