Back

Function: An internal curl error has occurred within the executor! Error Msg: Operation timed out

  • 1
  • Functions
THE-E
15 Apr, 2023, 15:20

I have 2 Appwrite Functions written in TypeScript with the node-sdk which work most of the time fine, but in some cases I get the following error:

TypeScript
[Error] Type: Exception
[Error] Message: An internal curl error has occurred within the executor! Error Msg: Operation timed out
[Error] File: /usr/src/code/app/executor.php
[Error] Line: 544`

I found two GitHub issues, with similar problems:

The TypeScript file is converted into JavaScript before deployment (tsconfig.json can be provided, if necessary)

Code is posted on the next post.

TL;DR
The user is experiencing an internal curl error within their MongoDB function. They found a similar issue on StackOverflow, where it suggests wrapping the code in a try-catch block. They also question if the error is related to the `finally` block and suggest catching potential exceptions. They mention that increasing the timeout and using `await` did not solve the problem. Other suggestions include removing the nested run function and increasing the timeout in function settings. The user is unable to see what exactly is failing and there are no errors in the logs. The timeout occurs after several minutes. It is suggested to coerce the error to a string and to remove any
THE-E
15 Apr, 2023, 15:20
TypeScript
import { Client, Account, Models } from "node-appwrite";
import { MongoClient } from "mongodb";

module.exports = async function (req: any, res: any) {
  const client = new Client();
  const user = req.variables["MONGODB_USER"];
  const password = req.variables["MONGODB_PASSWORD"];
  const hostname = req.variables["MONGODB_HOSTNAME"];
  // Create a new MongoClient
  const mongoUri = `mongodb://${user}:${password}@${hostname}:27017/?authMechanism=DEFAULT`;
  const mongoDbClient = new MongoClient(mongoUri);
  let userId: string;

  if (
    !req.variables["APPWRITE_FUNCTION_ENDPOINT"] ||
    !req.variables["APPWRITE_FUNCTION_API_KEY"]
  ) {
    console.warn(
      "Environment variables are not set. Function cannot use Appwrite SDK."
    );
  } else {
    client
      .setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"])
      .setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"])
      .setJWT(req.variables["APPWRITE_FUNCTION_JWT"])
      .setSelfSigned(true);
  }
  const account = new Account(client);
  let accountData: Models.Account<Models.Preferences>; 

  try {
    accountData = await account.get();
  } catch (error: any) {
    console.log(error); // Failure
  }

  userId = accountData!.$id;

  async function run() {
    try {
      // Connect the client to the server (optional starting in v4.7)
      await mongoDbClient.connect();
      let reqPayloadObj = JSON.parse(req.payload ?? "{}"); //Make string to object

      console.log(reqPayloadObj);

      const pipeline = //Pipeline 

      const coll = mongoDbClient.db("aticAR").collection("container_items");
      let myContainerItems = await coll.aggregate(pipeline).toArray();
      //Create Response
      res.json(JSON.stringify(myContainerItems));
    } catch(error: any){
      res.json({'error': error.toString()});
      console.log(error);
    } 
    finally {
      // Ensures that the client will close when you finish/error
      await mongoDbClient.close();
    }
  }
  run();
};
Drake
15 Apr, 2023, 15:21

This usually happens if your function throws an error and/or doesn't call res.json() or res.send() once

Drake
15 Apr, 2023, 15:23

Btw I don't think you need that nested run function 🧐

Drake
15 Apr, 2023, 15:25

Also, it might be better to coerce the error to a string like

TypeScript
console.log(error.toString());
Drake
15 Apr, 2023, 15:26

How long does it take before the timeout occurs?

THE-E
15 Apr, 2023, 15:27

The problem is, that it is a black box for me, I can't see, what exactly is failing. There is no Error in the container logs, appwrite logs, or appwrite execution logs.

THE-E
15 Apr, 2023, 15:27

15 seconds

Drake
15 Apr, 2023, 15:27

Maybe you're actually hitting the timeout. You can try increasing the timeout in your function settings

THE-E
15 Apr, 2023, 15:32

The problem is, that most of the time it succeeds with a response time of just few milliseconds.

Drake
15 Apr, 2023, 15:35

Ya if the timeout occurs, you won't get any data back.

THE-E
15 Apr, 2023, 15:36

Hmm, then debugging this issue is difficult/impossible?

Drake
15 Apr, 2023, 15:37

That's why I suggest increasing the timeout

THE-E
15 Apr, 2023, 15:37

Alright, I will do that.

Drake
15 Apr, 2023, 15:38

Also, the other suggestion about the run... either remove it or await it

THE-E
15 Apr, 2023, 15:57

I added await and increased timeout to 180 seconds, the result is the same. Strangely the duration is in ms even though it takes several minutes to execute/respond (maybe a bug?).

Drake
15 Apr, 2023, 15:58

No change in the logs?

THE-E
15 Apr, 2023, 15:58

Yep, no change. 🀨

Drake
15 Apr, 2023, 15:59

I wonder if there's an error thrown by the mongodb.close() 🧐

THE-E
15 Apr, 2023, 16:01

Is finally run after or before the respond is being send by the function?

Drake
15 Apr, 2023, 16:02

Before

THE-E
15 Apr, 2023, 16:06

Should I wrap it in a try-catch-block? Or is a potential exception caught by the upper catch-block?

Drake
15 Apr, 2023, 16:35

Try catch it just in case

THE-E
15 Apr, 2023, 17:08

This seems to be a strange behaviour of MongoDB. I got the following error when the function fails:

TypeScript
Command "top" returned error "connection 1 to <server-ip> closed"
Command "currentOp" returned error "Invalid collection name specified 'admin.$cmd.sys.inprog"
Command "serverStatus" returned error "connection 3 to <server-ip> closed"

https://stackoverflow.com/questions/59942238/mongoerror-topology-is-closed-please-connect-despite-established-database-conn So the error is most probably caused by the finally block and await mongoDbClient.close();.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more