Skip to content

Commit b4e8382

Browse files
committed
fix: use background executor where it can be and improve cleanup
1 parent 38f7342 commit b4e8382

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/cm/lsp/serverLauncher.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ function getExecutor(): Executor {
3131
return executor;
3232
}
3333

34+
/**
35+
* Get the background executor
36+
*/
37+
function getBackgroundExecutor(): Executor {
38+
const executor = getExecutor();
39+
return executor.BackgroundExecutor ?? executor;
40+
}
41+
3442
function joinCommand(command: string, args: string[] = []): string {
3543
if (!Array.isArray(args)) return command;
3644
return [command, ...args].join(" ");
@@ -42,7 +50,18 @@ function wrapShellCommand(command: string): string {
4250
return `sh -lc "set -e; ${escaped}"`;
4351
}
4452

45-
async function runCommand(command: string): Promise<string> {
53+
/**
54+
* Run a quick shell command using the background executor.
55+
*/
56+
async function runQuickCommand(command: string): Promise<string> {
57+
const wrapped = wrapShellCommand(command);
58+
return getBackgroundExecutor().execute(wrapped, true);
59+
}
60+
61+
/**
62+
* Run a shell command using the foreground executor
63+
*/
64+
async function runForegroundCommand(command: string): Promise<string> {
4665
const wrapped = wrapShellCommand(command);
4766
return getExecutor().execute(wrapped, true);
4867
}
@@ -396,7 +415,7 @@ async function performInstallCheck(
396415
): Promise<boolean> {
397416
try {
398417
if (launcher.checkCommand) {
399-
await runCommand(launcher.checkCommand);
418+
await runQuickCommand(launcher.checkCommand);
400419
}
401420
checkedCommands.set(cacheKey, STATUS_PRESENT);
402421
return true;
@@ -434,7 +453,7 @@ async function performInstallCheck(
434453
`Installing ${server.label}...`,
435454
);
436455
loadingDialog.show();
437-
await runCommand(install.command);
456+
await runForegroundCommand(install.command);
438457
toast(`${server.label} installed`);
439458
checkedCommands.set(cacheKey, STATUS_PRESENT);
440459
return true;
@@ -663,18 +682,26 @@ export async function ensureServerRunning(
663682
export function stopManagedServer(serverId: string): void {
664683
const entry = managedServers.get(serverId);
665684
if (!entry) return;
666-
getExecutor()
667-
.stop(entry.uuid)
668-
.catch((error: Error) => {
669-
console.warn(`Failed to stop language server ${serverId}`, error);
670-
});
685+
const executor = getExecutor();
686+
executor.stop(entry.uuid).catch((error: Error) => {
687+
console.warn(`Failed to stop language server ${serverId}`, error);
688+
});
671689
managedServers.delete(serverId);
672690
announcedServers.delete(serverId);
691+
692+
// Stop foreground service when all servers are stopped
693+
if (managedServers.size === 0) {
694+
executor.stopService().catch(() => {});
695+
}
673696
}
674697

675698
export function resetManagedServers(): void {
676699
for (const id of Array.from(managedServers.keys())) {
677700
stopManagedServer(id);
678701
}
679702
managedServers.clear();
703+
// Ensure foreground service is stopped
704+
getExecutor()
705+
.stopService()
706+
.catch(() => {});
680707
}

src/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ interface Executor {
4848
write: (uuid: string, input: string) => Promise<void>;
4949
stop: (uuid: string) => Promise<void>;
5050
isRunning: (uuid: string) => Promise<boolean>;
51+
/** Move the executor service to the foreground (shows notification) */
52+
moveToForeground: () => Promise<void>;
53+
/** Move the executor service to the background (hides notification) */
54+
moveToBackground: () => Promise<void>;
55+
/** Stop the executor service completely */
56+
stopService: () => Promise<void>;
57+
/**
58+
* Background executor
59+
*/
60+
BackgroundExecutor: Executor;
5161
}
5262

5363
declare const Executor: Executor | undefined;

0 commit comments

Comments
 (0)