Skip to content

Commit 0626bac

Browse files
timfishanonrig
authored andcommitted
fix(node): Anr should not block exit (#10035)
1 parent 0a1d62f commit 0626bac

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Sentry = require('@sentry/node');
2+
3+
function configureSentry() {
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
debug: true,
8+
autoSessionTracking: false,
9+
integrations: [new Sentry.Integrations.Anr({ captureStackTrace: true })],
10+
});
11+
}
12+
13+
async function main() {
14+
configureSentry();
15+
await new Promise(resolve => setTimeout(resolve, 1000));
16+
process.exit(0);
17+
}
18+
19+
main();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ conditionalTest({ min: 16 })('should report ANR when event loop blocked', () =>
115115
});
116116
});
117117

118+
test('can exit', done => {
119+
const testScriptPath = path.resolve(__dirname, 'should-exit.js');
120+
let hasClosed = false;
121+
122+
setTimeout(() => {
123+
expect(hasClosed).toBe(true);
124+
done();
125+
}, 5_000);
126+
127+
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
128+
hasClosed = true;
129+
});
130+
});
131+
118132
test('With session', done => {
119133
expect.assertions(9);
120134

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
123123
// Ensure this thread can't block app exit
124124
worker.unref();
125125

126+
process.on('exit', () => {
127+
worker.terminate();
128+
});
129+
126130
const timer = setInterval(() => {
127131
try {
128132
const currentSession = getCurrentScope().getSession();
@@ -135,6 +139,8 @@ async function _startWorker(client: NodeClient, _options: Partial<Options>): Pro
135139
//
136140
}
137141
}, options.pollInterval);
142+
// Timer should not block exit
143+
timer.unref();
138144

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

0 commit comments

Comments
 (0)