























@@ -1021,26 +1021,30 @@ async function startGateway(runner, port, env, logPath) {
10211021}
1022102210231023export async function stopGateway(child, options = {}) {
1024-if (!child || hasChildExited(child)) {
1024+const killProcess = options.killProcess ?? defaultKillProcess;
1025+if (!child || !isGatewayAlive(child, killProcess)) {
10251026return;
10261027}
10271028const teardownGraceMs = Math.max(0, options.teardownGraceMs ?? GATEWAY_TEARDOWN_GRACE_MS);
10281029const killGraceMs = Math.max(0, options.killGraceMs ?? GATEWAY_TEARDOWN_KILL_GRACE_MS);
10291030const exited = new Promise((resolve) => {
10301031child.once("exit", resolve);
10311032});
1032-const waitForExit = async (ms) =>
1033-hasChildExited(child)
1034- ? true
1035- : await Promise.race([exited.then(() => true), delay(ms).then(() => false)]);
1033+const waitForExit = async (ms) => {
1034+if (!isGatewayAlive(child, killProcess)) {
1035+return true;
1036+}
1037+await Promise.race([exited, delay(ms)]);
1038+return !isGatewayAlive(child, killProcess);
1039+};
103610401037-if (!signalGateway(child, "SIGTERM")) {
1041+if (!signalGateway(child, "SIGTERM", killProcess)) {
10381042return;
10391043}
10401044if (await waitForExit(teardownGraceMs)) {
10411045return;
10421046}
1043-if (!signalGateway(child, "SIGKILL")) {
1047+if (!signalGateway(child, "SIGKILL", killProcess)) {
10441048return;
10451049}
10461050if (await waitForExit(killGraceMs)) {
@@ -1053,6 +1057,25 @@ export function hasChildExited(child) {
10531057return child.exitCode !== null || child.signalCode !== null;
10541058}
105510591060+function defaultKillProcess(pid, signal) {
1061+return process.kill(pid, signal);
1062+}
1063+1064+function isGatewayAlive(child, killProcess) {
1065+if (process.platform !== "win32" && typeof child.pid === "number") {
1066+try {
1067+killProcess(-child.pid, 0);
1068+return true;
1069+} catch (error) {
1070+if (error?.code === "ESRCH") {
1071+return false;
1072+}
1073+throw error;
1074+}
1075+}
1076+return !hasChildExited(child);
1077+}
1078+10561079function createChildExitPromise(child) {
10571080if (!child || typeof child.once !== "function") {
10581081return null;
@@ -1069,10 +1092,10 @@ function releaseUnsettledGatewayChild(child) {
10691092child.unref?.();
10701093}
107110941072-function signalGateway(child, signal) {
1095+function signalGateway(child, signal, killProcess = defaultKillProcess) {
10731096if (process.platform !== "win32" && typeof child.pid === "number") {
10741097try {
1075-process.kill(-child.pid, signal);
1098+killProcess(-child.pid, signal);
10761099return true;
10771100} catch (error) {
10781101if (error?.code === "ESRCH") {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。