Skip to content

fix(storage): handle early stream destruction in createReadStream - #9586

Open
thiyaguk09 wants to merge 9 commits into
googleapis:mainfrom
thiyaguk09:fix/7603-stream-unable-to-pipe
Open

thiyaguk09 wants to merge 9 commits into
googleapis:mainfrom
thiyaguk09:fix/7603-stream-unable-to-pipe

Conversation

@thiyaguk09

Copy link
Copy Markdown
Contributor

Description

Prevents an unhandled ERR_STREAM_UNABLE_TO_PIPE exception in File#createReadStream when the downstream stream is destroyed or closed before the GCS HTTP response arrives.

Impact

Eliminates process crashes caused by synchronous ERR_STREAM_UNABLE_TO_PIPE errors thrown inside the asynchronous response callback when a downstream consumer disconnects early, while ensuring the underlying HTTP response stream is properly cleaned up to prevent memory or socket leaks.

Changes

  • Check throughStream.destroyed in File#createReadStream's response handler before invoking pipeline(), destroying or draining rawResponseStream and returning early if the destination stream is already destroyed.
  • Wrap the pipeline() call in a try...catch block to catch ERR_STREAM_UNABLE_TO_PIPE, suppress the pipeline completion error handler, safely destroy or drain rawResponseStream, and swallow the error while rethrowing any other unexpected errors.

Testing

  • Added a unit test in handwritten/storage/test/file.ts verifying that rawResponseStream is destroyed and pipeline() is not called when throughStream is already destroyed before the response callback runs.
  • Added a unit test in handwritten/storage/test/file.ts verifying that an ERR_STREAM_UNABLE_TO_PIPE error thrown by pipeline() is caught and swallowed and that rawResponseStream is destroyed.

Fixes #7603

…ing up rawResponseStream in file.createReadStream
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Oct 9, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces robust error handling and resource cleanup when piping streams in File.ts, specifically addressing scenarios where the target stream is already destroyed or when an ERR_STREAM_UNABLE_TO_PIPE error is thrown. Corresponding unit tests have been added to verify these behaviors. The feedback suggests correcting a TypeScript type cast from RequestError to any (or a generic error type with a code property) to avoid potential compilation issues and semantic inaccuracy, as the error is a standard Node.js stream error.

Comment thread handwritten/storage/src/file.ts Outdated
@thiyaguk09
thiyaguk09 marked this pull request as ready for review October 9, 2026 07:33
@thiyaguk09
thiyaguk09 requested review from a team as code owners October 9, 2026 07:33
Comment thread handwritten/storage/src/file.ts
Comment thread handwritten/storage/src/file.ts Outdated
Comment thread handwritten/storage/test/file.ts Outdated
Comment thread handwritten/storage/src/file.ts
@thiyaguk09
thiyaguk09 requested a review from kalragauri October 20, 2026 09:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File#createReadStream() can throw ERR_STREAM_UNABLE_TO_PIPE if the consumer closes before the response stream is piped

2 participants