kill old processes on boot

This commit is contained in:
Chris Wanstrath 2026-02-13 09:59:20 -08:00
parent 2f4d4f5c19
commit c10ebe3c98
2 changed files with 22 additions and 2 deletions

View File

@ -96,7 +96,8 @@ export function readLogs(appName: string, date?: string, tail?: number): string[
return lines
}
export function initApps() {
export async function initApps() {
await killStaleProcesses()
initPortPool()
setupShutdownHandlers()
rotateLogs()
@ -466,6 +467,25 @@ function handleHealthCheckFailure(app: App) {
}
}
async function killStaleProcesses() {
const result = Bun.spawnSync(['lsof', '-ti', `:${MIN_PORT - 1}-${MAX_PORT}`])
const output = result.stdout.toString().trim()
if (!output) return
const pids = output.split('\n').map(Number).filter(pid => pid && pid !== process.pid)
if (pids.length === 0) return
hostLog(`Found ${pids.length} stale process(es) on ports ${MIN_PORT - 1}-${MAX_PORT}`)
for (const pid of pids) {
try {
process.kill(pid, 'SIGKILL')
hostLog(`Killed stale process ${pid}`)
} catch {
// Process already gone
}
}
}
function initPortPool() {
_availablePorts.length = 0
for (let port = MIN_PORT; port <= MAX_PORT; port++) {

View File

@ -53,7 +53,7 @@ app.all('/api/tools/:tool/:path{.+}', async c => {
})
})
initApps()
await initApps()
export default {
...app.defaults,