Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug Report: Function execution failed. #5423

Closed
2 tasks done
usamashafiqs opened this issue Apr 24, 2023 · 18 comments
Closed
2 tasks done

馃悰 Bug Report: Function execution failed. #5423

usamashafiqs opened this issue Apr 24, 2023 · 18 comments
Assignees
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.

Comments

@usamashafiqs
Copy link

usamashafiqs commented Apr 24, 2023

馃憻 Reproduction steps

On localhost this is running fine but not on ec2 instance.

Executions Logs

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

Appwrite-executor Logs

Executing Runtime: 6359c5064e8969d821a8-6446621b0787a4b581b4
Function executed in 0.38529300689697 seconds, status: failed
Executing Runtime: 6359c5064e8969d821a8-6446621b0787a4b581b4
[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
Executing Runtime: 6359c5064e8969d821a8-6446621b0787a4b581b4
Function executed in 120.00923085213 seconds, status: failed

Code

const nodemailer = require('nodemailer');

module.exports = async function (req, res) {
  const transporter = nodemailer.createTransport({
    host: req.variables.HOST,
    port: req.variables.PORT,
    secure: req.variables.SECURE,
    auth: {
      user: req.variables.USER,
      pass: req.variables.PASS,
    },
  });

  const payload = JSON.parse(req.payload);
  const name = payload['name'];
  const email = payload['email'];
  const subject = payload['subject'];
  const text = payload['text'];

  const info = await transporter.sendMail({
    from: `"Sender" <${req.variables.USER}>,`,
    to: `"${name}" <${email}>,`,
    subject: subject,
    text: text,
  });

  res.json({
    message: 'Email sent successfully',
    emailId: info.messageId,
  });
};

馃憤 Expected behavior

Function execution should be fine.

馃憥 Actual Behavior

Function execution failed.

馃幉 Appwrite version

Version 1.3.x

馃捇 Operating system

Linux

馃П Your Environment

No response

馃憖 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

馃彚 Have you read the Code of Conduct?

@usamashafiqs usamashafiqs added the bug Something isn't working label Apr 24, 2023
@stnguyen90 stnguyen90 self-assigned this Apr 27, 2023
@stnguyen90 stnguyen90 added the product / functions Fixes and upgrades for the Appwrite Functions. label Apr 27, 2023
@stnguyen90
Copy link
Contributor

@usamashafiqs thanks for creating this issue! 馃檹馃徏

This typically happens due to an error from your function code. Please try to make sure your code doesn't throw any errors (you can wrap all your code in a big try/catch and call res.json() or res.send() exactly once.

This could also be a true timeout. One way to tell is if your function's execution time is consistently the same as the timeout configured for your function. If this is the case, you should increase the timeout on the function.

@usamashafiqs
Copy link
Author

usamashafiqs commented Apr 30, 2023

@stnguyen90 I wrap my code with try/catch and increase the timeout as well still getting the same error.

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

appwrite-executor logs

Executing Runtime: 6359c5064e8969d821a8-644dc09db4011b512f5c
[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: 54

This code is working on local appwrite instance but not on ec2 or linode instances.

@stnguyen90
Copy link
Contributor

stnguyen90 commented May 2, 2023

I wrap my code with try/catch and increase the timeout as well still getting the same error.

@usamashafiqs, some more questions:

  1. what is the new code?
  2. how long does the execution run for before erroring out?
  3. what did you update the timeout to?

@usamashafiqs
Copy link
Author

@stnguyen90, because I set the timeout value to 60, it took 60 seconds.
new code:

const nodemailer = require('nodemailer');

module.exports = async function (req, res) {
  try {
    const transporter = nodemailer.createTransport({
      host: req.variables.HOST,
      port: req.variables.PORT,
      secure: req.variables.SECURE,
      auth: {
        user: req.variables.USER,
        pass: req.variables.PASS,
      },
    });

    const payload = JSON.parse(req.payload);
    const name = payload['name'];
    const email = payload['email'];
    const subject = payload['subject'];
    const text = payload['text'];

    const info = await transporter.sendMail({
      from: `"Sender" <${req.variables.USER}>,`, // Sender email
      to: `"${name}" <${email}>,`,
      subject: subject,
      text: text,
    });

    res.json({
      message: 'Email sent successfully',
      emailId: info.messageId,
    });
  } catch (e) {
    res.send(e);
  }
};

@stnguyen90
Copy link
Contributor

@usamashafiqs this could be a true timeout and because it times out, Appwrite isn't able to capture the logs. Would you please increase the timeout? In addition, instead of calling res.send(e), would you please change it to res.send(e.toString()?

@usamashafiqs
Copy link
Author

usamashafiqs commented May 25, 2023

@usamashafiqs this could be a true timeout and because it times out, Appwrite isn't able to capture the logs. Would you please increase the timeout? In addition, instead of calling res.send(e), would you please change it to res.send(e.toString()?

Same Error:
I test the code run fine on any type of environment.

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

@usamashafiqs
Copy link
Author

@stnguyen90 I think function is not doing async tasks. When I try to execute a simple fetch request within a function got this error.

ReferenceError: e is not defined
    at module.exports (/usr/code-start/index.js:55:19)
    at /usr/local/src/server.js:68:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

@Hu-Wentao
Copy link

Hu-Wentao commented May 28, 2023

see #4594 , try catch and return res.send() with 2xx code

@stnguyen90
Copy link
Contributor

When I try to execute a simple fetch request within a function

@usamashafiqs, please share your full code.

@usamashafiqs
Copy link
Author

nodemail.tar.gz
@stnguyen90 This is the full code of the function.

@50l3r
Copy link

50l3r commented Jun 9, 2023

I have the same problem with a function who execute node-fetch call.

I avoid exceptions and use res.json but problem persists.

NOTE: The error occurs arbitrarily and not always.

@50l3r
Copy link

50l3r commented Jun 9, 2023

Oh my god, after searching a lot in the documentation and github I have seen that each function has a time limit that is specified in its configuration: https://appwrite.io/docs/functions#:~:text=Each%20execution%20has%20a%20default,for%20up%20to%20900%20seconds.

@usamashafiqs I think this can help you

@Haimantika
Copy link
Contributor

Oh my god, after searching a lot in the documentation and github I have seen that each function has a time limit that is specified in its configuration: https://appwrite.io/docs/functions#:~:text=Each%20execution%20has%20a%20default,for%20up%20to%20900%20seconds.

@usamashafiqs I think this can help you

Yes there is a timeout for functions. @usamashafiqs did knowing this help you?

@stnguyen90
Copy link
Contributor

@usamashafiqs,

nodemail.tar.gz @stnguyen90 This is the full code of the function.

The console.error(e); could be a problem. It would be best to do console.error(e.toString());.

In addition, it's odd the stack trace doesn't quite line up with the code. The stack trace says the error occurred on line 55, but your file is less than 55 lines.

@usamashafiqs
Copy link
Author

usamashafiqs commented Jul 10, 2023

@stnguyen90 The stack trace of the current code is:

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

I try to execute a simple fetch request within a function got this stack trace.

ReferenceError: e is not defined
    at module.exports (/usr/code-start/index.js:55:19)
    at /usr/local/src/server.js:68:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

They both are different, but they are doing async tasks.

I think the problem is that Function is not doing async tasks.

@stnguyen90
Copy link
Contributor

@usamashafiqs,

I try to execute a simple fetch request within a function got this stack trace.

ReferenceError: e is not defined
    at module.exports (/usr/code-start/index.js:55:19)
    at /usr/local/src/server.js:68:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

They both are different, but they are doing async tasks.

Would you please share your latest full code for this function so I can see what's on line 55 that's throwing the error?

I think the problem is that Function is not doing async tasks.

Async code works fine in Appwrite functions.

@stnguyen90
Copy link
Contributor

@usamashafiqs, are you still facing this problem? FYI, I'll need to close this due to inactivity soon.

@MACTEPyc
Copy link

MACTEPyc commented Aug 21, 2023

If you are using Appwrite locally on your computer, you may have problems connecting to the API. In my case, replacing the localhost address in the endpoint with the IP address of my computer helped.

Do not use "http:https://localhost/v1" or "http:https://127.0.0.1/v1" when calling Appwrite API FUNCTIONS if the server is running locally. Instead, specify the actual IP address of your machine in the ENDPOINT.

I spent two days figuring out this problem. I hope my experience will help someone save time and avoid headaches due to the wrong endpoint.

@stnguyen90 stnguyen90 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.
Projects
None yet
Development

No branches or pull requests

6 participants