小能豆

Firebase functions TLS & Socket hang up error when i run my function

javascript

I am getting this TLS & Socket hang up error each time my firebase function tries to access a collection group in my firestore database, any ideas what could be causing this error?

Exception from a finished function: FetchError: request to ... failed, reason: socket hang up

Firestore query error: FetchError: request to ... failed, reason: Client network socket disconnected before secure TLS connection was established"
const {onSchedule} = require("firebase-functions/v2/scheduler");
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");
initializeApp();
const db = getFirestore();
exports.checkOrders = onSchedule(
    {
      timeoutSeconds: 300,
      region: "europe-central2",
      schedule: "05 03 * * *",
    }, async (event) => {
      const now = new Date();
      const today = now.setHours(3, 0, 0, 0);
      const yearToday = now.getFullYear();
      const monthToday = now.getMonth() + 1;
      const dayToday = now.getDate();
      const formattedToday = `${yearToday}, ${monthToday < 10 ? "0" + monthToday : monthToday}, ${dayToday < 10 ? "0" + dayToday : dayToday}`;

      console.log(today);
      console.log(formattedToday);

      const mainSnapshot = await db.collection("xxx")
          .where("activity_status", "in", ["Active", "ActiveLast", "Pending", "Inactive", "", "Error"])
          .get();

      mainSnapshot.forEach((doc) => {
        const Data = doc.data();
        indexFunctions(Data, today, formattedToday);
      });

      async function indexFunctions(Data, today, formattedToday) {
        let retryCount = 0;
        const maxRetries = 3;

        const updateObject = {};
        let iterationCount = 1;
        const parentID = String(Data.id);
        const updateBatch = db.batch();
        while (retryCount < maxRetries) {
          try {
            const querySnapshot = await db.collectionGroup("qqq")
                .where("parent_id", "==", parentID).get();

// This is where the code starts throewing those errors, so i dont need to show the rest of the code.

My functions have the right permissions & the problem doesn’t seem to be coming from my code, at least i don’t think, because i tried stripping it down to the bone but still got these errors. I have tried checking for proxies, updating all dependancies and everything i could think of. Any help would be appreciated.


阅读 79

收藏
2023-12-02

共1个答案

小能豆

The error you’re encountering, “TLS & Socket hang up,” typically indicates an issue with the network connection or the server’s response. There are a few things you can check and try to troubleshoot this issue:

Network Issues:

Ensure that your development environment has stable internet connectivity.
If you are behind a corporate firewall or proxy, check if it is causing any interference.
If you are running this on a local development environment, try deploying the function to Firebase and see if the issue persists.
Firebase Quotas:

Check if your Firebase project is within its usage quotas. You might be hitting some limit, and Firebase is terminating the connection.
Cloud Firestore Limits:

Ensure that your Firestore database has not exceeded its limits or quotas. You can check the Firestore quotas documentation.
Timeouts:

You have set a timeout of 300 seconds for your scheduled function. While this is a generous timeout, if the function takes too long to respond, it might be terminated. Check if your function’s execution time is reasonable.
Retry Mechanism:

Your code includes a retry mechanism (while (retryCount < maxRetries)) for fetching data. While retries can be helpful, ensure that you are not hitting any kind of rate limit.
SSL/TLS Certificates:

Ensure that your server and Firestore have valid SSL/TLS certificates. TLS handshake issues can sometimes cause such problems.
Firebase SDK Version:

Ensure that you are using the latest version of the Firebase SDK. Outdated SDK versions might have bugs or compatibility issues.
Cloud Functions Region:

Check if your Cloud Function’s region (“europe-central2”) is causing any latency issues. You might try a different region for testing.
Firestore CollectionGroup Indexing:

Verify that you have proper indexing for your collection group queries. Firestore may require specific indexes for complex queries.
If none of the above resolves the issue, you may want to consider reaching out to Firebase support for further assistance, as they can investigate specific details related to your project.

2023-12-02