Skip to content

fix(openai): stop pinning large request bodies after the handler - #8875

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
zhx8702:fix/openai-body-copy-reduction
Nov 20, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
zhx8702:fix/openai-body-copy-reduction

Conversation

@zhx8702

@zhx8702 zhx8702 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

背景

一台 2 GiB 主机上,有用户持续发送带 4 张 base64 图片的 /v1/responses 请求,单个请求从 20MB 涨到 46.6MB,至少 3 个并发。网关被 OOM kill 后重启,客户端立即重试,又被杀,如此循环。

逐阶段测量存活堆时,除了改写阶段本身约 6 倍请求体的峰值,还发现了两类跟预算无关的问题:

  • 请求体在 handler 返回后仍然存活。 有几处字符串或指针会被异步写库或计费引用,导致整块请求体在 handler 返回、入口预算已经退还之后还活着。
  • 异步提示词审核做了多余的整段复制。 审核会复制完整正文,不在审核范围内的请求也会先复制再丢弃。切分和截短时,还会把整段 prompt 转成 []rune。

本 PR 只修这两类问题,不增加任何拒绝条件,也不加配置项。处理内存准入(503/413)的改动放在 #8876,那个 PR 依赖本 PR。

改动

请求体不再被长期引用(internal/service/)

  • newOpenAIRequestView:parseRawJSONView 是零拷贝读取,读出的 Model、PromptCacheKey、PreviousResponseID、ServiceTier、ReasoningEffort 与请求体共享底层内存。这些字段会进入 usage log 和异步任务,于是整块请求体被一起留住。现在改为 strings.Clone,每个字段只有几十字节。
  • RecordUsage:usageLog.UserAgent、IPAddress 原来取的是 &input.UserAgent、&input.IPAddress,会把整个 input 留到异步写库结束,input 里有 Result,Result 又引用请求体。现在改为取局部副本的地址。
  • Chat Completions / Anthropic Messages 转 Responses 时,json.Marshal(responsesReq) 之后把 responsesReq.Input 置为 nil。responsesReq 要一直活到流结束后的计费,但之后只读它的标量字段。这样流式期间的存活量从约 3 倍请求体降到约 2 倍。

异步提示词审核(internal/securityaudit/)

  • Coordinator 不再先 Clone() 再交给 PromptService.Enqueue,因为 Enqueue 自己还会再复制一次。接口注释写明了借用约定:调用期间只读借用,需要保留就自己复制。
  • PromptService.Enqueue 在复制正文之前,先检查配置是否生效、分组是否在范围内、是否有启用的端点,不满足就直接返回。日志事件和 dropped 计数跟 Enqueuer 原有的判断保持一致;Enqueuer 持久化前仍会再检查一次配置。
  • SplitRunes 按 UTF-8 边界切出不可变子串,不再把整段转成 []rune 再逐块复制。遇到无效 UTF-8 时保持原有行为。
  • TrimRunes 只遍历要保留的前缀。
  • RedactPreview 在正则没有命中时跳过 ReplaceAllString,因为它即使不做任何替换也会复制一份全文。替换顺序不变,仍然对完整输入做匹配。

行为变化

没有。对合法输入,输出与原实现一致;没有新的拒绝条件、配置项或 SQL。

效果

单请求存活堆:GOGC=1,数值是请求体的倍数,在 530c5e3a4 上测得。

路径 阶段 之前 之后
Responses(图片 / 长对话) handler 返回后 约 1(请求体仍被引用) 0
Chat Completions 流式期间 3.0 2.0
Messages 流式期间 3.0 2.0

异步审核每次操作新增的 Go 分配量:TotalAlloc 差值,统计的是累计分配,不是驻留内存。调用的是真实的 Coordinator.Check → PromptService.Enqueue → Enqueuer.Enqueue → ExtractPromptSnapshot 链路,仓库使用 fake 实现。

场景 之前 之后 减少
84KiB 文本 2,103,840 B 350,816 B 83%
1MiB 文本 22.18 MiB 3.08 MiB 86%
4 个并发 1MiB 文本(整批) 88.67 MiB 12.35 MiB 86%
不在审核分组内的 1MiB 请求 2,116,976 B 2,816 B 99.9%
单独切分 1MiB 文本(263 块) 5,277,440 B 9,344 B 99.8%

代价:脱敏正则大量命中的文本慢约 3%–4%(约 0.6ms),原因是命中时多做一次 MatchString。

测试

  • securityaudit/prompt_memory_optimization_test.go
    • 不在审核范围内的请求不复制正文。
    • 异步任务在 legacy 检查或 handler 修改输入之前,已经拿到自己的副本。
    • 8 个并发请求全部入队,没有新增丢弃。
    • TrimRunes、SplitRunes、RedactPreview 与原实现做对照:覆盖 Unicode、无效 UTF-8、优先级分隔符、完整输入的替换顺序,以及固定种子的语料。
  • service/openai_gateway_service_hotpath_test.go:在构造 view 之后改写请求体,读出的字段不受影响,证明它们不再与请求体共享内存。
  • go test ./internal/service/ ./internal/securityaudit/ ./internal/handler/ 通过,go vet 通过。

附注

Clone the few request-view strings that outlive the handler instead of aliasing the request body, drop the converted Responses input once it is marshaled, and avoid pointing usage logs at the forward input. Async prompt audit checks its scope before copying the body and no longer clones twice, splits and trims prompts without full []rune copies, and skips redaction rewrites that cannot match.

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@zhx8702

zhx8702 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 17, 2026
@zhx8702
zhx8702 marked this pull request as ready for review November 17, 2026 05:22
@Wei-Shaw
Wei-Shaw merged commit fb7d8fc into Wei-Shaw:main Nov 20, 2026
10 of 11 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants