From 2e3061472e1fd1f407d481bd1ee015c42012b5a0 Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Tue, 2 Jan 2024 16:54:20 -0500 Subject: [PATCH 1/2] fix(node): Handle inspector already open If the inspector is already running, then ANR should be able to (re)use that, instead of opening it itself. Fixes #10024 --- packages/node/src/integrations/anr/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/node/src/integrations/anr/index.ts b/packages/node/src/integrations/anr/index.ts index cf1f1cd6cab7..70c20962fca2 100644 --- a/packages/node/src/integrations/anr/index.ts +++ b/packages/node/src/integrations/anr/index.ts @@ -111,7 +111,9 @@ export class Anr implements Integration { if (options.captureStackTrace) { // eslint-disable-next-line @typescript-eslint/no-var-requires const inspector: InspectorApi = require('inspector'); - inspector.open(0); + if (!inspector.url()) { + inspector.open(0); + } } const { Worker } = getWorkerThreads(); From 463a13c9e28af184caac0f3631af918262d54c24 Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Tue, 2 Jan 2024 18:13:05 -0500 Subject: [PATCH 2/2] Add integration test --- .../node-integration-tests/suites/anr/test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/node-integration-tests/suites/anr/test.ts b/packages/node-integration-tests/suites/anr/test.ts index a070f611a0ab..011a0371a5d9 100644 --- a/packages/node-integration-tests/suites/anr/test.ts +++ b/packages/node-integration-tests/suites/anr/test.ts @@ -95,6 +95,26 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () => }); }); + test('With --inspect', done => { + expect.assertions(7); + + const testScriptPath = path.resolve(__dirname, 'basic.js'); + + childProcess.exec(`node --inspect ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => { + const [event] = parseJsonLines<[Event]>(stdout, 1); + + expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' }); + expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding'); + expect(event.exception?.values?.[0].value).toEqual('Application Not Responding for at least 200 ms'); + expect(event.exception?.values?.[0].stacktrace?.frames?.length).toBeGreaterThan(4); + + expect(event.exception?.values?.[0].stacktrace?.frames?.[2].function).toEqual('?'); + expect(event.exception?.values?.[0].stacktrace?.frames?.[3].function).toEqual('longWork'); + + done(); + }); + }); + test('With session', done => { expect.assertions(9);