From 85a644621455874c6f87afbdf3989b4b52218308 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sat, 28 Sep 2024 02:44:41 +0000 Subject: [PATCH] Avoid creating multiple clients per project If a file is opened within a project while a client for that project already exists, we create a new client to replace the old one in the map, without disposing of the old client. Fixes: 1. If createClient replaces a client in the map, always dispose of the replaced client to avoid leaking clients. 2. Remove now redundant calls to disposeClient which are made just before calls to createClient for the same project. 3. Don't call createClient at all when a document is opened in a project if a client already exists for the given project. --- src/clangd-context.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/clangd-context.ts b/src/clangd-context.ts index 62e391ec..725b50e1 100644 --- a/src/clangd-context.ts +++ b/src/clangd-context.ts @@ -110,11 +110,9 @@ export class ClangdContext implements vscode.Disposable { const oldActiveProject = this._activeProjectOverride; this._activeProjectOverride = project; if (oldActiveProject) { - this.disposeClient(oldActiveProject.id); this.createClient(oldActiveProject, this.serverOptions!) } if (project) { - this.disposeClient(project.id); this.createClient(project, this.serverOptions!) } } @@ -162,7 +160,6 @@ export class ClangdContext implements vscode.Disposable { this.clients.get(this._activeProjectOverride.id)?.dispose(); this._activeProjectOverride = undefined; } else if (change.updated?.includes(this._activeProjectOverride)) { - this.clients.get(this._activeProjectOverride.id)?.dispose(); this.createClient(this._activeProjectOverride, this.serverOptions!); } }) @@ -317,7 +314,7 @@ export class ClangdContext implements vscode.Disposable { } this.disposeClient(activeProject?.id ?? fallbackProjectId); this.createClient(activeProject, serverOptions); - } else { + } else if (!this.clients.has(project.id)) { this.createClient(project, serverOptions); } } @@ -331,7 +328,9 @@ export class ClangdContext implements vscode.Disposable { // max restart count config.get('restartAfterCrash') ? /*default*/ 4 : 0); client.registerFeature(new EnableEditsNearCursorFeature); - this.clients.set(project?.id ?? fallbackProjectId, client); + let client_key = project?.id ?? fallbackProjectId; + this.clients.get(client_key)?.dispose(); + this.clients.set(client_key, client); client.start() return client; }