Skip to content

fix: close response bodies on non-OK responses - #5817

Open
shubolab wants to merge 1 commit into
QuantumNous:mainfrom
shubolab:fix-close-response-bodies
Open

fix: close response bodies on non-OK responses#5817
shubolab wants to merge 1 commit into
QuantumNous:mainfrom
shubolab:fix-close-response-bodies

Conversation

@shubolab

@shubolab shubolab commented Jun 30, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 本 PR 为 AI 辅助完成;以下描述已按实际代码逻辑整理。

📝 变更描述 / Description

部分 HTTP 请求路径在 client.Do(req) 成功后,会先判断上游响应状态码是否为 200,再延后执行 defer resp.Body.Close()。如果上游可达但返回 401、429、500 等非 200 响应,函数会直接返回错误,导致该响应体没有及时关闭。

本 PR 将这些路径的 defer resp.Body.Close() 移到确认 client.Do 成功之后,保证后续所有错误返回分支都会关闭 response body。

覆盖的路径包括:

  • 渠道余额查询辅助函数的非 200 响应分支。
  • Suno 任务轮询的非 200 响应分支。
  • 拉取渠道模型列表的非 200 响应分支。

同时补充了 Suno 轮询的回归测试:构造一个返回 500 的 fake response body,断言函数返回错误时仍会调用 Close()

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

GOCACHE=/tmp/go-build-cache GOMODCACHE=/tmp/go-mod-cache go test ./service -run TestUpdateSunoTasksClosesNonOKResponseBody
ok  	github.com/QuantumNous/new-api/service	0.104s

GOCACHE=/tmp/go-build-cache GOMODCACHE=/tmp/go-mod-cache go test ./service ./controller
ok  	github.com/QuantumNous/new-api/service	0.744s
ok  	github.com/QuantumNous/new-api/controller	(cached)

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when fetching models and polling tasks by handling HTTP responses more consistently.
    • Fixed response handling so failed requests are cleaned up properly, reducing the risk of hangs or resource issues.
    • Added coverage for error-response handling to help prevent regressions.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ef5c3b57-6e35-4a21-b763-ffe294797e79

📥 Commits

Reviewing files that changed from the base of the PR and between 12fc010 and 5839d84.

📒 Files selected for processing (4)
  • controller/channel-billing.go
  • controller/channel.go
  • service/task_polling.go
  • service/task_polling_test.go

Walkthrough

Three functions (GetResponseBody, FetchModels, updateSunoTasks) are fixed to defer resp.Body.Close() immediately after a successful client.Do call, before any status-code check. A test helper with close-tracking and a new test verify the Suno polling path closes the body on non-200 responses.

HTTP Response Body Leak Fix

Layer / File(s) Summary
Defer body close before status check
controller/channel-billing.go, controller/channel.go, service/task_polling.go
GetResponseBody gains named returns and a deferred close that propagates close errors; FetchModels and updateSunoTasks relocate defer resp.Body.Close() to immediately after client.Do succeeds.
Test infrastructure and non-OK body-close test
service/task_polling_test.go
Adds statusCode/body fields to the test adaptor, a closeTrackingReadCloser helper, BaseURL seeding, and TestUpdateSunoTasksClosesNonOKResponseBody asserting the body is closed when updateSunoTasks returns an error.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops past leaking pipes,
Closing bodies, fixing gripes.
defer runs before the check,
No open fds left to wreck.
🐇 Connections freed, all tidy now!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue #5816 is not fully fixed because updateSunoTasks still returns early on non-200 without closing resp.Body. Move defer resp.Body.Close() to immediately after every successful client.Do in updateSunoTasks, then verify the non-OK response test passes.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: closing response bodies on non-OK HTTP responses.
Out of Scope Changes check ✅ Passed All changes are directly related to the response-body closing fix and its regression test.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

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.

非 200 响应分支未关闭 HTTP response body

1 participant