Skip to content

withIsolationScope failed to capture context for unhandled exception inside setInterval #16460

Closed
@zhiyan114

Description

@zhiyan114

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

9.24.0

Framework Version

Node 22.14.0 LTS

Link to Sentry event

No response

Reproduction Example/SDK Setup

https://github.com/zhiyan114/Firey-Bot/blob/275621a29a3f4181857476fd0a002a83301d91e9/src/services/VoiceChatReward.ts#L77C3-L110C4

In line 81, a user context is attached to the isolation scope, but (unhandled) exception in line 90 didn't capture this information.

The following function is being called here: https://github.com/zhiyan114/Firey-Bot/blob/275621a29a3f4181857476fd0a002a83301d91e9/src/services/VoiceChatReward.ts#L35C4-L35C71

Steps to Reproduce

For minimal reproduction step, the following code should replicate the exact behavior:

  • Use withIsolationScope within setTimeout, setTimeout, or equivalent.
import { init, captureException, consoleLoggingIntegration, extraErrorDataIntegration, flush, logger, rewriteFramesIntegration, withIsolationScope, withIsolationScope } from "@sentry/node";

function caller(bk: ()=> Promise<void>) {
    setTimeout(bk, 1000);
}

process.env["SENTRY_RELEASE"] = execSync(`git -C ${__dirname} rev-parse HEAD`).toString().trim(),
init({
    dsn: "someDSN",
    maxValueLength: 1000,
    tracesSampleRate: 0,
    sendDefaultPii: true,
    _experiments: {
      // Enable logs to be sent to Sentry.
      enableLogs: true,
    },
    integrations: [
        extraErrorDataIntegration({
          depth: 3
        }),
        
        consoleLoggingIntegration({ levels: ['log', 'error', 'warn'] }),
        
        rewriteFramesIntegration({
          iteratee(frame) {
            const absPath = frame.filename;
            if(!absPath) return frame;
            frame.filename = `/${path.relative(__dirname, absPath).replace(/\\/g, "/")}`
            return frame;
          }
        }),
      ],
      beforeSend: (a,b) => {
        console.log("Sending Error to Sentry...")
        return a;
      },
      beforeBreadcrumb: (a,b) => {
        console.log(`Sending Breadcrumb to Sentry: ${a.message}`)
        return a;
      },
      beforeSendTransaction: () => null,
      debug: true,
})

caller(async()=>{
  await withIsolationScope(async (scope) => {
        scope.setUser({
            id: 0,
            username: "ROOT",
            Priv: "SuperUser"
        })
        console.log("HIII");
        throw new Error("TESTING!!!!"); // setUser ctx will not be attached
        //captureException(new Error("TESTING!!!!")); // setUser ctx will be attached
    })
});

Expected Result

Unhandled exception are expected to attach user info context that's wrapped around by the withScope

Actual Result

The given context doesn't show up on the dashboard, the workaround is to force all errors to pass through captureException (which then it wouldn't be unhandled).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions