Skip to content

fix(body): cancel stream reader when body size limit exceeded#1320

Merged
pi0 merged 1 commit into
mainfrom
fix/body-size-reader-cancel
Mar 14, 2026
Merged

fix(body): cancel stream reader when body size limit exceeded#1320
pi0 merged 1 commit into
mainfrom
fix/body-size-reader-cancel

Conversation

@productdevbook

@productdevbook productdevbook commented Mar 14, 2026

Copy link
Copy Markdown
Member

Summary

  • `isBodySizeWithin` cloned the request body and read it to check the size, but when the limit was exceeded, the reader was never cancelled
  • The uncancelled reader holds a reference to the cloned stream, preventing garbage collection for large payloads (memory leak)
  • Now calls `reader.cancel()` when the size exceeds the limit, allowing the runtime to release stream resources immediately

Test plan

  • All body tests pass (36/36)
  • All body-limit unit tests pass (4/4)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved resource cleanup when request bodies exceed size limits, ensuring proper stream termination to prevent potential resource leaks.

isBodySizeWithin cloned the request body to check its size but never
cancelled the reader when the limit was exceeded. The uncancelled reader
held a reference to the cloned stream, preventing garbage collection
for large payloads.

Now calls reader.cancel() when the size exceeds the limit, allowing
the runtime to release the stream resources immediately.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@productdevbook
productdevbook requested a review from pi0 as a code owner March 14, 2026 15:58
@coderabbitai

coderabbitai Bot commented Mar 14, 2026

Copy link
Copy Markdown

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: 1d9f70ba-8c66-4b19-bc5c-b3a250e4e417

📥 Commits

Reviewing files that changed from the base of the PR and between c28356e and 78412d7.

📒 Files selected for processing (1)
  • src/utils/body.ts

📝 Walkthrough

Walkthrough

The change adds a stream cancellation step to the isBodySizeWithin function in the body utilities module. When the accumulated body size exceeds the configured limit, the code now calls reader.cancel() before returning false to ensure proper cleanup of the underlying stream resource.

Changes

Cohort / File(s) Summary
Stream Cleanup on Overflow
src/utils/body.ts
Added reader.cancel() call in isBodySizeWithin when body size exceeds limit, ensuring the stream is properly aborted before signaling overflow.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A stream once ran without a care,
But now we cancel with a flair!
🐰 With cleanup calls so swift and bright,
Resources freed—overflow done right! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: calling reader.cancel() when body size exceeds the limit to prevent memory leaks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/body-size-reader-cancel
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Mar 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/h3@1320

commit: 755028c

@pi0
pi0 merged commit 21db03b into main Mar 14, 2026
7 checks passed
@pi0
pi0 deleted the fix/body-size-reader-cancel branch March 14, 2026 17:02
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.

2 participants