

























@@ -327,6 +327,14 @@ function signalGatewayProcessForParentExit(child, signal, killProcess) {
327327}
328328}
329329330+function gatewayProcessAliveForParentExit(child, killProcess) {
331+try {
332+return isGatewayProcessAlive(child, killProcess);
333+} catch {
334+return true;
335+}
336+}
337+330338/**
331339 * Installs parent-process cleanup handlers for a spawned gateway.
332340 */
@@ -335,16 +343,38 @@ export function installGatewayParentCleanup(
335343{ killProcess = defaultKillProcess, processLike = process } = {},
336344) {
337345const signalHandlers = new Map();
338-const cleanup = (signal) => {
346+let forceKillTimer;
347+let parentSignalPending = false;
348+const forceCleanup = (signal) => {
339349signalGatewayProcessForParentExit(child, signal, killProcess);
340350if (process.platform !== "win32") {
341351signalGatewayProcessForParentExit(child, "SIGKILL", killProcess);
342352}
343353};
354+const cleanupAndReraise = (signal) => {
355+parentSignalPending = true;
356+signalGatewayProcessForParentExit(child, signal, killProcess);
357+const finish = () => {
358+forceKillTimer = undefined;
359+signalGatewayProcessForParentExit(child, "SIGKILL", killProcess);
360+processLike.kill?.(processLike.pid, signal);
361+};
362+// Signal handlers can give the detached gateway group one normal teardown
363+// grace window before re-raising; process exit cleanup cannot wait.
364+if (process.platform === "win32" || !gatewayProcessAliveForParentExit(child, killProcess)) {
365+processLike.kill?.(processLike.pid, signal);
366+return;
367+}
368+forceKillTimer = setTimeout(finish, GATEWAY_FORCE_KILL_GRACE_MS);
369+};
344370const exitHandler = () => {
345-cleanup("SIGTERM");
371+forceCleanup("SIGTERM");
346372};
347373const removeHandlers = () => {
374+if (!parentSignalPending && forceKillTimer) {
375+clearTimeout(forceKillTimer);
376+forceKillTimer = undefined;
377+}
348378processLike.off?.("exit", exitHandler);
349379for (const [signal, handler] of signalHandlers) {
350380processLike.off?.(signal, handler);
@@ -354,9 +384,8 @@ export function installGatewayParentCleanup(
354384processLike.once("exit", exitHandler);
355385for (const signal of PARENT_TERMINATION_SIGNALS) {
356386const handler = () => {
357-cleanup(signal);
358387removeHandlers();
359-processLike.kill?.(processLike.pid, signal);
388+cleanupAndReraise(signal);
360389};
361390signalHandlers.set(signal, handler);
362391processLike.once(signal, handler);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。