Firebase Functions: Unclear "connection error"

Function execution took ****ms, finished with status: 'connection error' or ECONNRESET usually happens when a function doesn’t know whether a promise resolved or not.

Every promise must be returned, as mentioned in the docs here. There is also a blog post (with helpful video!) about this.

A couple of examples of unreturned promises:

exports.someFunc = functions.database.ref('/some/path').onCreate(event => {
     let db = admin.database();

     // UNRETURNED PROMISE
     db.ref('/some/path').remove();

     return db.ref('/some/other/path').set(event.data.val());
});
exports.makeUppercase = functions.database.ref('/hello/{pushId}').onWrite(event => {
    return event.data.ref.set('world').then(snap => {

      // UNRETURNED PROMISE
      admin.database().ref('lastwrite').set(admin.database.ServerValue.TIMESTAMP);

    });
});
exports.makeUppercase = functions.database.ref('/hello/{pushId}').onWrite(event => {

    // UNRETURNED PROMISE
    event.data.ref.set('world').then(snap => {
        return admin.database().ref('lastwrite').set(admin.database.ServerValue.TIMESTAMP);
    });
});

To help catch this mistake before deploying code, check out this eslint rule.

For an in-depth look at promises, here are some helpful resources:

  • Mozilla docs
  • Ponyfoo promises deep dive
  • Links to the ECMA standard
  • Egghead.io course

Even though this question has an approved answer, you may have followed the steps in that answer and still reached a point where the error was still occurring.

In that case, we were informed by GCP that there's a known issue with Node 8 CFs and this connection error, for which the workaround is to update the node version to 10.

Related github issue: https://github.com/firebase/firebase-functions/issues/429

Specific comment: https://github.com/firebase/firebase-functions/issues/429#issuecomment-577324193


I think it might be too many simultaneous firebase database connections :/ https://groups.google.com/forum/#!topic/firebase-talk/4RjyYIDqMVQ