Skip to content

Wrong scope used when pushing/popping scope manually #5847

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

Closed
3 tasks done
danielrearden opened this issue Sep 28, 2022 · 2 comments
Closed
3 tasks done

Wrong scope used when pushing/popping scope manually #5847

danielrearden opened this issue Sep 28, 2022 · 2 comments
Labels
Package: node Issues related to the Sentry Node SDK

Comments

@danielrearden
Copy link

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

7.14.0

Framework Version

Node 16.15.1

Link to Sentry event

No response

Steps to Reproduce

We are using Fastify and have configured Sentry using a plugin:

const sentryPlugin = fastifyPlugin(async (instance) => {
  instance.addHook('onRequest', (request, reply, done) => {
    const currentHub = getCurrentHub();

    currentHub.pushScope();

    currentHub.configureScope((scope) => {
      scope.addEventProcessor((event) => {
        return addRequestDataToEvent(event, request.raw);
      });

      // additional context set on the scope
      // including setting the user
    })

    done();
  });
  
  instance.addHook('onResponse', (request, reply, done) => {
    const currentHub = getCurrentHub();

    currentHub.popScope();

    done();
  });

If we just fire off a few requests, this all works as expected -- the event in Sentry is decorated with the correct context and user. But if we spam the server, we can see the scope leaking and the event showing the wrong user.

As far as we can tell, we're calling popScope each time we call pushScope. Is there anything else that could explain this behavior?

Expected Result

When calling any SDK method like captureException, the correct scope would be used after calling pushScope.

Actual Result

Sporadically, the wrong scope is used.

@AbhiPrasad
Copy link
Member

Hey, thanks for writing in!

But if we spam the server, we can see the scope leaking and the event showing the wrong user

Yes, this is because the operations are async. Therefore, when you call popScope, you might be popping the wrong scope off the stack. In these situations, we recommend storing the scope with async hooks, or using domains as is noted in #4784 (comment)

@danielrearden
Copy link
Author

Thank you for the clarification @AbhiPrasad. Closing.

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

No branches or pull requests

2 participants