Skip to content

Commit bc7e87f

Browse files
authored
fix(v8/node): Correctly resolve debug IDs for ANR events with custom appRoot (#14823)
A backport of #14822
1 parent e7b3530 commit bc7e87f

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as assert from 'assert';
2+
import * as crypto from 'crypto';
3+
import * as path from 'path';
4+
import * as url from 'url';
5+
6+
import * as Sentry from '@sentry/node';
7+
8+
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
9+
10+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
11+
12+
setTimeout(() => {
13+
process.exit();
14+
}, 10000);
15+
16+
Sentry.init({
17+
dsn: process.env.SENTRY_DSN,
18+
release: '1.0',
19+
autoSessionTracking: false,
20+
integrations: [Sentry.anrIntegration({ captureStackTrace: true, anrThreshold: 100, appRootPath: __dirname })],
21+
});
22+
23+
Sentry.setUser({ email: '[email protected]' });
24+
Sentry.addBreadcrumb({ message: 'important message!' });
25+
26+
function longWork() {
27+
for (let i = 0; i < 20; i++) {
28+
const salt = crypto.randomBytes(128).toString('base64');
29+
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
30+
assert.ok(hash);
31+
}
32+
}
33+
34+
setTimeout(() => {
35+
longWork();
36+
}, 1000);

dev-packages/node-integration-tests/suites/anr/test.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ const ANR_EVENT = {
3131
mechanism: { type: 'ANR' },
3232
stacktrace: {
3333
frames: expect.arrayContaining([
34-
{
34+
expect.objectContaining({
3535
colno: expect.any(Number),
3636
lineno: expect.any(Number),
3737
filename: expect.any(String),
3838
function: '?',
3939
in_app: true,
40-
},
41-
{
40+
}),
41+
expect.objectContaining({
4242
colno: expect.any(Number),
4343
lineno: expect.any(Number),
4444
filename: expect.any(String),
4545
function: 'longWork',
4646
in_app: true,
47-
},
47+
}),
4848
]),
4949
},
5050
},
@@ -123,6 +123,26 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () =>
123123
.start(done);
124124
});
125125

126+
test('Custom appRootPath', done => {
127+
const ANR_EVENT_WITH_SPECIFIC_DEBUG_META: Event = {
128+
...ANR_EVENT_WITH_SCOPE,
129+
debug_meta: {
130+
images: [
131+
{
132+
type: 'sourcemap',
133+
debug_id: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa',
134+
code_file: 'app:///app-path.mjs',
135+
},
136+
],
137+
},
138+
};
139+
140+
createRunner(__dirname, 'app-path.mjs')
141+
.withMockSentryServer()
142+
.expect({ event: ANR_EVENT_WITH_SPECIFIC_DEBUG_META })
143+
.start(done);
144+
});
145+
126146
test('multiple events via maxAnrEvents', done => {
127147
createRunner(__dirname, 'basic-multiple.mjs')
128148
.withMockSentryServer()

packages/node/src/integrations/anr/worker.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,27 @@ function applyDebugMeta(event: Event): void {
9191
return;
9292
}
9393

94+
const normalisedDebugImages = options.appRootPath ? {} : mainDebugImages;
95+
if (options.appRootPath) {
96+
for (const [path, debugId] of Object.entries(mainDebugImages)) {
97+
normalisedDebugImages[normalizeUrlToBase(path, options.appRootPath)] = debugId;
98+
}
99+
}
100+
94101
const filenameToDebugId = new Map<string, string>();
95102

96103
for (const exception of event.exception?.values || []) {
97104
for (const frame of exception.stacktrace?.frames || []) {
98105
const filename = frame.abs_path || frame.filename;
99-
if (filename && mainDebugImages[filename]) {
100-
filenameToDebugId.set(filename, mainDebugImages[filename] as string);
106+
if (filename && normalisedDebugImages[filename]) {
107+
filenameToDebugId.set(filename, normalisedDebugImages[filename] as string);
101108
}
102109
}
103110
}
104111

105112
if (filenameToDebugId.size > 0) {
106113
const images: DebugImage[] = [];
107-
for (const [filename, debug_id] of filenameToDebugId.entries()) {
108-
const code_file = options.appRootPath ? normalizeUrlToBase(filename, options.appRootPath) : filename;
109-
114+
for (const [code_file, debug_id] of filenameToDebugId.entries()) {
110115
images.push({
111116
type: 'sourcemap',
112117
code_file,

0 commit comments

Comments
 (0)