-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(wasm): initialised sentryWasmImages for webworkers #18812
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
Open
harshit078
wants to merge
7
commits into
getsentry:develop
Choose a base branch
from
harshit078:support-wasm-web-workers
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
32071e7
feat(wasm): initialised sentryWasmImages for webworkers
harshit078 96a750e
Merge branch 'develop' into support-wasm-web-workers
harshit078 02ea888
feat(wasm): added tests for webworker
harshit078 a38298e
Merge branch 'develop' into support-wasm-web-workers
harshit078 4d5657e
Merge branch 'develop' into support-wasm-web-workers
harshit078 dc1a2c1
feat(wasm): address cursor comments and added test suite
harshit078 4796ccb
fix(wasm): address cursor comments
harshit078 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
dev-packages/browser-integration-tests/suites/wasm/webWorker/assets/worker.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // This worker manually just replicates what the actual Sentry.registerWebWorkerWasm() does | ||
|
|
||
| const origInstantiateStreaming = WebAssembly.instantiateStreaming; | ||
| WebAssembly.instantiateStreaming = function instantiateStreaming(response, importObject) { | ||
| return Promise.resolve(response).then(res => { | ||
| return origInstantiateStreaming(res, importObject).then(rv => { | ||
| if (res.url) { | ||
| registerModuleAndForward(rv.module, res.url); | ||
| } | ||
| return rv; | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| function registerModuleAndForward(module, url) { | ||
| const buildId = getBuildId(module); | ||
|
|
||
| if (buildId) { | ||
| const image = { | ||
| type: 'wasm', | ||
| code_id: buildId, | ||
| code_file: url, | ||
| debug_file: null, | ||
| debug_id: (buildId + '00000000000000000000000000000000').slice(0, 32) + '0', | ||
| }; | ||
|
|
||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWasmImages: [image], | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Extract build ID from WASM module | ||
| function getBuildId(module) { | ||
| const sections = WebAssembly.Module.customSections(module, 'build_id'); | ||
| if (sections.length > 0) { | ||
| const buildId = Array.from(new Uint8Array(sections[0])) | ||
| .map(b => b.toString(16).padStart(2, '0')) | ||
| .join(''); | ||
| return buildId; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // Handle messages from the main thread | ||
| self.addEventListener('message', async event => { | ||
| if (event.data.type === 'load-wasm-and-crash') { | ||
| const wasmUrl = event.data.wasmUrl; | ||
|
|
||
| function crash() { | ||
| throw new Error('WASM error from worker'); | ||
| } | ||
|
|
||
| try { | ||
| const { instance } = await WebAssembly.instantiateStreaming(fetch(wasmUrl), { | ||
| env: { | ||
| external_func: crash, | ||
| }, | ||
| }); | ||
|
|
||
| instance.exports.internal_func(); | ||
| } catch (err) { | ||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWorkerError: { | ||
| reason: err, | ||
| filename: self.location.href, | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| self.addEventListener('unhandledrejection', event => { | ||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryWorkerError: { | ||
| reason: event.reason, | ||
| filename: self.location.href, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| // Let the main thread know that worker is ready | ||
| self.postMessage({ _sentryMessage: false, type: 'WORKER_READY' }); |
15 changes: 15 additions & 0 deletions
15
dev-packages/browser-integration-tests/suites/wasm/webWorker/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { wasmIntegration } from '@sentry/wasm'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [wasmIntegration({ applicationKey: 'wasm-worker-app' })], | ||
| }); | ||
|
|
||
| const worker = new Worker('/worker.js'); | ||
|
|
||
| Sentry.addIntegration(Sentry.webWorkerIntegration({ worker })); | ||
|
|
||
| window.wasmWorker = worker; |
8 changes: 8 additions & 0 deletions
8
dev-packages/browser-integration-tests/suites/wasm/webWorker/subject.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| window.events = []; | ||
|
|
||
| window.triggerWasmError = () => { | ||
| window.wasmWorker.postMessage({ | ||
| type: 'load-wasm-and-crash', | ||
| wasmUrl: 'https://localhost:5887/simple.wasm', | ||
| }); | ||
| }; |
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/wasm/webWorker/template.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="triggerWasmError">Trigger WASM Error in Worker</button> | ||
| </body> | ||
| </html> |
138 changes: 138 additions & 0 deletions
138
dev-packages/browser-integration-tests/suites/wasm/webWorker/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { envelopeRequestParser, waitForErrorRequest } from '../../../utils/helpers'; | ||
| import { shouldSkipWASMTests } from '../../../utils/wasmHelpers'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| wasmWorker: Worker; | ||
| triggerWasmError: () => void; | ||
| } | ||
| } | ||
|
|
||
| const bundle = process.env.PW_BUNDLE || ''; | ||
| if (bundle.startsWith('bundle')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| sentryTest( | ||
| 'WASM debug images from worker should be forwarded to main thread and attached to events', | ||
| async ({ getLocalTestUrl, page, browserName }) => { | ||
| if (shouldSkipWASMTests(browserName)) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| await page.route('**/simple.wasm', route => { | ||
| const wasmModule = fs.readFileSync(path.resolve(__dirname, '../simple.wasm')); | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: wasmModule, | ||
| headers: { | ||
| 'Content-Type': 'application/wasm', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/worker.js', route => { | ||
| return route.fulfill({ | ||
| path: `${__dirname}/assets/worker.js`, | ||
| }); | ||
| }); | ||
|
|
||
| const errorEventPromise = waitForErrorRequest(page, e => { | ||
| return e.exception?.values?.[0]?.value === 'WASM error from worker'; | ||
| }); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| await page.waitForFunction(() => window.wasmWorker !== undefined); | ||
|
|
||
| await page.evaluate(() => { | ||
| window.triggerWasmError(); | ||
| }); | ||
|
|
||
| const errorEvent = envelopeRequestParser(await errorEventPromise); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('WASM error from worker'); | ||
|
|
||
| expect(errorEvent.debug_meta?.images).toBeDefined(); | ||
| expect(errorEvent.debug_meta?.images).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| type: 'wasm', | ||
| code_file: expect.stringMatching(/simple\.wasm$/), | ||
| code_id: '0ba020cdd2444f7eafdd25999a8e9010', | ||
| debug_id: '0ba020cdd2444f7eafdd25999a8e90100', | ||
| }), | ||
| ]), | ||
| ); | ||
|
|
||
| const wasmFrame = errorEvent.exception?.values?.[0]?.stacktrace?.frames?.find( | ||
| frame => frame.filename && frame.filename.includes('simple.wasm'), | ||
| ); | ||
|
|
||
| if (wasmFrame) { | ||
| expect(wasmFrame.platform).toBe('native'); | ||
| expect(wasmFrame.instruction_addr).toBeDefined(); | ||
| expect(wasmFrame.addr_mode).toMatch(/^rel:\d+$/); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| sentryTest( | ||
| 'WASM frames from worker should be recognized as first-party when applicationKey is configured', | ||
| async ({ getLocalTestUrl, page, browserName }) => { | ||
| if (shouldSkipWASMTests(browserName)) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| await page.route('**/simple.wasm', route => { | ||
| const wasmModule = fs.readFileSync(path.resolve(__dirname, '../simple.wasm')); | ||
| return route.fulfill({ | ||
| status: 200, | ||
| body: wasmModule, | ||
| headers: { | ||
| 'Content-Type': 'application/wasm', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| await page.route('**/worker.js', route => { | ||
| return route.fulfill({ | ||
| path: `${__dirname}/assets/worker.js`, | ||
| }); | ||
| }); | ||
|
|
||
| const errorEventPromise = waitForErrorRequest(page, e => { | ||
| return e.exception?.values?.[0]?.value === 'WASM error from worker'; | ||
| }); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| await page.waitForFunction(() => window.wasmWorker !== undefined); | ||
|
|
||
| await page.evaluate(() => { | ||
| window.triggerWasmError(); | ||
| }); | ||
|
|
||
| const errorEvent = envelopeRequestParser(await errorEventPromise); | ||
|
|
||
| const wasmFrame = errorEvent.exception?.values?.[0]?.stacktrace?.frames?.find( | ||
| frame => frame.filename && frame.filename.includes('simple.wasm'), | ||
| ); | ||
|
|
||
| if (wasmFrame) { | ||
| expect(wasmFrame.module_metadata).toEqual( | ||
| expect.objectContaining({ | ||
| '_sentryBundlerPluginAppKey:wasm-worker-app': true, | ||
| }), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test conditionals may cause silent assertion skipping
Medium Severity · Bugbot Rules
The integration tests use
if (wasmFrame)conditionals that silently skip assertions when the frame isn't found. The second test ("WASM frames from worker should be recognized as first-party when applicationKey is configured") has no assertions at all before the conditional, so ifwasmFrameis undefined, the entire test passes without verifying anything. This violates the rule about conditionals in tests: "Flag usage of conditionals in one test and recommend splitting up the test for the different paths." The existing WASM test insuites/wasm/test.tsusesexpect.arrayContainingto directly assert on frames, which properly fails if the expected frame is missing.Additional Locations (1)
dev-packages/browser-integration-tests/suites/wasm/webWorker/test.ts#L77-L82