# 检测到 LUA 脚本死锁：脚本 <sha> 阻塞了 <seconds> 秒

- **ID:** `redis/lua-script-deadlock-detected`
- **领域:** redis
- **类别:** runtime_error
- **错误码:** `ERM`
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

Lua 脚本进入无限循环或在资源上阻塞（例如，循环内的 Redis.call），超过了 lua-time-limit，触发死锁警告。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 6.0 | active | — | — |
| 7.0 | active | — | — |
| 7.2 | active | — | — |

## 解决方案

1. ```
   如果脚本未执行任何写入（使用 'SCRIPT DEBUG' 检查），运行 'SCRIPT KILL'。如果已写入，使用 'SHUTDOWN NOSAVE' 停止 Redis。
   ```
2. ```
   添加循环计数器或使用 'redis.setresp(3)' 限制迭代次数。示例：'local i = 0; while i < 1000 do redis.call("PING"); i = i + 1 end'。
   ```

## 无效尝试

- **** — Only works if script hasn't written data; otherwise, Redis forces shutdown. (40% 失败率)
- **** — Masks the problem; script may still hang indefinitely, blocking other operations. (70% 失败率)
- **** — Causes downtime and potential data loss; does not fix the script logic. (60% 失败率)
