Skip to content

fix(fetch): cancel ReadableStream body after request stream capability probe#7515

Merged
jasonsaayman merged 1 commit intoaxios:v1.xfrom
penkzhou:fix/fetch-adapter-readablestream-leak
Mar 16, 2026
Merged

fix(fetch): cancel ReadableStream body after request stream capability probe#7515
jasonsaayman merged 1 commit intoaxios:v1.xfrom
penkzhou:fix/fetch-adapter-readablestream-leak

Conversation

@penkzhou
Copy link
Copy Markdown
Contributor

@penkzhou penkzhou commented Mar 16, 2026

Problem

The module-level capability probe in the fetch adapter (lib/adapters/fetch.js) creates a new ReadableStream() as a Request body to test for streaming support, but never cancels it. The Request constructor sets up an internal pull pipeline on the stream body; since the stream is never consumed or cancelled, the [[pullAlgorithm]] Promise remains pending indefinitely, causing an async resource leak.

How to reproduce

Run any test file that imports axios (with the default fetch adapter, which has been the default since v1.7) using Vitest with async leak detection:

vitest --detect-async-leaks

This reports 1 PROMISE leak per test file due to the uncancelled stream from the probe.

Root cause

In the factory() function, the supportsRequestStream probe creates a ReadableStream inline:

const hasContentType = new Request(platform.origin, {
  body: new ReadableStream(),  // ← never cancelled
  method: 'POST',
  // ...
}).headers.has('Content-Type');

The Request constructor enqueues an internal pull on the stream. Without cancellation, this pull promise persists for the lifetime of the process.

Fix

Extract the ReadableStream to a variable and call body.cancel() after the probe completes:

const body = new ReadableStream();

const hasContentType = new Request(platform.origin, {
  body,
  method: 'POST',
  // ...
}).headers.has('Content-Type');

body.cancel();

Impact

Affects every project using the axios fetch adapter (default since v1.7) under test tooling with async leak detection (Vitest --detect-async-leaks, Jest --detectOpenHandles, Node.js async_hooks). The fix is a single cancel() call with no behavioral change to the probe logic.

Test

Added a unit test that spies on ReadableStream.prototype.cancel during a fresh getFetch() call (via a unique env to bypass the seed cache) and asserts the cancel method is invoked.


Summary by cubic

Fixes an async resource leak in the fetch adapter by cancelling the ReadableStream used in the request-stream capability probe. Adds a unit test to verify the stream is cancelled.

Bug Fixes

  • Cancel the probe ReadableStream in lib/adapters/fetch.js after creating the Request.
  • Prevents a pending pull promise and async leaks seen in vitest --detect-async-leaks, Jest open handles, and Node async_hooks.
  • No behavior change to request-stream support detection.

Testing

  • Added a unit test that spies on ReadableStream.prototype.cancel during a fresh getFetch() run; asserts it is called.
  • No other tests changed.

Written for commit 69588e8. Summary will update on new commits.

…y probe

The module-level capability probe in the fetch adapter creates a
ReadableStream as a Request body to test for streaming support, but
never cancels it.  The Request constructor sets up an internal pull
pipeline on the stream; since the stream is never consumed or
cancelled, the [[pullAlgorithm]] Promise remains pending indefinitely,
causing an async resource leak detectable by Node.js async_hooks and
Vitest --detect-async-leaks.

Extract the ReadableStream to a variable and call body.cancel() after
the probe completes to properly tear down the stream's internal
pipeline.
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

@penkzhou
Copy link
Copy Markdown
Contributor Author

I encountered this leak during development. Since the source of the leak is within this specific library, I would like to have it fixed so that more people can benefit from the solution. cc @jasonsaayman @DigitalBrainJS .

@jasonsaayman jasonsaayman added priority::high A high priority issue commit::fix The PR is related to a bugfix labels Mar 16, 2026
Copy link
Copy Markdown
Member

@jasonsaayman jasonsaayman left a comment

Choose a reason for hiding this comment

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

LGTM 🔥

@jasonsaayman jasonsaayman merged commit 94e1543 into axios:v1.x Mar 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix priority::high A high priority issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants