From 96201c42c1dc8a403023bf1041516398141a075c Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Mon, 13 Nov 2023 16:28:17 -0500 Subject: [PATCH] Improve error handling and shutdown handling for ANR If communicating via debugger to generate a stack trace fails, then fall back to a regular event, instead of throwing an uncaught exception. If the parent process exits, exit the ANR child. Fixes #9546 --- packages/node/src/anr/index.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/node/src/anr/index.ts b/packages/node/src/anr/index.ts index caa384b3928f..549b962d4b70 100644 --- a/packages/node/src/anr/index.ts +++ b/packages/node/src/anr/index.ts @@ -215,15 +215,21 @@ function handleChildProcess(options: Options): void { async function watchdogTimeout(): Promise { log('Watchdog timeout'); - const pauseAndCapture = await debuggerPause; - - if (pauseAndCapture) { - log('Pausing debugger to capture stack trace'); - pauseAndCapture(); - } else { - log('Capturing event'); - sendAnrEvent(); + + try { + const pauseAndCapture = await debuggerPause; + + if (pauseAndCapture) { + log('Pausing debugger to capture stack trace'); + pauseAndCapture(); + return; + } + } catch (_) { + // ignore } + + log('Capturing event'); + sendAnrEvent(); } const { poll } = watchdogTimer(createHrTimer, options.pollInterval, options.anrThreshold, watchdogTimeout); @@ -234,6 +240,10 @@ function handleChildProcess(options: Options): void { } poll(); }); + process.on('disconnect', () => { + // Parent process has exited. + process.exit(); + }); } /**