Skip to content

Commit

Permalink
Deactivate VSCode extension asynchronously
Browse files Browse the repository at this point in the history
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.

microsoft/vscode#35196 (comment)

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.
  • Loading branch information
cgavalos committed Jun 28, 2023
1 parent 5468714 commit 1cb6692
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/ShaderTools.VSCode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
return sessionManager.stop();
}
8 changes: 6 additions & 2 deletions src/ShaderTools.VSCode/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class SessionManager {
this.startEditorServices();
}

public stop() {
public stop(): Promise<void> {
if (this.sessionStatus === SessionStatus.Failed) {
// Before moving further, clear out the client and process if
// the process is already dead (i.e. it crashed)
Expand All @@ -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 {
Expand Down

0 comments on commit 1cb6692

Please sign in to comment.