























@@ -11,8 +11,16 @@ const MAX_GRACE_MS = 60_000;
1111 *
1212 * This gives child processes a chance to clean up (close connections, remove
1313 * temp files, terminate their own children) before being hard-killed.
14+ *
15+ * When the child was spawned with `detached: false` (e.g. service-managed
16+ * runtime under launchd/systemd), pass `detached: false` to skip the Unix
17+ * `process.kill(-pid, ...)` group-kill — it would otherwise target the
18+ * gateway's own process group and SIGTERM the gateway itself. (#71662)
1419 */
15-export function killProcessTree(pid: number, opts?: { graceMs?: number }): void {
20+export function killProcessTree(
21+pid: number,
22+opts?: { graceMs?: number; detached?: boolean },
23+): void {
1624if (!Number.isFinite(pid) || pid <= 0) {
1725return;
1826}
@@ -24,7 +32,7 @@ export function killProcessTree(pid: number, opts?: { graceMs?: number }): void
2432return;
2533}
263427-killProcessTreeUnix(pid, graceMs);
35+killProcessTreeUnix(pid, graceMs, opts?.detached !== false);
2836}
29373038function normalizeGraceMs(value?: number): number {
@@ -43,12 +51,23 @@ function isProcessAlive(pid: number): boolean {
4351}
4452}
455346-function killProcessTreeUnix(pid: number, graceMs: number): void {
47-// Step 1: Try graceful SIGTERM to process group
48-try {
49-process.kill(-pid, "SIGTERM");
50-} catch {
51-// Process group doesn't exist or we lack permission - try direct
54+function killProcessTreeUnix(pid: number, graceMs: number, useGroupKill: boolean): void {
55+// Step 1: Try graceful SIGTERM. Prefer process-group kill (`-pid`) when the
56+// child was spawned detached so it has its own group; otherwise stick to the
57+// direct pid to avoid SIGTERMing our own process group (the gateway). (#71662)
58+if (useGroupKill) {
59+try {
60+process.kill(-pid, "SIGTERM");
61+} catch {
62+// Process group doesn't exist or we lack permission - try direct
63+try {
64+process.kill(pid, "SIGTERM");
65+} catch {
66+// Already gone
67+return;
68+}
69+}
70+} else {
5271try {
5372process.kill(pid, "SIGTERM");
5473} catch {
@@ -59,7 +78,7 @@ function killProcessTreeUnix(pid: number, graceMs: number): void {
59786079// Step 2: Wait grace period, then SIGKILL if still alive
6180setTimeout(() => {
62-if (isProcessAlive(-pid)) {
81+if (useGroupKill && isProcessAlive(-pid)) {
6382try {
6483process.kill(-pid, "SIGKILL");
6584return;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。