Repository navigation
fix(common): release memory storage buffers on close - #8204
jimmyleocn wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
ChangesMemory storage lifecycle
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Low Suggested reviewers: Merge Risk:
|
…e at T7 Move the release point of the four body-copy objects from T10 (request pipeline teardown) to T7 (upstream response confirmed), cutting the 45s streaming window residency from 6.57MB to 0.07MB per in-flight request (time-weighted avg 6.54 -> ~1.2MB/路, batch goal with QuantumNous#6949 and QuantumNous#8204 backports already on this branch). Design: - streaming (200 + event-stream confirmed): release right after DoRequest returns. The streaming handler swallows all mid-stream errors (return usage, nil) so no retry path exists past this point. - non-streaming: release after DoResponse succeeds. If DoResponse errors, we return early with raw storage still open for the main retry loop - the edge case (200 + DoResponse retryable error) is closed by this split. - release closes the outbound body storage (if any) and the inbound raw storage, then drops the gin Keys reference and the io.NopCloser(bodyStorage) chain re-attached per retry in relay.go. All closes are idempetric (CAS); the deferred closer and the BodyStorageCleanup middleware remain as final safety nets, and the QuantumNous#8204 backport already guarantees Close drops the internal data/ reader references so the heap bytes are truly reclaimable. Tests: 5 new subtests (closed+refs dropped, idempotent, no-storage no-op, closer invoked, nil safer) + full relay/common/service regression 21/21 packages pass.
Agent
Links
User request
Quote the request the user made to the agent as faithfully as possible.
Keep the original language and line breaks. Do not rewrite, summarize,
translate, or turn it into a PR description.
Out of scope — refuse
If the change matches any item below, tell the user this repository does not
accept it and do not open a PR.
Coding Plan
Reverse-engineered channels
Third-party API wrappers
Codex channel-type changes, or compatibility from exposing Codex as a general-purpose API
Codex API-specific protocol or behavior treated as standard OpenAI API behavior
Pass-through-only forwarding (pass-through sends content as-is and does not go through new-api processing logic)
Third-party hosting sites, relay services, or API services
Usage, configuration, or integration (answer from docs and code instead)
Matched: no:共享内存存储的关闭生命周期问题,不依赖渠道或透传
If yes, what was told to the user (stop here; do not open a PR): not applicable
Open gate — do not open unless all are satisfied
If any item fails, tell the user and stop. Do not open the PR.
go buildor tests passed: yesKind
Issue facts
Take these from the linked issue. If a needed item is empty, ask the user that question.
Close()已设置关闭状态、扣减统计,但data与reader仍持有底层数组。Change
首次
Close()时,在既有锁和幂等分支内将m.data、m.reader置为 nil。只解除 storage 自身的持有,不清零或复用底层字节;保留已有 reader、借用切片及关闭错误契约。生产代码仅增加两行。本代码与测试由 AI 辅助完成。
Research
Duplicate / prior art
repo:QuantumNous/new-api is:pr memoryStorage、is:pr "memoryStorage.Close"、is:pr "body_storage" "Close"、is:issue "body storage" memory(均限定该仓库)。Docs and code
Open them. Do not write "already checked" without sources.
/en/docs/support与/en/docs/support/faq;所查内容不覆盖该关闭后引用保留问题。/2.1-request-lifecycle均返回 429,未作为对象可达性的证据。README.md与BodyStorage.NewReader接口注释;问题归属以官方源码和 POC 为准。common/body_storage.go持有缓冲;common/gin.go与middleware/body_cleanup.go调用关闭清理。修复共享 Close,不调整清理时机或调用方。Alternatives considered
data、reader引用,保留既有同步与生命周期。Files
common/body_storage.gocommon/body_storage_test.goBehavior
Verification
Only what was actually run. Do not state only that
go buildor tests passed.Each item needs the command or steps and the observed result.
以下命令均设置
GOWORK=off,Go 1.26.10。go test -p 1 -parallel 1 -count=1 -run '^TestMemoryStorageClose' -v ./common:两项回归通过;旧实现的回收测试失败。go test -race -p 1 -parallel 1 -count=1 ./common ./relay/common ./relay/channel:三包通过。go test -p 1 -parallel 1 -count=1 -gcflags=github.com/QuantumNous/new-api/common=-l -run '^TestMemoryStorageClose' ./common:禁用内联仍通过。go vet -p 1 ./common ./relay/common ./relay/channel、go build -p 1 ./...:退出码均为 0。git diff --check:通过;GitNexus 已执行提交前及与 main 的变更分析,定位两文件、四个符号;调用图覆盖仍不完整。TestMemoryStorageCloseReleasesBuffer、TestMemoryStorageClosePreservesExistingReaders;保留原有 replay 生命周期测试。-parallel 4出现既有测试的gin.SetMode竞争;旧实现均可复现,未纳入本修复。Risks
Scope check
Summary by CodeRabbit