fix(server): gracefully stop verifier daemon

This commit is contained in:
Jinwoo-H
2026-08-09 23:49:31 -04:00
parent e857fb4570
commit 68b49fb4f4
3 changed files with 112 additions and 38 deletions
+3 -38
View File
@@ -1,10 +1,11 @@
import { execFileSync, spawn, type ChildProcess } from 'node:child_process'
import { existsSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync } from 'node:fs'
import { mkdtempSync, realpathSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { decodePairingOffer, type PairingOffer } from '../../src/shared/pairing'
import { sendRemoteRuntimeRequest } from '../../src/shared/remote-runtime-client'
import type { RuntimeStatus, RuntimeTerminalRead } from '../../src/shared/runtime-types'
import { stopNodeServerVerifierDaemons } from './node-server-verifier-daemon-cleanup'
type ReadyPayload = {
type: 'orca_server_ready'
@@ -70,7 +71,7 @@ async function main(): Promise<void> {
if (activeServer?.child.exitCode === null) {
await stopServer(activeServer).catch(() => activeServer?.child.kill('SIGKILL'))
}
await stopOwnedDaemon()
await stopNodeServerVerifierDaemons(dataPath)
rmSync(ownedRoot, { force: true, maxRetries: 20, recursive: true, retryDelay: 100 })
}
}
@@ -277,42 +278,6 @@ async function call<TResult>(
return response.result
}
async function stopOwnedDaemon(): Promise<void> {
const daemonPath = join(dataPath, 'daemon')
if (!existsSync(daemonPath)) {
return
}
const stoppedPids: number[] = []
for (const entry of readdirSync(daemonPath)) {
if (!/^daemon-v\d+\.pid$/.test(entry)) {
continue
}
try {
const record = JSON.parse(readFileSync(join(daemonPath, entry), 'utf8')) as { pid?: unknown }
if (typeof record.pid === 'number' && Number.isInteger(record.pid)) {
process.kill(record.pid, 'SIGTERM')
stoppedPids.push(record.pid)
}
} catch {
// The isolated verifier directory can contain a daemon that already retired.
}
}
await Promise.all(stoppedPids.map(waitForProcessExit))
}
async function waitForProcessExit(pid: number): Promise<void> {
const deadline = Date.now() + 10_000
while (Date.now() < deadline) {
try {
process.kill(pid, 0)
} catch {
return
}
await new Promise((resolveWait) => setTimeout(resolveWait, 100))
}
throw new Error(`terminal daemon ${pid} did not exit after SIGTERM`)
}
function assert(condition: unknown, message: string): asserts condition {
if (!condition) {
throw new Error(message)