Skip to content

Commit e20f445

Browse files
fix(supervisor): keep service-managed children attached (#38463, thanks @spirittechie)
Co-authored-by: Jesse Paul <[email protected]>
1 parent cc7e616 commit e20f445

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Docs: https://docs.openclaw.ai
282282
- Config/env substitution degraded mode: convert missing `${VAR}` resolution in config reads from hard-fail to warning-backed degraded behavior, while preventing unresolved placeholders from being accepted as gateway credentials. (#39050) Thanks @akz142857.
283283
- Discord inbound listener non-blocking dispatch: make `MESSAGE_CREATE` listener handoff asynchronous (no per-listener queue blocking), so long runs no longer stall unrelated incoming events. (#39154) Thanks @yaseenkadlemakki.
284284
- Daemon/Windows PATH freeze fix: stop persisting install-time `PATH` snapshots into Scheduled Task scripts so runtime tool lookup follows current host PATH updates; also refresh local TUI history on silent local finals. (#39139) Thanks @Narcooo.
285+
- Gateway/systemd service restart hardening: clear stale gateway listeners by explicit run-port before service bind, add restart stale-pid port-override support, tune systemd start/stop/exit handling, and disable detached child mode only in service-managed runtime so cgroup stop semantics clean up descendants reliably. (#38463) Thanks @spirittechie.
285286

286287
## 2026.3.2
287288

‎src/process/supervisor/adapters/child.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@ async function createAdapterHarness(params?: {
4949
}
5050

5151
describe("createChildAdapter", () => {
52+
const originalServiceMarker = process.env.OPENCLAW_SERVICE_MARKER;
53+
5254
beforeAll(async () => {
5355
({ createChildAdapter } = await import("./child.js"));
5456
});
5557

5658
beforeEach(() => {
5759
spawnWithFallbackMock.mockClear();
5860
killProcessTreeMock.mockClear();
61+
if (originalServiceMarker === undefined) {
62+
delete process.env.OPENCLAW_SERVICE_MARKER;
63+
} else {
64+
process.env.OPENCLAW_SERVICE_MARKER = originalServiceMarker;
65+
}
5966
});
6067

6168
it("uses process-tree kill for default SIGKILL", async () => {
@@ -90,6 +97,19 @@ describe("createChildAdapter", () => {
9097
expect(killMock).toHaveBeenCalledWith("SIGTERM");
9198
});
9299

100+
it("disables detached mode in service-managed runtime", async () => {
101+
process.env.OPENCLAW_SERVICE_MARKER = "openclaw";
102+
103+
await createAdapterHarness({ pid: 7777 });
104+
105+
const spawnArgs = spawnWithFallbackMock.mock.calls[0]?.[0] as {
106+
options?: { detached?: boolean };
107+
fallbacks?: Array<{ options?: { detached?: boolean } }>;
108+
};
109+
expect(spawnArgs.options?.detached).toBe(false);
110+
expect(spawnArgs.fallbacks ?? []).toEqual([]);
111+
});
112+
93113
it("keeps inherited env when no override env is provided", async () => {
94114
await createAdapterHarness({
95115
pid: 3333,

‎src/process/supervisor/adapters/child.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function resolveCommand(command: string): string {
2121

2222
export type ChildAdapter = SpawnProcessAdapter<NodeJS.Signals | null>;
2323

24+
function isServiceManagedRuntime(): boolean {
25+
return Boolean(process.env.OPENCLAW_SERVICE_MARKER?.trim());
26+
}
27+
2428
export async function createChildAdapter(params: {
2529
argv: string[];
2630
cwd?: string;
@@ -34,11 +38,10 @@ export async function createChildAdapter(params: {
3438

3539
const stdinMode = params.stdinMode ?? (params.input !== undefined ? "pipe-closed" : "inherit");
3640

37-
// On Windows, `detached: true` creates a new process group and can prevent
38-
// stdout/stderr pipes from connecting when running under a Scheduled Task
39-
// (headless, no console). Default to `detached: false` on Windows; on
40-
// POSIX systems keep `detached: true` so the child survives parent exit.
41-
const useDetached = process.platform !== "win32";
41+
// In service-managed mode keep children attached so systemd/launchd can
42+
// stop the full process tree reliably. Outside service mode preserve the
43+
// existing POSIX detached behavior.
44+
const useDetached = process.platform !== "win32" && !isServiceManagedRuntime();
4245

4346
const options: SpawnOptions = {
4447
cwd: params.cwd,

0 commit comments

Comments
 (0)