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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@sentry/serverless "Cannot read properties of undefined (reading 'finish')" since 7.45.0 #7826

Closed
3 tasks done
TheHolyWaffle opened this issue Apr 12, 2023 · 4 comments · Fixed by #7829
Closed
3 tasks done
Assignees
Labels
Package: serverless Issues related to the Sentry Serverless SDK Type: Bug

Comments

@TheHolyWaffle
Copy link

TheHolyWaffle commented Apr 12, 2023

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using? If you use the CDN bundles, please specify the exact bundle (e.g. bundle.tracing.min.js) in your SDK setup.

@sentry/serverless

SDK Version

7.45.0

Framework Version

No response

Link to Sentry event

No response

SDK Setup

       AWSLambda.init({
            dsn: env.get('SENTRY_DSN'),
            tracesSampleRate: 1.0,
            environment: env.get('ENVIRONMENT_NAME'),
            normalizeDepth: 0,
            release: env.get('SENTRY_RELEASE'),
            beforeSend(event, hint) {
                const originalException = hint.originalException;
                if (originalException instanceof CrmApiError && !originalException.sendToSentry) {
                    // Some exceptions are known errors than can occur during runtime.
                    // We don't want to pollute Sentry with these issues, these errors have 'sendToSentry' set to false
                    return null;
                }
                return event;
            },
            maxValueLength: 500, // default is 250, avoids cutting of Error's with a long 'message',
            integrations: [
                // Nodejs stacktraces by default point towards `/var/task/index.js`
                // We have to change this path so that it corresponds to the uploaded sourcemaps in Sentry. See .gitlab-ci.yml
                new RewriteFrames({
                    root: '/var/task',
                    prefix: `app:///${env.get('SENTRY_FUNCTION_ID')}/`,
                }),
            ],
        });

Steps to Reproduce

Wrap an AWS lambda handler using and throw an Error:

AWSLambda.wrapHandler(..., {
    captureAllSettledReasons: true,
})

Expected Result

Lambda does not fail

Actual Result

{
  "errorType": "TypeError",
  "errorMessage": "Cannot read properties of undefined (reading 'finish')",
  "trace": [
    "TypeError: Cannot read properties of undefined (reading 'finish')",
    "    at Runtime.handler (/var/task/index.js:28:35554)"
  ]
}
@TheHolyWaffle
Copy link
Author

TheHolyWaffle commented Apr 12, 2023

This issue arises when upgrading @sentry/serverless from 7.44.2 to 7.45.0

When inspecting the bundled lambda code and looking for position 28:35554 I end up at this piece of code that has the unexpected undefined variable:

      let m,
        _ = s;
      _.headers &&
        ur.isString(_.headers["sentry-trace"]) &&
        (m = ur.extractTraceparentData(_.headers["sentry-trace"]));
      let b = _.headers && _.headers.baggage,
        E = ur.baggageHeaderToDynamicSamplingContext(b),
        N = St.getCurrentHub(),
        q = N.startTransaction({
          name: o.functionName,
          op: "function.aws.lambda",
          ...m,
          metadata: {
            dynamicSamplingContext: m && !E ? {} : E,
            source: "component",
          },
        }),
        Y = N.pushScope(),
        B;
      try {
        CA(Y, o, r),
          Y.setSpan(q),
          (B = await i(s, o)),
          a.captureAllSettledReasons &&
            Array.isArray(B) &&
            NA(B) &&
            RA(B).forEach((ee) => {
              St.captureException(ee);
            });
      } catch (F) {
        throw (St.captureException(F), F);
      } finally {
        clearTimeout(n),
          q.finish(), // <--- ERROR OCCURS HERE
          N.popScope(),
          await St.flush(a.flushTimeout).catch((F) => {
            (typeof __SENTRY_DEBUG__ > "u" || __SENTRY_DEBUG__) &&
              ur.logger.error(F);
          });
      }

@AbhiPrasad
Copy link
Member

Hey! If you provide debug: true to your SDK init, what shows up in the logs? I suspect for some reason our tracing API is not be registered, so N.startTransaction returns undefined.

@AbhiPrasad AbhiPrasad added Status: Needs Information Package: serverless Issues related to the Sentry Serverless SDK labels Apr 12, 2023
@TheHolyWaffle
Copy link
Author

TheHolyWaffle commented Apr 12, 2023

@AbhiPrasad Turned on debug: true but I saw no extra output in the cloudwatch logs.

This made me realize, we're running integration tests and purposely do not call AWSLambda.init. (This is because we don't want to send events to Sentry during integration tests.) The lambda handler, however, is still being wrapped with AWSLambda.wrapHandler. This also explains why the tracing API is not yet set up.

So to me, it seems that the AWSLambda.wrapHandler should also work when AWSLambda.init is not called

@AbhiPrasad
Copy link
Member

Yup! I see what is happening - we can fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: serverless Issues related to the Sentry Serverless SDK Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants