fix(gateway): terminate owned process before retry to prevent port conflict on Windows (#724)

This commit is contained in:
写了吗?
2026-04-02 16:11:03 +08:00
committed by GitHub
Unverified
parent d3112f28d8
commit 560ae95611
3 changed files with 67 additions and 6 deletions

View File

@@ -259,8 +259,8 @@ export class GatewayManager extends EventEmitter {
this.startHealthCheck();
},
waitForPortFree: async (port) => {
await waitForPortFree(port);
waitForPortFree: async (port, signal) => {
await waitForPortFree(port, 30000, signal);
},
startProcess: async () => {
await this.startProcess();
@@ -282,6 +282,29 @@ export class GatewayManager extends EventEmitter {
delay: async (ms) => {
await new Promise((resolve) => setTimeout(resolve, ms));
},
terminateOwnedProcess: async () => {
if (this.process && this.ownsProcess) {
logger.info('Terminating owned Gateway process before retry...');
const proc = this.process;
const pid = proc.pid;
await terminateOwnedGatewayProcess(proc).catch(() => {});
// Only clear the handle if the process has actually exited.
// terminateOwnedGatewayProcess may resolve via its timeout path
// while the child is still alive; in that case keep the reference
// so subsequent retries and stop() can still target it.
if (pid != null) {
try {
process.kill(pid, 0);
// Still alive — keep this.process so later cleanup can reach it
} catch {
// Process is gone — safe to clear the handle
this.process = null;
}
} else {
this.process = null;
}
}
},
});
} catch (error) {
if (error instanceof LifecycleSupersededError) {