Skip to content

Commit

Permalink
fix(node): Anr should not block exit (#10035)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jan 3, 2024
1 parent 651ef4a commit 25356d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dev-packages/node-integration-tests/suites/anr/should-exit.js
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();
14 changes: 14 additions & 0 deletions dev-packages/node-integration-tests/suites/anr/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () =>
});
});

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

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

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

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

Expand Down
6 changes: 6 additions & 0 deletions packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
// Ensure this thread can't block app exit
worker.unref();

process.on('exit', () => {
worker.terminate();
});

const timer = setInterval(() => {
try {
const currentSession = getCurrentScope().getSession();
Expand All @@ -135,6 +139,8 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
//
}
}, options.pollInterval);
// Timer should not block exit
timer.unref();

worker.on('message', (msg: string) => {
if (msg === 'session-ended') {
Expand Down

0 comments on commit 25356d2

Please sign in to comment.