fix: harden windows gateway liveness (#658)

This commit is contained in:
luSkyl
2026-05-12 20:44:34 +08:00
committed by GitHub
parent b9b99042a0
commit c987448f01
3 changed files with 201 additions and 17 deletions
@@ -200,15 +200,28 @@ export class GatewayManager {
}
}
/** profile gateway.pid 文件读取 PIDJSON 格式 { "pid": 12345 } */
/** Read a profile gateway PID, falling back to runtime state when gateway.pid is missing. */
private readPidFile(name: string): number | null {
const pidPath = join(this.profileDir(name), 'gateway.pid')
if (!existsSync(pidPath)) return null
const profilePath = this.profileDir(name)
const pidPath = join(profilePath, 'gateway.pid')
try {
const content = readFileSync(pidPath, 'utf-8').trim()
if (existsSync(pidPath)) {
const content = readFileSync(pidPath, 'utf-8').trim()
const data = JSON.parse(content)
return typeof data.pid === 'number' ? data.pid : parseInt(data.pid, 10) || null
}
} catch {}
const statePath = join(profilePath, 'gateway_state.json')
if (!existsSync(statePath)) return null
try {
const content = readFileSync(statePath, 'utf-8').trim()
const data = JSON.parse(content)
return typeof data.pid === 'number' ? data.pid : parseInt(data.pid, 10) || null
const pid = typeof data.pid === 'number' ? data.pid : parseInt(data.pid, 10) || null
const state = data?.gateway_state
return pid && (state === 'running' || state === 'starting') ? pid : null
} catch {
return null
}
@@ -218,13 +231,13 @@ export class GatewayManager {
// 进程 & 端口检测工具
// ============================
/** 检查进程是否存活(发送信号 0,不实际杀死进程) */
/** Check process liveness without sending a terminating signal. */
private isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0)
return true
} catch {
return false
} catch (err: any) {
return err?.code === 'EPERM'
}
}