





























@@ -282,6 +282,7 @@ export async function runWatchMain(params = {}) {
282282283283const childEnv = { ...deps.env };
284284const watchSession = `${deps.now()}-${deps.process.pid}`;
285+const useChildProcessGroup = process.platform !== "win32" && deps.process.stdin?.isTTY !== true;
285286childEnv.OPENCLAW_WATCH_MODE = "1";
286287childEnv.OPENCLAW_WATCH_SESSION = watchSession;
287288// The watcher owns process restarts; keep SIGUSR1/config reloads in-process
@@ -302,6 +303,36 @@ export async function runWatchMain(params = {}) {
302303let shutdownExitCode = null;
303304let shutdownKillTimer = null;
304305306+const signalWatchProcess = (child, signal) => {
307+if (!child || typeof child.kill !== "function") {
308+return;
309+}
310+if (useChildProcessGroup && typeof child.pid === "number") {
311+try {
312+deps.signalProcess(-child.pid, signal);
313+return;
314+} catch (error) {
315+if (error?.code === "ESRCH" || error?.code === "EPERM") {
316+return;
317+}
318+}
319+}
320+child.kill(signal);
321+};
322+323+const forceKillWatchProcessGroup = (child) => {
324+if (!useChildProcessGroup || typeof child?.pid !== "number") {
325+return;
326+}
327+try {
328+deps.signalProcess(-child.pid, "SIGKILL");
329+} catch (error) {
330+if (error?.code !== "ESRCH" && error?.code !== "EPERM") {
331+throw error;
332+}
333+}
334+};
335+305336const settle = (code) => {
306337if (settled) {
307338return;
@@ -328,26 +359,27 @@ export async function runWatchMain(params = {}) {
328359settle(code);
329360return;
330361}
331-watchProcess.kill(WATCH_RESTART_SIGNAL);
362+const shutdownProcess = watchProcess;
363+signalWatchProcess(shutdownProcess, WATCH_RESTART_SIGNAL);
332364shutdownKillTimer ??= setTimeout(() => {
333365shutdownKillTimer = null;
334-if (watchProcess && typeof watchProcess.kill === "function") {
335-watchProcess.kill("SIGKILL");
336-}
366+signalWatchProcess(shutdownProcess, "SIGKILL");
337367}, WATCH_SHUTDOWN_KILL_GRACE_MS);
338368};
339369340-const settleIfShuttingDown = () => {
370+const settleIfShuttingDown = (exitedProcess) => {
341371if (!shuttingDown || shutdownExitCode === null) {
342372return false;
343373}
374+forceKillWatchProcessGroup(exitedProcess);
344375settle(shutdownExitCode);
345376return true;
346377};
347378348379const startRunner = () => {
349380watchProcess = deps.spawn(deps.process.execPath, buildRunnerArgs(deps.args), {
350381cwd: deps.cwd,
382+detached: useChildProcessGroup,
351383env: childEnv,
352384stdio: "inherit",
353385});
@@ -357,14 +389,16 @@ export async function runWatchMain(params = {}) {
357389settle(1);
358390});
359391watchProcess.on("exit", (exitCode, exitSignal) => {
392+const exitedProcess = watchProcess;
360393watchProcess = null;
361394if (settled) {
362395return;
363396}
364-if (settleIfShuttingDown()) {
397+if (settleIfShuttingDown(exitedProcess)) {
365398return;
366399}
367400if (restartRequested || shouldRestartAfterChildExit(exitCode, exitSignal)) {
401+forceKillWatchProcessGroup(exitedProcess);
368402restartRequested = false;
369403startRunner();
370404return;
@@ -388,7 +422,7 @@ export async function runWatchMain(params = {}) {
388422settled = true;
389423shuttingDown = true;
390424if (watchProcess && typeof watchProcess.kill === "function") {
391-watchProcess.kill(WATCH_RESTART_SIGNAL);
425+signalWatchProcess(watchProcess, WATCH_RESTART_SIGNAL);
392426}
393427releaseWatchLock(lockHandle);
394428watcher?.close?.().catch?.(() => {});
@@ -421,6 +455,7 @@ export async function runWatchMain(params = {}) {
421455);
422456watchProcess = deps.spawn(deps.process.execPath, buildDoctorRunnerArgs(), {
423457cwd: deps.cwd,
458+detached: useChildProcessGroup,
424459env: childEnv,
425460stdio: "inherit",
426461});
@@ -430,11 +465,12 @@ export async function runWatchMain(params = {}) {
430465settle(1);
431466});
432467watchProcess.on("exit", (exitCode, exitSignal) => {
468+const exitedProcess = watchProcess;
433469watchProcess = null;
434470if (settled) {
435471return;
436472}
437-if (settleIfShuttingDown()) {
473+if (settleIfShuttingDown(exitedProcess)) {
438474return;
439475}
440476if (exitCode === 0 && !exitSignal) {
@@ -460,7 +496,7 @@ export async function runWatchMain(params = {}) {
460496}
461497restartRequested = true;
462498if (typeof watchProcess.kill === "function") {
463-watchProcess.kill(WATCH_RESTART_SIGNAL);
499+signalWatchProcess(watchProcess, WATCH_RESTART_SIGNAL);
464500}
465501};
466502此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。