fix: preserve gateways across dev restarts (#662)

This commit is contained in:
ekko
2026-05-12 21:35:17 +08:00
committed by GitHub
parent ebb166abee
commit 5bd1475a83
2 changed files with 338 additions and 3 deletions
@@ -147,6 +147,11 @@ function isLocalHost(host: string): boolean {
return ['127.0.0.1', 'localhost', '::1', '[::1]', '0.0.0.0'].includes(host)
}
function shouldDetachGatewayProcess(): boolean {
const override = process.env.HERMES_WEB_UI_STOP_GATEWAYS_ON_SHUTDOWN?.trim().toLowerCase()
return override === '0' || override === 'false'
}
// ============================
// GatewayManager
// ============================
@@ -560,18 +565,22 @@ export class GatewayManager {
}
}
// 所有平台统一使用 run 模式:子进程跟随父进程生命周期
// 所有平台统一使用 run 模式dev/nodemon 可通过 env 保留 gateway 进程。
return new Promise((resolve, reject) => {
const env = { ...process.env, HERMES_HOME: hermesHome }
const detachGateway = shouldDetachGatewayProcess()
const child = spawn(HERMES_BIN, ['gateway', 'run', '--replace'], {
stdio: 'ignore',
detached: detachGateway,
windowsHide: true,
env,
})
// 不使用 detached 和 unref,让子进程跟随父进程生命周期
if (detachGateway) {
child.unref()
}
const pid = child.pid ?? 0
logger.info('Starting gateway for profile "%s" (run mode, PID: %d, port: %d)', name, pid, port)
logger.info('Starting gateway for profile "%s" (run mode, PID: %d, port: %d, detached: %s)', name, pid, port, detachGateway)
// 保存子进程引用,用于后续管理
this.gateways.set(name, { pid, port, host, url, owned: true, process: child })