fix(backend): only kill processes started by TUI
Track PIDs of backends started by /backend command and only kill those specific PIDs. Previous implementation used pkill which would kill all Ollama/llama-server processes including those started by the user or systemd services. Now we only terminate processes we started.
This commit is contained in:
@@ -52,6 +52,7 @@ export class MinimalTui {
|
|||||||
private totalUsage: TokenUsage = { inputTokens: 0, outputTokens: 0 };
|
private totalUsage: TokenUsage = { inputTokens: 0, outputTokens: 0 };
|
||||||
private currentHint = '';
|
private currentHint = '';
|
||||||
private lastLine = '';
|
private lastLine = '';
|
||||||
|
private backendPids: Map<string, number> = new Map();
|
||||||
|
|
||||||
constructor(private config: MinimalTuiConfig) {}
|
constructor(private config: MinimalTuiConfig) {}
|
||||||
|
|
||||||
@@ -322,24 +323,30 @@ export class MinimalTui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async stopBackend(provider: string): Promise<void> {
|
private async stopBackend(provider: string): Promise<void> {
|
||||||
try {
|
const pid = this.backendPids.get(provider);
|
||||||
const { spawn } = await import('child_process');
|
if (!pid) {
|
||||||
let processName: string;
|
return; // No tracked PID, process not started by us
|
||||||
switch (provider) {
|
|
||||||
case 'ollama':
|
|
||||||
processName = 'ollama';
|
|
||||||
break;
|
|
||||||
case 'llamacpp':
|
|
||||||
processName = 'llama-server';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
process.kill(pid, 'SIGTERM');
|
||||||
|
// Wait up to 2 seconds for graceful shutdown
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
spawn('pkill', [processName]).on('close', resolve);
|
const timeout = setTimeout(resolve, 2000);
|
||||||
|
const checkInterval = setInterval(() => {
|
||||||
|
try {
|
||||||
|
process.kill(pid, 0); // Check if process exists
|
||||||
|
} catch {
|
||||||
|
clearInterval(checkInterval);
|
||||||
|
clearTimeout(timeout);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
});
|
});
|
||||||
|
this.backendPids.delete(provider);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Ignore errors stopping backends
|
// Process already dead or permission error
|
||||||
|
this.backendPids.delete(provider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,18 +355,25 @@ export class MinimalTui {
|
|||||||
const { spawn } = await import('child_process');
|
const { spawn } = await import('child_process');
|
||||||
const args: string[] = [];
|
const args: string[] = [];
|
||||||
|
|
||||||
|
let proc: ReturnType<typeof spawn> | undefined;
|
||||||
|
|
||||||
switch (provider) {
|
switch (provider) {
|
||||||
case 'ollama':
|
case 'ollama':
|
||||||
spawn('ollama', ['serve'], { detached: true, stdio: 'ignore' }).unref();
|
proc = spawn('ollama', ['serve'], { detached: true, stdio: 'ignore' });
|
||||||
break;
|
break;
|
||||||
case 'llamacpp':
|
case 'llamacpp':
|
||||||
args.push('--model', config.model);
|
args.push('--model', config.model);
|
||||||
args.push('--port', new URL(config.endpoint ?? 'http://localhost:8080').port || '8080');
|
args.push('--port', new URL(config.endpoint ?? 'http://localhost:8080').port || '8080');
|
||||||
args.push('--host', '0.0.0.0');
|
args.push('--host', '0.0.0.0');
|
||||||
spawn('llama-server', args, { detached: true, stdio: 'ignore' }).unref();
|
proc = spawn('llama-server', args, { detached: true, stdio: 'ignore' });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (proc && proc.pid) {
|
||||||
|
proc.unref();
|
||||||
|
this.backendPids.set(provider, proc.pid);
|
||||||
|
}
|
||||||
|
|
||||||
// Wait briefly for the daemon to start
|
// Wait briefly for the daemon to start
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user