Skip to content

Commit bea7e69

Browse files
authored
Agent: don't kill entire process on uncaught exceptions (#3151)
Previously, the agent process would kill itself if the Node.js process encountered an uncaught exception because this is the default behavior for Node.js. This was problematic because most uncaught exceptions are innocent, they're typically non-critical code that runs in some un-awaited promise where we forget to wrap the code in try/catch. This PR adds an uncaught error handler that logs the error (and stack trace) to stderr instead. For clients like JetBrains, we automatically redirect stderr to `idea.log` making it easy to troubleshoot the problem. This change should be a big stability boost since tiny programming mistakes no longer take down the entire agent process. I am embarrassed we didn't do this sooner, but better late than never. ## Test plan * Manually add `setTimeout(() => {throw new Error('boom')}, 100)` somewhere in the code * Comment out the uncaught exception handler * Run `pnpm run test agent/src/index.test.ts` * Confirm that tests fail * Uncomment exception handler * Confirm all tests pass <!-- Required. See https://sourcegraph.com/docs/dev/background-information/testing_principles. -->
1 parent 7d0bc74 commit bea7e69

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

agent/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ console.log = console.error
1010
// stdout/stdin to communicate with the agent client.
1111
const rootCommand: Command = require('./cli/root').rootCommand
1212

13+
process.on('uncaughtException', e => {
14+
// By default, an uncaught exception will take down the entire process.
15+
// Instead of taking down the process, we just report it to stderr and move
16+
// on. In almost all cases, an uncaught exception is an innocent error that
17+
// does not have to take down the process. For example, if a telemetry
18+
// request fails, then it's totally fine to just report it here with a stack
19+
// trace so we can look into it and fix it.
20+
console.error('Uncaught exception:', e)
21+
})
22+
1323
const args = process.argv.slice(2)
1424
const { operands } = rootCommand.parseOptions(args)
1525
if (operands.length === 0) {

0 commit comments

Comments
 (0)