Skip to content

fix: error the body stream when a FormData Blob part fails to read - #5836

Open
ethanstoner wants to merge 1 commit into
nodejs:mainfrom
ethanstoner:fix/formdata-blob-error-unhandled-rejection
Open

ethanstoner wants to merge 1 commit into
nodejs:mainfrom
ethanstoner:fix/formdata-blob-error-unhandled-rejection

Conversation

@ethanstoner

@ethanstoner ethanstoner commented Sep 17, 2026

Copy link
Copy Markdown

Fixes #5835.

If a FormData body contains a Blob whose stream fails partway through (for
example a file from fs.openAsBlob() that changed after it was opened), the
process crashes with an unhandled rejection. Anything reading the body never
gets an error back.

Since #4791, extractBody writes the multipart parts from an async function
that nothing awaits, with no try/catch. When a part's stream throws, the error
escapes as an unhandled rejection and the ReadableStream is never errored.
Before #4791, pull() returned iterator.next(), so the same failure errored
the stream.

This wraps that loop in a try/catch and calls controller.error(err) on
failure. response.text() now rejects with NotReadableError.

Testing:

  • New test/fetch/issue-5835.js: fails on main with an unhandled rejection,
    passes with the fix.
  • Checked by hand that a normal FormData upload still works, that fetch()
    uploading the broken Blob rejects with a TypeError caused by
    NotReadableError, and that cancelling the body partway through leaves no
    unhandled rejection.
  • test/fetch passes apart from tests that fail the same way on main.
    npm run lint is clean.

I used Claude Code to find the cause and write the fix and test. I reviewed the
change, ran the checks above, and can answer questions about it.

extractBody's async IIFE for FormData/string/BufferSource bodies (added
in nodejs#4791) ran `for await` over the action's async iterator with no
try/catch. When a FormData part is a Blob whose stream() throws (e.g. a
file-backed Blob from fs.openAsBlob() whose underlying file changed
after being read), the throw propagated out of the un-awaited IIFE and
became an unhandled promise rejection instead of erroring the body's
ReadableStream. This crashed the process instead of causing the fetch
or body read to reject, and regressed behavior present before nodejs#4791,
where pull() returned iterator.next() and a rejection there errored the
stream naturally.

Wrap the IIFE body in try/catch and call controller.error(err) on
failure so the stream errors and any pending read (e.g. response.text())
rejects instead.

Fixes nodejs#5835

Co-Authored-By: Claude <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.53%. Comparing base (bfea020) to head (3c9fd88).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5836   +/-   ##
=======================================
  Coverage   93.53%   93.53%           
=======================================
  Files         110      110           
  Lines       39773    39779    +6     
=======================================
+ Hits        37200    37206    +6     
  Misses       2573     2573           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

FormData body with an errored Blob part crashes the process with an unhandled rejection instead of erroring the body stream (regression in 7.20.0)

2 participants