getsentry / getsentry/sentry-javascript

webWorkerIntegration: breadcrumbs/tags/context set in worker thread get lost

Open
#18,704 7 comments 0 reactions 1 assignee Claimed by @andreiborza View on GitHub
Browser Feature
Dominant language
TypeScript
Stars
8.7k
Forks
1.8k
Avg merge
1d 17h
Merged PRs (30d)
515

Description

### Is there an existing issue for this?

- [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [x] I have reviewed the documentation https://docs.sentry.io/
- [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

### How do you use Sentry?

Sentry Saas (sentry.io)

### Which SDK are you using?

@sentry/browser

### SDK Version

10.32.1

### Framework Version

_No response_

### Link to Sentry event

_No response_

### Reproduction Example/SDK Setup

```ts
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: import.meta.env.VITE_SENTRY_DSN,
sendDefaultPii: false,
integrations: [Sentry.browserTracingIntegration()],
});

// then in the module that creates workers...
const sentryWebWorkerIntegration = Sentry.webWorkerIntegration({ worker: [] });
Sentry.addIntegration(sentryWebWorkerIntegration);

// and when a worker is created...
const worker = new Worker(new URL("./worker.ts", import.meta.url), {...});
sentryWebWorkerIntegration.addWorker(worker);

// and in worker.ts...
import * as Sentry from "@sentry/browser";
Sentry.registerWebWorker({ self });
```

(I'd be happy to extract a minimal reproducible project if this is a bug that's likely to be fixed. You can see the actual code I'm running here: [Sentry.init](https://github.com/medmunds/puzzles-web/blob/88bfc83d38f8883f7bf00f4695284d51aaa8282e/src/main.ts#L31-L124), [webWorkerIntegration](https://github.com/medmunds/puzzles-web/blob/88bfc83d38f8883f7bf00f4695284d51aaa8282e/src/puzzle/puzzle.ts#L26-L31), [addWorker](https://github.com/medmunds/puzzles-web/blob/88bfc83d38f8883f7bf00f4695284d51aaa8282e/src/puzzle/puzzle.ts#L43-L47), [registerWebWorker](https://github.com/medmunds/puzzles-web/blob/88bfc83d38f8883f7bf00f4695284d51aaa8282e/src/puzzle/worker.ts#L5-L9), worker call to [addBreadcrumb](https://github.com/medmunds/puzzles-web/blob/88bfc83d38f8883f7bf00f4695284d51aaa8282e/src/puzzle/worker.ts#L52-L63).)

### Steps to Reproduce

1. Create a project that uses web workers with the Sentry.webWorkerIntegration [as documented](https://docs.sentry.io/platforms/javascript/best-practices/web-workers/), including calling Sentry.registerWebWorker in the worker
2. In the worker, call `Sentry.addBreadcrumb({...})`
3. Do something that captures an exception (in the worker or on the main thread)
4. Examine the breadcrumbs in the recorded event for the data you added in step 2

### Expected Result

The breadcrumb you added in step 2 should be present on the recorded event.

When using webWorkerIntegration, calls in the web worker thread to Sentry.addBreadcrumb (and setTags, setContext, addAttachment, etc.) should get forwarded to the main thread and attached to events there.

(Ideally, calling Sentry.registerWebWorker in the worker would also instrument console, fetch, and other worker-thread functions that Sentry normally hooks in the main thread.)

### Actual Result

Calling Sentry.addBreadcrumb from a web worker appears to succeed, but the data is not attached to later captured events.

### Additional Context

If enriching events from the worker isn't meant to be supported, perhaps Sentry could throw an error (or at least console.warn) when those APIs are called from the worker. (Presumably Sentry in the worker knows whether it is being used with registerWebWorker—so the APIs won't work—or separately with Sentry.init.)

The same probably goes for calling other APIs (like Sentry.addIntegration) that wouldn't be forwardable from the worker.

### Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.