Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions apps/ui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,17 @@ app.on('window-all-closed', () => {
logger.error('Failed to kill server process:', (error as Error).message);
}
} else {
serverProcess.kill('SIGTERM');
// Unix/Linux: kill entire process tree
try {
execSync(`pkill -P ${serverProcess.pid}`, { stdio: 'ignore' });
} catch {
// pkill returns non-zero if no processes found, ignore
}
try {
process.kill(serverProcess.pid, 'SIGKILL');
} catch {
// Process may already be dead
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This logic for terminating the server process tree is nearly identical to the logic in the before-quit event handler on lines 815-828. Duplicating this code makes it harder to maintain and increases the risk of introducing inconsistencies in the future.

To improve this, I recommend extracting the entire server termination logic (for both Windows and Unix-like systems) into a single helper function. This function can then be called from both window-all-closed and before-quit handlers.

Here's an example of how you could structure it:

function cleanupServerProcess(reason: 'window-closed' | 'quitting'): void {
  if (!serverProcess || !serverProcess.pid) {
    return;
  }

  const logMessage = reason === 'window-closed' 
    ? 'All windows closed, stopping server...' 
    : 'Stopping server...';
  logger.info(logMessage);

  if (process.platform === 'win32') {
    try {
      execSync(`taskkill /f /t /pid ${serverProcess.pid}`, { stdio: 'ignore' });
    } catch (error) {
      logger.error('Failed to kill server process:', (error as Error).message);
    }
  } else {
    // Unix/macOS: kill entire process tree
    try {
      execSync(`pkill -P ${serverProcess.pid}`, { stdio: 'ignore' });
    } catch {
      // pkill returns non-zero if no processes found, ignore
    }
    try {
      process.kill(serverProcess.pid, 'SIGKILL');
    } catch {
      // Process may already be dead
    }
  }
  serverProcess = null;
}

// Then call it from the handlers:
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    cleanupServerProcess('window-closed');
    // ... rest of the handler
  }
});

app.on('before-quit', () => {
  cleanupServerProcess('quitting');
  // ... rest of the handler
});

serverProcess = null;
}
Expand Down Expand Up @@ -802,7 +812,20 @@ app.on('before-quit', () => {
logger.error('Failed to kill server process:', (error as Error).message);
}
} else {
serverProcess.kill('SIGTERM');
// Unix/macOS: kill entire process tree using pkill
// SIGTERM first, then SIGKILL if needed
try {
// Kill all child processes of the server
execSync(`pkill -P ${serverProcess.pid}`, { stdio: 'ignore' });
} catch {
// pkill returns non-zero if no processes found, ignore
}
try {
// Kill the server process itself with SIGKILL for reliability
process.kill(serverProcess.pid, 'SIGKILL');
} catch {
// Process may already be dead
}
}
serverProcess = null;
}
Expand Down