fix(gateway): use launchd KeepAlive restarts

This commit is contained in:
Peter Steinberger
2026-04-05 07:43:28 +01:00
parent d655a8bc76
commit a65ab607c7
5 changed files with 57 additions and 63 deletions

View File

@@ -72,6 +72,17 @@ vi.mock("../../logging/subsystem.js", () => ({
const LOOP_SIGNALS = ["SIGTERM", "SIGINT", "SIGUSR1"] as const;
type LoopSignal = (typeof LOOP_SIGNALS)[number];
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
function setPlatform(platform: string) {
if (!originalPlatformDescriptor) {
return;
}
Object.defineProperty(process, "platform", {
...originalPlatformDescriptor,
value: platform,
});
}
function removeNewSignalListeners(signal: LoopSignal, existing: Set<(...args: unknown[]) => void>) {
for (const listener of process.listeners(signal)) {
@@ -356,6 +367,33 @@ describe("runGatewayLoop", () => {
});
});
it("waits briefly before exiting on launchd supervised restart", async () => {
vi.clearAllMocks();
try {
setPlatform("darwin");
process.env.LAUNCH_JOB_LABEL = "ai.openclaw.gateway";
restartGatewayProcessWithFreshPid.mockReturnValueOnce({
mode: "supervised",
});
await withIsolatedSignals(async ({ captureSignal }) => {
const { runtime, exited } = await createSignaledLoopHarness();
const sigusr1 = captureSignal("SIGUSR1");
const startedAt = Date.now();
sigusr1();
await expect(exited).resolves.toBe(0);
expect(runtime.exit).toHaveBeenCalledWith(0);
expect(Date.now() - startedAt).toBeGreaterThanOrEqual(1400);
});
} finally {
delete process.env.LAUNCH_JOB_LABEL;
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
}
});
it("forwards lockPort to initial and restart lock acquisitions", async () => {
vi.clearAllMocks();

View File

@@ -12,6 +12,7 @@ import {
markGatewaySigusr1RestartHandled,
scheduleGatewaySigusr1Restart,
} from "../../infra/restart.js";
import { detectRespawnSupervisor } from "../../infra/supervisor-markers.js";
import { createSubsystemLogger } from "../../logging/subsystem.js";
import {
getActiveTaskCount,
@@ -23,6 +24,7 @@ import { createRestartIterationHook } from "../../process/restart-recovery.js";
import type { RuntimeEnv } from "../../runtime.js";
const gatewayLog = createSubsystemLogger("gateway");
const LAUNCHD_SUPERVISED_RESTART_EXIT_DELAY_MS = 1500;
type GatewayRunSignalAction = "stop" | "restart";
@@ -77,6 +79,16 @@ export async function runGatewayLoop(params: {
? `spawned pid ${respawn.pid ?? "unknown"}`
: "supervisor restart";
gatewayLog.info(`restart mode: full process restart (${modeLabel})`);
if (
respawn.mode === "supervised" &&
detectRespawnSupervisor(process.env, process.platform) === "launchd"
) {
// A short clean-exit pause keeps rapid SIGUSR1/config restarts from
// tripping launchd crash-loop throttling before KeepAlive relaunches.
await new Promise((resolve) => {
setTimeout(resolve, LAUNCHD_SUPERVISED_RESTART_EXIT_DELAY_MS);
});
}
exitProcess(0);
return;
}