question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running into connection resets after 60s when streaming to GCS, `timeout` setting has no effect

See original GitHub issue

Environment details

  • OS: Ubuntu 20.04
  • Node.js version: 14.5.0
  • npm version: using yarn
  • @google-cloud/storage version: 5.1.2

Steps to reproduce

I’m trying to stream large files to GCS via createWriteStream(). The files are streamed from a multi-part file upload in a POST handler through ffmpeg to createWriteStream(). When an upload stream takes longer than 60 seconds to complete, the connection resets and the upload never completes. I can’t seem to catch an error in that case. I tried to use the timeout option in createWriteStream() but it does not seem to do anything (although it is picked up in reqOpts).

EDIT: The issue is limited to piping the original Express request req through busboy to GCS. If I write the upload to disk before streaming it to GCS, all is well and the upload finishes as expected. So this might not actually be an issue with GCS. Feel free to close this issue in that case 🙂

My upload code looks like this:

const storageClient = new Storage(credentials);

function gcsUploadStream(key) {
  const pass = PassThrough();

  const bucket = storageClient.bucket(GOOGLE_BUCKET);
  const blob = bucket.file(key);

  const googleCloudStorageStream = blob.createWriteStream({
    resumable: false,
    validation: false,
    timeout: 1000 * 60 * 60,
  });

  pass.pipe(googleCloudStorageStream);

  const promise = new Promise((resolve, reject) => {
    googleCloudStorageStream.on('error', err => {
      googleCloudStorageStream.end();
      reject(err);
    });

    googleCloudStorageStream.on('finish', () => {
      googleCloudStorageStream.end();
      resolve(blob.name);
    });
  });

  return { writeStream: pass, promise };
}

The pipeline can be boiled down to:

app.post('/', (req, res) => {
busboy.on('file', async (fieldname, file, filename) => {
  const { writeStream, promise } = gcsUploadStream(`xo.flac`);
  flacEncoder(fileStream).pipe(writeStream);
});

busboy.on('finish', async () => {
  const gcsUri = await upload.promise;
  debug(`Finished upload for file: ${gcsUri}`);
});

req.pipe(busboy);
});

Any help would be greatly appreciated, thank you very much for looking!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Eywekcommented, Jul 21, 2020

I was having the same issue, after debugging I’ve found that the nodejs header timeout is the cause (cf. https://nodejs.org/api/http.html#http_server_headerstimeout), you can try to disable it with:

server.headersTimeout = 0
1reaction
jimmyleecommented, Oct 10, 2020

This issue saved my life, thank you so much. Just in case someone else needs it:

  const listenServer = server.listen(Environment.PORT, (e) => {
    if (e) throw e;

    console.log(`${Environment.PORT}`);
  });

  listenServer.headersTimeout = 0;

In case it wasn’t obvious that you couldn’t set headersTimeout = 0 on some arbitrary server object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting request timeout (services) | Cloud Run Documentation
For Cloud Run services, the request timeout setting specifies the time within which a response must be returned by services deployed to Cloud...
Read more >
Random timeouts when downloading/uploading to GCS ...
Hi, Cloud functions in production seem to time out quite frecuently when uploading/downloading objects from GCS buckets. We've never seen such timeouts ......
Read more >
Timeouts connecting to GCS blobstores when configured to ...
The connection to the storage is timing out after 60 seconds and the current client library does not allow the timeout to be...
Read more >
The Connection Has Timed Out How To Fix It Tutorial - YouTube
The Connection Has Timed Out -- How To Fix It [Tutorial].A server connection timeout means that a server is taking too long to...
Read more >
Setting the Idle Query and Idle Session Timeouts
In the Idle Session Timeout field, specify the time in seconds after which an idle session expires. A session is idle when no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found