Skip to content

fix: handle stream read errors in responseToReadable#1449

Open
venkat22022202 wants to merge 1 commit intogoogle:mainfrom
venkat22022202:fix/fetch-pipe-unhandled-rejection
Open

fix: handle stream read errors in responseToReadable#1449
venkat22022202 wants to merge 1 commit intogoogle:mainfrom
venkat22022202:fix/fetch-pipe-unhandled-rejection

Conversation

@venkat22022202
Copy link
Copy Markdown

Fixes #1443

The responseToReadable function in src/goods.ts assigns an async function to rs._read without error handling. If reader.read() throws (e.g. network disconnection, aborted request, corrupted stream), the promise rejection is unhandled and crashes the Node.js process.

Fix

Wrapped the async _read body in try/catch. Errors are propagated to the stream via rs.destroy(err), allowing downstream consumers to handle them gracefully instead of crashing.

Usage demo

import { fetch } from 'zx'

const controller = new AbortController()
const result = fetch('https://speed.hetzner.de/100MB.bin', { signal: controller.signal })
const p = result.pipe`cat`
setTimeout(() => controller.abort(), 100)
// Before: UnhandledPromiseRejection crashes process
// After: Error propagated through stream, handled gracefully
  • Setup Set the latest Node.js LTS version.
  • Build: I've run npm build before committing and verified the bundle updates correctly.
  • Tests: I've run test and confirmed all tests succeed. Added tests to cover my changes if needed.
  • Docs: I've added or updated relevant documentation as needed.
  • Sign Commits have verified signatures and follow conventinal commits spec
  • CoC: My changes follow the project's coding guidelines and Code of Conduct.
  • Review: This PR represents original work and is not solely generated by AI tools.

…dled rejections

Fixes google#1443

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 1, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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