Skip to content

Application hangs when destroying a File.createWriteStream() stream to abort the upload #2367

@vbshnsk

Description

@vbshnsk

Environment details

  • OS: Mac OS Ventura 13.5
  • Node.js version: 16.19
  • npm version: 8.19
  • @google-cloud/storage version: 7.6.0

Steps to reproduce

So I am trying to implement an endpoint on my server that would pass the incoming data to a File write stream and save it to GCS, and I want to halt the upload if during streaming file turns out to be bigger than expected. Here's a snippet of how it is implemented:

const MAX_TRANSACTION_IMAGE_SIZE_BYTES = 10 * 1024 * 1024;

async function test(body: NodeJS.ReadableStream) {
  let receivedBytes = 0;
  const cancelStream = new AbortController();

  const bucketName = 'bucket';
  const file = this.storage
    .bucket(bucketName)
    .file('image.png')
    .createWriteStream();

  const validateStream = new PassThrough();
  validateStream.on('data', (chunk) => {
    receivedBytes += chunk.length;
    if (receivedBytes > MAX_TRANSACTION_IMAGE_SIZE_BYTES) {
      // this correctly aborts everything in a pipeline, and destroys the streams, but
      // the underlying streams still seem to hang
      cancelStream.abort(new PayloadTooLargeException('Image too large'));
    }
  });

  await pipeline(body, validateStream, file, {
    signal: cancelStream.signal,
  });
}

I have checked the code and I see that calling .destroy() on the returned write stream does not really result in anything being passed to the underlying emitStream or writeFileStream, so they stay hanging. Is this a bug on the SDK side? Is there a way to abort the upload request?

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/nodejs-storage API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions