feat: add cleanup for orphaned MCP server processes on startup

- Implemented a new method `cleanupOrphanedProcesses` to identify and terminate orphaned `uvx` processes from previous sessions.
- Integrated the cleanup method into the `start` process of the WorkerService to ensure a clean environment at startup.
- Added logging for process cleanup actions and handled potential errors gracefully without failing the service startup.
This commit is contained in:
Alex Newman
2025-11-16 22:36:39 -05:00
parent 20d45006c0
commit 02fef487e7
2 changed files with 58 additions and 22 deletions
File diff suppressed because one or more lines are too long
+35
View File
@@ -180,10 +180,45 @@ export class WorkerService {
this.app.get('/api/search/help', this.handleSearchHelp.bind(this));
}
/**
* Cleanup orphaned MCP server processes (uvx/chroma) from previous sessions
*/
private async cleanupOrphanedProcesses(): Promise<void> {
try {
const { execSync } = await import('child_process');
// Find orphaned uvx processes (which spawn chroma servers)
try {
const processes = execSync('pgrep -fl uvx', { encoding: 'utf-8', stdio: 'pipe' }).trim();
if (processes) {
const processCount = processes.split('\n').length;
logger.info('WORKER', 'Cleaning up orphaned MCP processes', { count: processCount });
// Kill the processes
execSync('pkill -f uvx', { stdio: 'pipe' });
logger.success('WORKER', `Cleaned up ${processCount} orphaned MCP server processes`);
}
} catch (error: any) {
// pgrep returns exit code 1 if no processes found (not an error)
if (error.status === 1) {
logger.debug('WORKER', 'No orphaned MCP processes to clean up');
} else {
throw error;
}
}
} catch (error) {
// Don't fail startup if cleanup fails
logger.warn('WORKER', 'Failed to cleanup orphaned processes (non-fatal)', {}, error as Error);
}
}
/**
* Start the worker service
*/
async start(): Promise<void> {
// Cleanup orphaned processes from previous sessions
await this.cleanupOrphanedProcesses();
// Initialize database (once, stays open)
await this.dbManager.initialize();