Skip to content

fix(goods): handle errors in responseToReadable stream reader#1444

Open
daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu:fix/responseToReadable-error-handling-1443
Open

fix(goods): handle errors in responseToReadable stream reader#1444
daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu:fix/responseToReadable-error-handling-1443

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 27, 2026

Problem

When using fetch().pipe(), if the response body reader errors (e.g., due to network disconnection, aborted request, or corrupted stream), the async _read function in responseToReadable throws an unhandled promise rejection that can crash the Node.js process.

Root Cause

The _read method assigned in responseToReadable is an async function, but reader.read() and Buffer.from() calls are not wrapped in a try/catch. Since Node.js stream internals call _read without awaiting or catching the returned promise, any rejection becomes unhandled.

Fix

  • Wrap the body of rs._read in a try/catch block in responseToReadable (src/goods.ts)
  • On error, call rs.destroy(err) to properly propagate the error through the stream, allowing downstream consumers to handle it gracefully

Tests Added

Change Point Test
try/catch in _read with rs.destroy(err) responseToReadable propagates reader errors to stream — creates a mock reader that rejects, verifies the error is propagated via the stream's error event

Impact

Only affects the internal responseToReadable helper used by fetch().pipe(). Normal fetch operations are unaffected — this only changes behavior when the response stream encounters an error during reading.

Fixes #1443

@daguimu daguimu force-pushed the fix/responseToReadable-error-handling-1443 branch from deb84e4 to acfc842 Compare March 27, 2026 03:45
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.

[Bug]: Unhandled promise rejection in fetch().pipe() when response stream errors

1 participant