From 1cb6692295b627c44bafdf1ce729e2d9dd7736a6 Mon Sep 17 00:00:00 2001 From: Carlos Gilberto Avalos Date: Tue, 27 Jun 2023 19:33:44 -0400 Subject: [PATCH] Deactivate VSCode extension asynchronously When using HLSL Tools on MacOS, orphaned ShaderTools.LanguageServer processes will accumulate, wasting memory. This happens after closing and opening VSCode repeatedly. This does not appear to happen on Windows in my experience. https://github.com/microsoft/vscode/issues/35196#issuecomment-334399348 The above GitHub issue details other developers' experiences with the same problem. The solution presented by dbaeumer was to return a `Promise` from an exported `deactivate()`. https://code.visualstudio.com/api/references/activation-events#Start-up This is confirmed by the above VSCode docs. Extensions with asynchronous cleanup processes must return a `Promise` from their `deactivate()`. As `LanguageServerClient.stop()` returns a `Promise`, we are obligated to return it when deactivating. --- src/ShaderTools.VSCode/src/main.ts | 4 ++++ src/ShaderTools.VSCode/src/session.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ShaderTools.VSCode/src/main.ts b/src/ShaderTools.VSCode/src/main.ts index 151064a1..a208b7fa 100644 --- a/src/ShaderTools.VSCode/src/main.ts +++ b/src/ShaderTools.VSCode/src/main.ts @@ -8,4 +8,8 @@ var sessionManager: SessionManager = undefined; export function activate(context: vscode.ExtensionContext): void { context.subscriptions.push(sessionManager = new SessionManager()); sessionManager.start(); +} + +export function deactivate(): Promise { + return sessionManager.stop(); } \ No newline at end of file diff --git a/src/ShaderTools.VSCode/src/session.ts b/src/ShaderTools.VSCode/src/session.ts index 9836c679..f464ab15 100644 --- a/src/ShaderTools.VSCode/src/session.ts +++ b/src/ShaderTools.VSCode/src/session.ts @@ -35,7 +35,7 @@ export class SessionManager { this.startEditorServices(); } - public stop() { + public stop(): Promise { if (this.sessionStatus === SessionStatus.Failed) { // Before moving further, clear out the client and process if // the process is already dead (i.e. it crashed) @@ -44,13 +44,17 @@ export class SessionManager { this.sessionStatus = SessionStatus.Stopping; + var promise = Promise.resolve(); + // Close the language server client if (this.languageServerClient !== undefined) { - this.languageServerClient.stop(); + promise = this.languageServerClient.stop(); this.languageServerClient = undefined; } this.sessionStatus = SessionStatus.NotStarted; + + return promise; } public dispose() : void {