Skip to content

Commit 60478b6

Browse files
committed
fix(storage): handle upload errors before pipeline setup
createWriteStream starts the simple or resumable upload before installing the pipeline that normally owns fileWriteStream errors. If startup fails synchronously, fileWriteStream and the public write stream are destroyed before pipeline() runs. Node 24 and newer then throw ERR_STREAM_UNABLE_TO_PIPE while attaching the already-invalid streams, and the original upload error can escape uncaught. Install a temporary error listener before upload startup and forward any early failure through the existing pipeline callback. If startup destroyed the source, destination, or public stream, tear down the remaining stream and skip pipeline setup. Remove the temporary listener once the normal pipeline is ready to take ownership. This restores the existing createWriteStream error tests on Node 24 and 26 without changing successful upload behavior.
1 parent b97be01 commit 60478b6

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

handwritten/storage/src/file.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,12 +2192,28 @@ class File extends ServiceObject<File, FileMetadata> {
21922192
});
21932193

21942194
writeStream.once('writing', () => {
2195+
const onPrePipelineError = (error: Error) => {
2196+
pipelineCallback(error);
2197+
};
2198+
fileWriteStream.once('error', onPrePipelineError);
2199+
21952200
if (options.resumable === false) {
21962201
this.startSimpleUpload_(fileWriteStream, options);
21972202
} else {
21982203
this.startResumableUpload_(fileWriteStream, options);
21992204
}
22002205

2206+
if (
2207+
fileWriteStream.destroyed ||
2208+
writeStream.destroyed ||
2209+
emitStream.destroyed
2210+
) {
2211+
emitStream.destroy();
2212+
return;
2213+
}
2214+
2215+
fileWriteStream.removeListener('error', onPrePipelineError);
2216+
22012217
// remove temporary noop listener as we now create a pipeline that handles the errors
22022218
emitStream.removeListener('error', noop);
22032219

0 commit comments

Comments
 (0)