From 067a1c4b7000a30fed4e7d9e467b594685efc1b3 Mon Sep 17 00:00:00 2001 From: Angela P Wen Date: Tue, 14 May 2024 15:56:18 +0000 Subject: [PATCH] Handle exit and error events for query server child processes --- .../src/query-server/query-server-client.ts | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/extensions/ql-vscode/src/query-server/query-server-client.ts b/extensions/ql-vscode/src/query-server/query-server-client.ts index 2fb46471c55..c342e3b4996 100644 --- a/extensions/ql-vscode/src/query-server/query-server-client.ts +++ b/extensions/ql-vscode/src/query-server/query-server-client.ts @@ -240,8 +240,32 @@ export class QueryServerClient extends DisposableObject { this.nextCallback = 0; this.nextProgress = 0; this.progressCallbacks = {}; - child.on("close", () => { - this.restartQueryServerOnFailure(); + + // 'exit' may or may not fire after 'error' event, so we want to guard against restarting the + // query server twice if both events fire. + let wasExitOrErrorHandled = false; + child.on("error", (err: Error) => { + if (!wasExitOrErrorHandled) { + void this.logger.log(`Query server terminated with error: ${err}.`); + this.restartQueryServerOnFailure(); + wasExitOrErrorHandled = true; + } + }); + child.on("exit", (code: number, signal: string) => { + if (!wasExitOrErrorHandled) { + if (code !== null) { + void this.logger.log( + `Query server terminated with exit code: ${code}.`, + ); + } + if (signal !== null) { + void this.logger.log( + `Query server terminated due to receipt of signal: ${signal}.`, + ); + } + this.restartQueryServerOnFailure(); + wasExitOrErrorHandled = true; + } }); }