Skip to content

fix(common): release memory storage buffers on close - #8204

Open
jimmyleocn wants to merge 1 commit into
QuantumNous:mainfrom
jimmyleocn:fix/memory-storage-close-8203
Open

jimmyleocn wants to merge 1 commit into
QuantumNous:mainfrom
jimmyleocn:fix/memory-storage-close-8203

Conversation

@jimmyleocn

@jimmyleocn jimmyleocn commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Agent

  • Tool: Codex CLI
  • Tool version: 0.164.0
  • Model (full id): gpt-6.1-sol(当前会话报告的模型 ID)
  • Host (CLI / IDE / GitHub coding agent / other): CLI
  • Date (UTC): 2026-11-04

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.

  • Verbatim:

提交代码。然后根据pr模板草拟一下pr的文案给我看下。

  • Later constraints or corrections from the user (quote, or none):

嗯不扩大影响范围。

做下修复的问题的自检。

PR标题可以改成英文,再看下没问题就进行提交PR,然后关联之前创建的Issue

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.

  • Out of scope (including pass-through-only forwarding): no
  • Usage / configuration / integration (answered instead of opening): no
  • Required issue facts present without invention: yes
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • Open: yes:用户已要求提交 PR,并关联 [Bug] memoryStorage.Close 后仍保留请求体缓冲区引用 #8203
  • If no, what was told to the user (stop here): not applicable:提交条件满足

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other: not applicable

Issue facts

Take these from the linked issue. If a needed item is empty, ask the user that question.

  • Actual behavior: Close() 已设置关闭状态、扣减统计,但 data 与 reader 仍持有底层数组。
  • Impact: 关闭后的 storage 仍被持有时,延迟已不再使用的请求体缓冲回收。
  • Frequency: 条件触发,隔离 POC 可复现;生产频率与保留规模未统计。
  • Evidence that the problem is in new-api rather than the client or upstream: [Bug] memoryStorage.Close 后仍保留请求体缓冲区引用 #8203 的官方源码 POC;原实现 retained=true,仅解除两处引用的 overlay 对照 retained=false,无上游参与。
  • Applicable types and their fields (relay / billing / frontend / deployment; write "not applicable" otherwise): not applicable:内部 Go 存储生命周期,不涉及渠道请求、计费、前端或部署变更。

Change

首次 Close() 时,在既有锁和幂等分支内将 m.data、m.reader 置为 nil。只解除 storage 自身的持有,不清零或复用底层字节;保留已有 reader、借用切片及关闭错误契约。生产代码仅增加两行。

本代码与测试由 AI 辅助完成。

Research

Duplicate / prior art

Docs and code

Open them. Do not write "already checked" without sources.

  • https://docs.newapi.ai/ : 已打开 /en/docs/support 与 /en/docs/support/faq;所查内容不覆盖该关闭后引用保留问题。
  • https://deepwiki.com/QuantumNous/new-api : 参考 [Bug] memoryStorage.Close 后仍保留请求体缓冲区引用 #8203 已记录的 Request Lifecycle 检索;本次直连首页及 /2.1-request-lifecycle 均返回 429,未作为对象可达性的证据。
  • README / repo docs: README.md 与 BodyStorage.NewReader 接口注释;问题归属以官方源码和 POC 为准。
  • Code paths and what they imply for this change: common/body_storage.go 持有缓冲;common/gin.go 与 middleware/body_cleanup.go 调用关闭清理。修复共享 Close,不调整清理时机或调用方。

Alternatives considered

  • Option A: 同时解除 data、reader 引用,保留既有同步与生命周期。
  • Option B: 仅解除其中一处;两个对照都未通过缓冲区回收测试。
  • Why this approach: 两处引用都指向同一数组;两行即可修复,不扩大到请求取消或整体内存优化。

Files

Path Why
common/body_storage.go 首次关闭时解除两处缓冲引用
common/body_storage_test.go 验证回收、重复关闭与既有 reader/切片/关闭错误契约

Behavior

  • Before: 关闭对象仍可使不再使用的缓冲存活,即使统计已扣减。
  • After: 关闭对象不再持有缓冲;没有其他合法引用时缓冲可回收。
  • Explicit non-goals / leftover work: 不改磁盘存储、取消流程、计费或渠道行为;不声称解决生产 OOM 或固定百分比收益。

Verification

Only what was actually run. Do not state only that go build or tests passed.
Each item needs the command or steps and the observed result.

  • Commands and results:
    以下命令均设置 GOWORK=off,Go 1.26.10。
    • Windows:go test -p 1 -parallel 1 -count=1 -run '^TestMemoryStorageClose' -v ./common:两项回归通过;旧实现的回收测试失败。
    • Linux:go test -race -p 1 -parallel 1 -count=1 ./common ./relay/common ./relay/channel:三包通过。
    • Windows:go test -p 1 -parallel 1 -count=1 -gcflags=github.com/QuantumNous/new-api/common=-l -run '^TestMemoryStorageClose' ./common:禁用内联仍通过。
    • Windows:go vet -p 1 ./common ./relay/common ./relay/channel、go build -p 1 ./...:退出码均为 0。
    • git diff --check:通过;GitNexus 已执行提交前及与 main 的变更分析,定位两文件、四个符号;调用图覆盖仍不完整。
  • Manual steps and observed result: 未做生产手工测试。临时 overlay 自检:原实现、仅清除一处引用均回收失败;nil/空/单字节内容及并发 Close/Read/Seek/Bytes/NewReader 通过,Linux 并发用例通过 race 检查;辅助文件不纳入提交。
  • UI: screenshot or recording (or why none): not applicable:无界面变更。
  • Tests added or updated, or why none: 在现有测试文件新增 TestMemoryStorageCloseReleasesBuffer、TestMemoryStorageClosePreservesExistingReaders;保留原有 replay 生命周期测试。
  • Databases / providers / platforms exercised: Windows/amd64、Linux/amd64;不连接数据库或真实提供方。
  • Not verified: 全仓库测试、生产端到端、内存/RSS 降幅及 OOM 归因。并行回归并非全通过:Windows GOAWAY 重试偶发连接中断,Linux -parallel 4 出现既有测试的 gin.SetMode 竞争;旧实现均可复现,未纳入本修复。

Risks

  • Failure modes: 已有 reader 或借用切片仍可合法保留缓冲,Close 不保证立即降低 RSS。GitNexus 上游影响为 UNKNOWN(接口调用解析不完整),已补充源码检查与回归。
  • Billing / quota / auth impact: 无相关代码变更。
  • Follow-ups: 既有测试阻碍保持原样,不扩大本 PR 范围。

Scope check

  • Single focused change: yes:仅修复已关闭内存请求体存储的缓冲持有。
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no

Summary by CodeRabbit

  • Bug Fixes
    • Closing in-memory storage now releases its backing memory and updates memory usage statistics.
    • Existing readers and borrowed data remain usable after storage closes; new storage operations return a closed-storage error.

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

memoryStorage.Close now clears its stored data and reader references after a successful close. Tests check that the backing allocation can be collected and that existing readers and borrowed bytes remain usable.

Changes

Memory storage lifecycle

Layer / File(s) Summary
Clear storage references and verify close behavior
common/body_storage.go, common/body_storage_test.go
After a successful close, memoryStorage clears its data and reader references. Tests check allocation collection, memory statistics, existing reader and borrowed-byte access, and errors from operations on closed storage.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: calcium-ion

Merge Risk: Minimal · up to 921d2

The change releases closed storage buffers without clearing borrowed bytes or invalidating existing readers. No actionable merge-blocking risk is identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: memory storage buffers are released when the storage is closed. The raw summary says memoryStorage.Close now clears its in-memory byte slice and reader after the first…
Linked Issues check Passed Issue #8203 requires memoryStorage.Close() to release the storage-owned data and reader references while preserving close idempotency, ErrStorageClosed, existing readers, borrowed slices, and …
Out of Scope Changes check Passed The changes are limited to common/body_storage.go and its focused regression tests in common/body_storage_test.go. They implement the #8203 buffer-retention fix and verify its required lifecycle b…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check

Explanation

The title names the main change: memory storage buffers are released when the storage is closed. The raw summary says memoryStorage.Close now clears its in-memory byte slice and reader after the first successful close, and the new tests check that closing releases the backing allocation. The title follows the conventional commit format and is concise, specific, and free of file lists or vague terms. The title is 52 characters, within a normal length.

Full details: Linked Issues check

Explanation

Issue #8203 requires memoryStorage.Close() to release the storage-owned data and reader references while preserving close idempotency, ErrStorageClosed, existing readers, borrowed slices, and underlying bytes. The diff sets m.data and m.reader to nil inside the existing first-close lock and idempotency branch. The added tests cover buffer reclamation, repeated close, existing readers, borrowed slices, post-close errors, and unchanged size behavior. No change clears or reuses the underlying bytes.

Full details: Out of Scope Changes check

Explanation

The changes are limited to common/body_storage.go and its focused regression tests in common/body_storage_test.go. They implement the #8203 buffer-retention fix and verify its required lifecycle behavior. The diff does not change request cancellation, disk storage, billing, channels, frontend behavior, deployment, or pass-through forwarding.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the buffer’s place,
Then watches old bytes leave no trace.
The reader keeps its borrowed view,
While closed storage says what it should do.
Hop, hop—the tests confirm the case.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 76fc6a2f-b540-415c-8ab1-b68263d455fe
Commits

Reviewing files that changed from the base of the PR and between 7061447 and 921d28f.

Files selected for processing: 2
  • common/body_storage.go
  • common/body_storage_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

m15068160369-design added a commit to m15068160369-design/new-api that referenced this pull request Nov 18, 2026
…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.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] memoryStorage.Close 后仍保留请求体缓冲区引用

1 participant