Skip to content

fix(node): Anr doesn't block exit #10064

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

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}