Skip to content

[Fix] Cancel safeFetch body reader when maxSize exceeded#523

Merged
github-actions[bot] merged 1 commit into
clawwork-ai:mainfrom
advancedresearcharray:fix/safefetch-stream-cancel-408
Jun 6, 2026
Merged

[Fix] Cancel safeFetch body reader when maxSize exceeded#523
github-actions[bot] merged 1 commit into
clawwork-ai:mainfrom
advancedresearcharray:fix/safefetch-stream-cancel-408

Conversation

@advancedresearcharray

@advancedresearcharray advancedresearcharray commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #408

Test plan

  • pnpm check passes
  • New unit tests cover reader cancel on oversize streams, single-chunk overflow, and abort signal

release-note

NONE

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Hi @advancedresearcharray,
Thanks for your pull request!
If the PR is ready, use the /auto-cc command to assign Reviewer to Review.
We will review it shortly.

Details

Instructions for interacting with me using comments are available here.
If you have questions or suggestions related to my behavior, please file an issue against the gh-ci-bot repository.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the robustness of the safeFetch utility by ensuring that the response body reader is explicitly cancelled when a stream exceeds the defined size limit. This prevents unnecessary buffering and ensures that network resources are released promptly upon hitting a memory limit, effectively mitigating potential memory exhaustion issues.

Highlights

  • Reader Cancellation: Added a call to reader.cancel() when the response body size exceeds the configured maxSize to ensure the underlying stream is terminated immediately.
  • Regression Testing: Introduced new unit tests to verify reader cancellation, handling of single-chunk overflows, and proper propagation of abort signals.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the safeFetch utility to cancel the stream reader when the response size exceeds maxSize, and adds comprehensive unit tests to verify this behavior. The reviewer suggests triggering the stream reader cancellation asynchronously without awaiting it, which prevents unnecessary delays in aborting the request and throwing the size-limit error.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

if (done) break;
total += value.byteLength;
if (total > maxSize) {
await reader.cancel().catch(() => undefined);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Awaiting reader.cancel() can unnecessarily delay throwing the error and aborting the request if the cancellation promise takes time to resolve. Since we want to fail fast and reject the promise immediately when maxSize is exceeded, we should trigger the cancellation asynchronously without awaiting it.

Suggested change
await reader.cancel().catch(() => undefined);
reader.cancel().catch(() => undefined);

Cancel the response stream and abort the fetch signal as soon as
incremental reads exceed maxSize so Chromium stops buffering the body.
Adds regression tests for reader cancel and single-chunk overflow.

Closes clawwork-ai#408

Signed-off-by: root <root@array-fleet-ops.array.local>
@advancedresearcharray advancedresearcharray force-pushed the fix/safefetch-stream-cancel-408 branch from 8fd77b5 to 437c599 Compare June 6, 2026 08:00
@samzong

samzong commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks @advancedresearcharray!

/approve
/lgtm

@github-actions github-actions Bot added approved Approved for merge lgtm Looks good to me labels Jun 6, 2026
@github-actions github-actions Bot merged commit 99e13f2 into clawwork-ai:main Jun 6, 2026
11 checks passed
advancedresearcharray pushed a commit to advancedresearcharray/ClawWork that referenced this pull request Jun 8, 2026
clawwork-ai#408)

The streaming maxSize enforcement and reader cancellation were merged in
clawwork-ai#487 and clawwork-ai#523, but issue clawwork-ai#408 remained open. Add a regression test that
verifies safeFetch stops reading an oversized body before draining all
chunks, preventing the memory DoS described in the issue.

Closes clawwork-ai#408

Signed-off-by: Array Agent <advancedresearcharray@array.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@advancedresearcharray advancedresearcharray deleted the fix/safefetch-stream-cancel-408 branch June 10, 2026 20:03
advancedresearcharray pushed a commit to advancedresearcharray/ClawWork that referenced this pull request Jun 13, 2026
Closes clawwork-ai#408

The streaming maxSize enforcement (clawwork-ai#487), reader cancellation (clawwork-ai#523), and
regression tests (clawwork-ai#534) are already on main. This commit closes the issue
that remained open after squash merges omitted the closing keyword.

Signed-off-by: advancedresearcharray <advancedresearcharray@github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
advancedresearcharray pushed a commit to advancedresearcharray/ClawWork that referenced this pull request Jun 13, 2026
Closes clawwork-ai#408

The streaming maxSize enforcement (clawwork-ai#487), reader cancellation (clawwork-ai#523), and
regression tests (clawwork-ai#534) are already on main. This commit closes the issue
that remained open after squash merges omitted the closing keyword.

Signed-off-by: advancedresearcharray <advancedresearcharray@github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
advancedresearcharray pushed a commit to advancedresearcharray/ClawWork that referenced this pull request Jun 13, 2026
Closes clawwork-ai#408

The streaming maxSize enforcement (clawwork-ai#487), reader cancellation (clawwork-ai#523), and
regression tests (clawwork-ai#534) are already on main. This commit closes the issue
that remained open after squash merges omitted the closing keyword.

Signed-off-by: advancedresearcharray <advancedresearcharray@github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
advancedresearcharray pushed a commit to advancedresearcharray/ClawWork that referenced this pull request Jun 13, 2026
Verified reader.cancel() + controller.abort() on streamed bytes exceeding
maxSize, IPv6 pinned hostname bracketing, and 25 regression tests passing.
Fix already landed on main via clawwork-ai#487, clawwork-ai#523, clawwork-ai#534; this commit closes clawwork-ai#408.

Closes clawwork-ai#408

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Approved for merge lgtm Looks good to me

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] safeFetch buffers entire response before enforcing maxSize — memory DoS

2 participants