fix(fetch): cancel ReadableStream body after request stream capability probe#7515
Merged
jasonsaayman merged 1 commit intoaxios:v1.xfrom Mar 16, 2026
Merged
Conversation
…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.
Contributor
There was a problem hiding this comment.
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-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
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 . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The module-level capability probe in the fetch adapter (
lib/adapters/fetch.js) creates anew ReadableStream()as aRequestbody to test for streaming support, but never cancels it. TheRequestconstructor 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:
This reports 1
PROMISEleak per test file due to the uncancelled stream from the probe.Root cause
In the
factory()function, thesupportsRequestStreamprobe creates aReadableStreaminline:The
Requestconstructor enqueues an internal pull on the stream. Without cancellation, this pull promise persists for the lifetime of the process.Fix
Extract the
ReadableStreamto a variable and callbody.cancel()after the probe completes: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.jsasync_hooks). The fix is a singlecancel()call with no behavioral change to the probe logic.Test
Added a unit test that spies on
ReadableStream.prototype.cancelduring a freshgetFetch()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
ReadableStreamused in the request-stream capability probe. Adds a unit test to verify the stream is cancelled.Bug Fixes
ReadableStreaminlib/adapters/fetch.jsafter creating theRequest.vitest --detect-async-leaks, Jest open handles, and Nodeasync_hooks.Testing
ReadableStream.prototype.cancelduring a freshgetFetch()run; asserts it is called.Written for commit 69588e8. Summary will update on new commits.