Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions with ECONNRESET errors until I redeploy

I'm using Google Cloud Functions to:

  1. Watch for a new Firebase entry
  2. Download a file that's referenced in the Firebase entry
  3. Generate a thumbnail based on that file.
  4. Upload the thumbnail to the cloud bucket.

Unfortunately I'm getting ECONNRESET errors repeatedly on step 4, and the only way to fix it seems to be to redeploy the function. Any ideas how to further debug this?

Edit:

It seems like many times when this happens, when I try to deploy the function again, it errors, and I have to run the deploy twice. Is something hanging or something?

enter image description here

like image 907
Edward Jiang Avatar asked Mar 27 '17 23:03

Edward Jiang


People also ask

What is cold start in cloud functions?

Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start. Cold starts can take significant amounts of time to complete.

Why does cloud deployment fail?

Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. Your source code must contain an entry point function that has been correctly specified in your deployment, either via Cloud console or Cloud SDK.


1 Answers

Update May 9 2017

According to this thread, the google cloud nodejs API developers have made some changes to the defaults that are used when initializing that should solve these ECONNRESET socket issues.

From @stephen++ on github GoogleCloudPlatform/google-cloud-node issue 2254:

We have disabled the forever agent by default in Cloud Function environments. If you un- and re-install @google-cloud/storage, you will pick up the new behavior automatically. Thanks for all of the helpful debugging, everyone!


Older Post Follows:

The solution for me to similar ECONNRESET issues using storage on the cloud functions platform was to use npm:promise-retry, but set up your own retry strategy because the default of 10 retries is too many.

I reported an ECONNRESET issue with cloud functions to Google Support (which you might star if you are also getting ECONNRESET in this context but not in other contexts) and they replied with a "won't fix" that the behavior is expected. Google support said the socket that the API client library uses to connect times out after a few minutes, and then when your cloud function tries to use it again you get ECONNRESET. They recommended adding autoRetry:true when initializing the storage API, but that did not help.

The ECONNRESETs happen on the read side too. In both read and write cases promise-retry helps, and most of the time with only 1 retry needed to reset the bad socket.

So I wrote npm:pipe-to-storage to return a promise to do the retries, check md5, etc., but I haven't tested it with binary data, only text, so I don't know if you could use it with image files. The calls would look like this:

const fs = require('fs');
const storage = require('@google-cloud/storage')();
const pipeToStorage = require('pipe-to-storage')(storage);
const source = ()=>(fs.createReadStream("/path/to/your/file/to/upload"));
pipeToStorage(source, bucketName, fileNameInBucket).then(//do next step);

See also How do I read the contents of a new cloud storage file of type .json from within a cloud function?

like image 56
Paul Avatar answered Sep 30 '22 16:09

Paul