Skip to content

Commit

Permalink
fix(node): Anr doesn't block exit (#10064)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jan 4, 2024
1 parent 99f39b5 commit 33348ec
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Sentry = require('@sentry/node');

function configureSentry() {
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
debug: true,
autoSessionTracking: false,
integrations: [new Sentry.Integrations.Anr({ captureStackTrace: true })],
});
}

async function main() {
configureSentry();
await new Promise(resolve => setTimeout(resolve, 1000));
process.exit(0);
}

main();
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function configureSentry() {
async function main() {
configureSentry();
await new Promise(resolve => setTimeout(resolve, 1000));
process.exit(0);
}

main();
16 changes: 15 additions & 1 deletion dev-packages/node-integration-tests/suites/anr/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () =>
});
});

test('can exit', done => {
test('should exit', done => {
const testScriptPath = path.resolve(__dirname, 'should-exit.js');
let hasClosed = false;

Expand All @@ -129,6 +129,20 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () =>
});
});

test('should exit forced', done => {
const testScriptPath = path.resolve(__dirname, 'should-exit-forced.js');
let hasClosed = false;

setTimeout(() => {
expect(hasClosed).toBe(true);
done();
}, 5_000);

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, () => {
hasClosed = true;
});
});

test('With session', done => {
expect.assertions(9);

Expand Down
5 changes: 3 additions & 2 deletions packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
const worker = new Worker(new URL(`data:application/javascript;base64,${base64WorkerScript}`), {
workerData: options,
});
// Ensure this thread can't block app exit
worker.unref();

process.on('exit', () => {
worker.terminate();
Expand Down Expand Up @@ -160,4 +158,7 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
clearInterval(timer);
log('ANR worker exit', code);
});

// Ensure this thread can't block app exit
worker.unref();
}

0 comments on commit 33348ec

Please sign in to comment.