Skip to content

Redis 重启后限流请求持续返回 500 #6412

Closed

Description

@DUNHKpcc

Read This First (Do Not Remove This Section)

  • Docs: https://docs.newapi.ai/
  • Usage questions first: https://deepwiki.com/QuantumNous/new-api
  • Issues about forwarding behavior after enabling pass-through mode are not accepted; pass-through mode forwards requests directly, so please verify
    upstream behavior yourself.
  • Technical support requests such as coding plans or reverse-engineering channels are not accepted as issues.
  • Warning: issues with this template removed, section headings deleted, or content cleared may be closed directly. Repeated abusive submissions may result
    in a block.

Your current newapi version

v1.0.0-rc.21

Submission Checks

  • Non-duplicate issue: I have searched existing Issues and confirmed there are no
    similar issues.
  • Read this first: I have fully read the section above, reviewed the docs at https://docs.newapi.ai/ and the project README, and asked AI first,
    confirming this is not a usage, configuration, or integration question.
  • Template intact: I have not removed any guidance or section headings from this template and will complete it as requested.
  • Maintainer time: I understand that maintainers have limited time and issues that do not follow this template may be ignored or closed directly.

查重说明:搜索后发现 #4782 报告了相同问题,但该 issue 因未使用规定模板、缺少版本和 Submission Checks 而被机器人关闭,并被告知可以使用正确的 Bug Report 模板
重新提交。本 issue 是按该要求进行的规范重提。另有相关 PR #2856,但它已关闭且未合并。

Issue Description

启用 Redis 和模型请求限流后,如果 Redis 在 new-api 进程仍然运行期间发生重启、替换、故障转移或脚本缓存被清空,所有经过模型请求限流的 API 请求会持续返回 HTTP
500:

  rate_limit_check_failed
  rate limit failed: NOSCRIPT No matching script. Please use EVAL.

客户端可能把该 500 显示为通用的 “We're currently experiencing high demand”,但实际并不是上游高负载,也不是 pass-through、渠道配置或使用问题。

实际生产环境中观察到:

  • new-api、Redis、PostgreSQL、nginx 和宿主机资源均健康;
  • Redis 的启动时间晚于 new-api,Redis 重启后其 Lua script cache 被清空;
  • new-api 日志在检查窗口内连续出现 496 次 rate_limit_check_failed / NOSCRIPT;
  • 受影响接口包括 POST /v1/responses 和 POST /v1/messages;
  • 这些请求由 new-api 很快返回 500,并未正常进入上游处理;
  • 仅重启 new-api 后立即恢复:后续实际 relay 请求全部成功,未再出现 NOSCRIPT 或限流相关 500。

代码层面的原因位于 common/limiter/limiter.go:

  1. New() 通过全局 sync.Once 仅执行一次 ScriptLoad,并把 SHA 保存在进程内;
  2. Allow() 后续始终使用 EvalSha;
  3. Redis 重启后脚本缓存丢失,但 new-api 仍持有旧 SHA;
  4. 当前代码没有在 Redis 返回 NOSCRIPT 时重新加载脚本或回退到 EVAL,而是把错误直接返回给中间件;
  5. middleware/model-rate-limit.go 将该错误转换为 HTTP 500 rate_limit_check_failed,导致启用该限流路径的用户持续失败。

相关实现的核心逻辑如下:

limitSHA, err := r.ScriptLoad(ctx, rateLimitScript).Result()
// ...
result, err := rl.client.EvalSha(
    ctx,
    rl.limitScriptSHA,
    []string{key},
    config.Requested,
    config.Rate,
    config.Capacity,
).Int()

这是应用层缺少 Redis NOSCRIPT 恢复逻辑导致的问题,不是上游行为或使用配置问题。

建议修复方向:

  • EvalSha 返回 NOSCRIPT 时,线程安全地重新加载内嵌 Lua 脚本、更新 SHA,并且只重试一次;
  • 或使用 go-redis 提供的 Script.Run 模式,让客户端在 EVALSHA 遇到 NOSCRIPT 时自动回退到 EVAL;
  • 不应绕过或关闭限流;
  • 增加测试:首次请求成功后,在测试 Redis 中执行 SCRIPT FLUSH 模拟脚本缓存丢失,再次请求应自动恢复且不能返回 500;

Steps to Reproduce

以下步骤只能在隔离测试环境执行,不要在生产 Redis 上执行 SCRIPT FLUSH。

  1. 使用 v1.0.0-rc.21,配置并连接 Redis。

  2. 启用 ModelRequestRateLimitEnabled,并设置非零的请求次数和持续时间限制。

  3. 启动 Redis 和 new-api。

  4. 发送一次经过模型请求限流的请求,确认请求正常,并让 new-api 加载 Lua 脚本和缓存 SHA。

  5. 保持 new-api 进程运行,在测试环境重启 Redis;或者在专用测试 Redis 中执行 SCRIPT FLUSH。

  6. 再次发送相同类型的 API 请求。

  7. 观察 new-api 返回 HTTP 500,并记录:

    rate limit failed: NOSCRIPT No matching script. Please use EVAL.
    
  8. 重启 new-api 后再次请求,服务恢复,证明问题来自进程内旧 SHA 与 Redis 新脚本缓存不一致。

Expected Result

Redis 重启、故障转移或脚本缓存丢失后,new-api 应当自动识别 NOSCRIPT,安全地重新加载 Lua 脚本并重试当前限流操作一次。

用户请求不应持续返回 HTTP 500,也不应要求运维人员手工重启 new-api。自动恢复过程必须保持现有限流语义,不能临时绕过限流。

Related Screenshots

暂无截图。关键脱敏日志如下:

检查总请求数限制失败: rate limit failed: NOSCRIPT No matching script. Please use EVAL.
[ERR] ... | rate_limit_check_failed
[GIN] ... | relay | 500 | ... | POST /v1/responses
[GIN] ... | relay | 500 | ... | POST /v1/messages

相关记录:

Activity

  1. added
    bug
    on Jul 23, 2026
  2. new-api-reviewer-bot commented on Jul 23, 2026

    @new-api-reviewer-bot

    [Bot Review]

    感谢提交,格式是完整的,版本、提交确认、问题描述和复现步骤都已提供。

    我也看到你说明了相关的 #4782 和 #2856:#4782 目前不是开放 issue,#2856 是已关闭 PR,所以这里不按重复 issue 处理。

  3. yujianwudi commented on Sep 2, 2026

    @yujianwudi

    Follow-up evidence from a fresh v1.0.0-rc.30 incident

    This is additional evidence for the existing open issue #6412. The same failure was reproduced on an official NewAPI release / official Docker image in a self-hosted deployment. No third-party upstream error was involved.

    Environment

    • NewAPI: v1.0.0-rc.30, OCI revision 27ff6a8767e728f879d52770c273d4f73214a430
    • Deployment: official Docker image, Linux amd64
    • Redis: separate container, upgraded from Redis 7.4.9 to Redis 8.10.1
    • PostgreSQL and Redis were healthy; this issue only concerns the NewAPI Redis rate-limit script path.
    • Standard API endpoints affected: POST /v1/chat/completions, POST /v1/responses, and POST /v1/messages.

    Actual behavior

    After Redis restarted while the NewAPI process remained running, requests that entered the model request rate-limit middleware repeatedly returned HTTP 500:

    rate_limit_check_failed
    rate limit failed: NOSCRIPT No matching script. Please use EVAL.
    

    The error occurred before the request reached an upstream provider. A representative request returned in approximately 0.5 ms and had no corresponding consume/billing log. Requests that did not enter this rate-limit path, such as ordinary status or model-list requests, were not evidence of an upstream outage.

    Timeline and impact

    • Redis restarted at 2026-09-02 23:39:28 +08:00 and loaded the RDB successfully.

    • NewAPI was not restarted and kept running with the previously loaded script SHA.

    • Representative failure at 2026-09-02 23:51:26 +08:00:

      [GIN] ... | relay | 202609021551260463553538268d9d6y0qTpmke | 500 | ... | POST /v1/chat/completions
      [ERR] ... | ... | user <redacted> | rate_limit_check_failed
      检查总请求数限制失败: rate limit failed: NOSCRIPT No matching script. Please use EVAL.
      
    • From the Redis restart until 2026-09-03 00:01:04 +08:00, NewAPI recorded 5,873 rate-limit failures affecting 80 distinct authenticated users. New failures were still occurring immediately before the workaround.

    • Restarting only the NewAPI container at 2026-09-03 00:06:03 +08:00 immediately restored the service. Subsequent chat/completions, responses, and messages requests returned HTTP 200, and no further NOSCRIPT or rate_limit_check_failed entries were observed.

    Code-level cause

    In common/limiter/limiter.go:

    1. New() uses a global sync.Once and calls ScriptLoad only once per NewAPI process.
    2. Allow() always calls EvalSha using the SHA retained in process memory.
    3. Redis restart, failover, or SCRIPT FLUSH removes the server-side Lua script cache while the NewAPI process keeps the old SHA.
    4. The current code does not reload the script or fall back to EVAL after NOSCRIPT.
    5. middleware/model-rate-limit.go converts that internal error into HTTP 500 rate_limit_check_failed before calling c.Next().

    The relevant v1.0.0-rc.30 source is available at:

    Reproduction

    Use an isolated test Redis only; do not run SCRIPT FLUSH on production:

    1. Run NewAPI v1.0.0-rc.30 with Redis and enable model request rate limiting with a non-zero total request limit.
    2. Send one request through a model request rate-limit path so NewAPI loads the embedded Lua script.
    3. Keep NewAPI running and restart the test Redis, or execute SCRIPT FLUSH in the isolated Redis instance.
    4. Send another equivalent request.
    5. Observe HTTP 500 and NOSCRIPT No matching script. Please use EVAL.
    6. Restart NewAPI and repeat the request; it succeeds because the script is loaded again.

    Expected behavior

    When Redis loses the cached script, NewAPI should automatically reload the embedded Lua script or use a safe EVAL fallback, retry the current limiter operation once, and preserve the existing rate-limit semantics. A Redis restart or failover should not require a manual NewAPI restart and should not return an internal HTTP 500.

    This is an application-level recovery defect, not an upstream provider failure. Please consider adding a regression test that flushes the test Redis script cache after the first successful request and verifies automatic recovery.

  4. Hades-ming commented on Oct 19, 2026

    @Hades-ming

    最新版本我也遇到了这个问题 看到了一个 pr 但似乎还没有合并到主分支
    PR #6486:自动恢复 Redis Lua 脚本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions