Sendgrid emails sends fail 90-95% of the time w/ 'connResetException' or 'ETIMEDOUT'

We are using inngest + sendgrid to send invite emails. This works reliably when running locally, but when deployed to netlify the function runs fail 90-95%+ of the time.

We know that inngest successfully registers the request to send email events and then calls our inngest api endpoint at netlify. Per the netlify function logs for our inngest endpoint, we either see silent failures to send or errors related to “connResetException” or “ETIMEDOUT”. These error messages will look something like:

  1. “errorMessage”: “Error: Client network socket disconnected before secure TLS connection was established”
  2. “errorMessage”: “Error: connect ETIMEDOUT 54.90.121.51:443”,

We’ve tried many variations of code and calls based on other netlify support forum posts related to sendgrid issues (e.g. SendGrid not sending from Netlify Functions).

We’re not sure what else to try at this point and would really appreciate some help.

More Context:

Our code looks like this right now. Everything is encapsulated in one function to avoid thawed contexts in lambda (e.g. Known issues - Fauna Documentation).

export const sendInvitationEmail = async (
  inviteeEmail: string,
  inviterName: string,
  code: string
) => {
  const sgMail = require('@sendgrid/mail')
  sgMail.setApiKey('apiKey')

  const msg = {
    to: inviteeEmail,
    from: {
      email: 'team@example.com',
      name: 'Example Team',
    },
    subject: `Invite subject`,
    text: `Invite text`,
    html: `Invite html`,
  }

  const ret = await sgMail.send(msg)
}

Here are some full error jsons.

{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: Client network socket disconnected before secure TLS connection was established",
  "reason": {
    "message": "Client network socket disconnected before secure TLS connection was established",
    "name": "Error",
    "stack": "Error: Client network socket disconnected before secure TLS connection was established\n    at connResetException (node:internal/errors:705:14)\n    at TLSSocket.onConnectEnd (node:_tls_wrap:1594:19)\n    at TLSSocket.emit (node:events:525:35)\n    at endReadableNT (node:internal/streams/readable:1358:12)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",
    "config": {
      "transitional": {
        "silentJSONParsing": true,
        "forcedJSONParsing": true,
        "clarifyTimeoutError": false
      },
      "transformRequest": [
        null
      ],
      "transformResponse": [
        null
      ],
      "timeout": 0,
      "xsrfCookieName": "XSRF-TOKEN",
      "xsrfHeaderName": "X-XSRF-TOKEN",
      "maxContentLength": null,
      "maxBodyLength": null,
      "headers": {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "User-Agent": "sendgrid/7.7.0;nodejs",
        "Authorization": “redacted”,
        "Content-Length": 593
      },
      "url": "/v3/mail/send",
      "method": "post",
      "data": “{redacted}”,
      "baseURL": "https://api.sendgrid.com/"
    },
    "code": "ECONNRESET",
    "status": null
  },
  "promise": {},
  "stack": [
    "Runtime.UnhandledPromiseRejection: Error: Client network socket disconnected before secure TLS connection was established",
    "    at process.<anonymous> (file:///var/runtime/index.mjs:1188:17)",
    "    at process.emit (node:events:513:28)",
    "    at emit (node:internal/process/promises:140:20)",
    "    at processPromiseRejections (node:internal/process/promises:274:27)",
    "    at processTicksAndRejections (node:internal/process/task_queues:97:32)"
  ]
}
{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: connect ETIMEDOUT 54.90.121.51:443",
  "reason":
    {
      "message": "connect ETIMEDOUT 54.90.121.51:443",
      "name": "Error",
      "stack": "Error: connect ETIMEDOUT 54.90.121.51:443\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)",
      "config":
        {
          "transitional":
            {
              "silentJSONParsing": true,
              "forcedJSONParsing": true,
              "clarifyTimeoutError": false,
            },
          "transformRequest": [null],
          "transformResponse": [null],
          "timeout": 0,
          "xsrfCookieName": "XSRF-TOKEN",
          "xsrfHeaderName": "X-XSRF-TOKEN",
          "maxContentLength": null,
          "maxBodyLength": null,
          "headers":
            {
              "Accept": "application/json",
              "Content-Type": "application/json",
              "User-Agent": "sendgrid/7.7.0;nodejs",
              "Authorization": “redacted”,
              "Content-Length": 581,
            },
          "url": "/v3/mail/send",
          "method": "post",
          "data": “{redacted}”,
          "baseURL": "https://api.sendgrid.com/",
        },
      "code": "ETIMEDOUT",
      "status": null,
    },
  "promise": {},
  "stack":
    [
      "Runtime.UnhandledPromiseRejection: Error: connect ETIMEDOUT 54.90.121.51:443",
      "    at process.<anonymous> (file:///var/runtime/index.mjs:1188:17)",
      "    at process.emit (node:events:513:28)",
      "    at emit (node:internal/process/promises:140:20)",
      "    at processPromiseRejections (node:internal/process/promises:274:27)",
      "    at processTicksAndRejections (node:internal/process/task_queues:97:32)",
    ],
}

Hey @akash-wellspring,

Sorry for the delay. The thread was dropped from our internal tracking system and I just discovered this today during a clean-up. Are you still facing issues?