Skip to content

[Bug] macOS 上可用内存读数恒为 0~515MiB,导致 503 gateway_busy 误拦截,提示内容【系统或容器可用内存紧张,排队等待已结束,请稍后重试】 #640

Open

Description

@yangsong0925

版本号

3.5.0-beta.4

哪一家 / 哪个地区版本

Trae CN

同一时间,这一家的官方客户端能正常用吗?

能,官方客户端一切正常,只有走网关报错

你做了什么,看到什么

环境

  • 系统:macOS(Mac mini,物理内存 24 GiB)
  • 网关自身内存占用:约 158~190 MiB(拦截期间基本不变)
  • 客户端:Anthropic 格式 /v1/messages、OpenAI 格式 /v1/chat/completions

系统日志

⚠️ 网关容量拦截 (503 gateway_busy): 路径=/v1/messages 调用方=key:k1a1f91cb3a61 原因=系统或容器内存紧张(memory_pressure) 等待=10002ms 并发=0/1024 排队=1/2048 排队内存=1/256MiB 内存占用=186/24576MiB 可用=0MiB

问题描述

网关容量闸门在内存充足的情况下持续判定 memory_pressure,返回 503 gateway_busy,
请求在选路前即被拒绝,上游从未被调用。

从日志看,除「可用内存」一项外,所有闸门读数都远未到线:
0, 129, 165, 195, 327, 366, 376, 388, 390, 462, 515 MiB

全部被判定为紧张,且波动与网关自身占用(158~190 MiB)无关。

关键观察:总量读对,只有可用读错

机器实际内存状况(vm_stat,page size 16384):

项 页数 约合
Pages free 160270 2.45 GiB
Pages inactive 374911 5.72 GiB
Pages speculative 4744 74 MiB
可回收合计 539925 8.24 GiB
Pages wired down 173623 2.65 GiB
compressor 占用 422658 6.45 GiB

Pages throttled: 0,Pageouts: 1559(累计值很小),无 swap 增长,
macOS 自身并未判定内存压力。

反算物理内存总量:free + active + inactive + speculative + wired + compressor
≈ 23.4 GiB,与日志中的上限 24576MiB 吻合。

结论:程序能正确读取物理内存总量,但「可用内存」在 macOS 上取值异常。

推测原因

程序可能使用 Node 的 os.freemem() 或等价方式获取可用内存。
该值在 macOS 上仅统计 free 页,不包含可回收的 inactive / speculative 页;
macOS 也没有 Linux 的 /proc/meminfo 和 MemAvailable 可依赖。
在 macOS 把空闲内存大量用于文件缓存的常态下,该读数会长期接近 0。

复现

  1. macOS 上启动网关,正常使用一段时间(让系统把空闲内存用于文件缓存)
  2. 发送较大的 /v1/messages 请求(带系统提示与较多工具定义)
  3. 观察返回 503 gateway_busy,日志原因为 memory_pressure,而并发/排队/排队内存均未达线

实测一条链路连续 12 次被拦截,间隔稳定 12 秒,持续约 9.6 分钟,全部失败。

验证

执行 sudo purge 强制释放文件缓存后,同一请求立即恢复 200:

  • 最后一次拦截停止后,未再出现新的 gateway_busy
  • 同一 Key 的 /v1/messages 请求连续返回 200

说明触发条件取决于可用内存读数,而非真实内存不足。

期望行为

在系统内存充足时不应触发 memory_pressure 拦截。

建议修复

  1. macOS 上把 Pages inactive 与 Pages speculative(以及可回收的 purgeable)
    计入可用内存,而不是只看 free 页
  2. 或在设置中暴露该阈值,允许用户调低或关闭内存保护
  3. 建议在拦截日志中同时输出取值来源与原始读数,便于排查

临时规避

  • 复发时执行 sudo purge 后重试(仅缓解,缓存会重新增长)
  • 改用 /v1/chat/completions 链路(小请求更容易通过)
  • 精简请求体积(减少工具定义、压缩上下文)

其它补充

No response

Activity

  1. coderabbitai commented on Nov 20, 2026

    @coderabbitai
    ⚠️ Possible Duplicate Issue(s)

    🔗 Related PRs

    #627 - 修复 macOS 内存压力误判:改读内核压力等级 [open]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions