fix(daemon): replace bootout with kickstart -k for launchd restarts on macOS
On macOS, launchctl bootout permanently unloads the LaunchAgent plist. Even with KeepAlive: true, launchd cannot respawn a service whose plist has been removed from its registry. This left users with a dead gateway requiring manual 'openclaw gateway install' to recover. Affected trigger paths: - openclaw gateway restart from an agent session (#43311) - SIGTERM on config reload (#43406) - Gateway self-restart via SIGTERM (#43035) - Hot reload on channel config change (#43049) Switch restartLaunchAgent() to launchctl kickstart -k, which force-kills and restarts the service without unloading the plist. When the restart originates from inside the launchd-managed process tree, delegate to a new detached handoff helper (launchd-restart-handoff.ts) to avoid the caller being killed mid-command. Self-restart paths in process-respawn.ts now schedule the detached start-after-exit handoff before exiting instead of relying on exit/KeepAlive timing. Fixes #43311, #43406, #43035, #43049feature/PAZ-358-fe-credit-error-display
parent
e11be576fb
commit
3c0fd3dffe
@ -0,0 +1,142 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { resolveGatewayLaunchAgentLabel } from "./constants.js";
|
||||
|
||||
export type LaunchdRestartHandoffMode = "kickstart" | "start-after-exit";
|
||||
|
||||
export type LaunchdRestartHandoffResult = {
|
||||
ok: boolean;
|
||||
pid?: number;
|
||||
detail?: string;
|
||||
};
|
||||
|
||||
export type LaunchdRestartTarget = {
|
||||
domain: string;
|
||||
label: string;
|
||||
plistPath: string;
|
||||
serviceTarget: string;
|
||||
};
|
||||
|
||||
function resolveGuiDomain(): string {
|
||||
if (typeof process.getuid !== "function") {
|
||||
return "gui/501";
|
||||
}
|
||||
return `gui/${process.getuid()}`;
|
||||
}
|
||||
|
||||
function resolveLaunchAgentLabel(env?: Record<string, string | undefined>): string {
|
||||
const envLabel = env?.OPENCLAW_LAUNCHD_LABEL?.trim();
|
||||
if (envLabel) {
|
||||
return envLabel;
|
||||
}
|
||||
return resolveGatewayLaunchAgentLabel(env?.OPENCLAW_PROFILE);
|
||||
}
|
||||
|
||||
export function resolveLaunchdRestartTarget(
|
||||
env: Record<string, string | undefined> = process.env,
|
||||
): LaunchdRestartTarget {
|
||||
const domain = resolveGuiDomain();
|
||||
const label = resolveLaunchAgentLabel(env);
|
||||
const home = env.HOME?.trim() || os.homedir();
|
||||
const plistPath = path.join(home, "Library", "LaunchAgents", `${label}.plist`);
|
||||
return {
|
||||
domain,
|
||||
label,
|
||||
plistPath,
|
||||
serviceTarget: `${domain}/${label}`,
|
||||
};
|
||||
}
|
||||
|
||||
export function isCurrentProcessLaunchdServiceLabel(
|
||||
label: string,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): boolean {
|
||||
const launchdLabel =
|
||||
env.LAUNCH_JOB_LABEL?.trim() || env.LAUNCH_JOB_NAME?.trim() || env.XPC_SERVICE_NAME?.trim();
|
||||
if (launchdLabel) {
|
||||
return launchdLabel === label;
|
||||
}
|
||||
const configuredLabel = env.OPENCLAW_LAUNCHD_LABEL?.trim();
|
||||
return Boolean(configuredLabel && configuredLabel === label);
|
||||
}
|
||||
|
||||
function buildLaunchdRestartScript(mode: LaunchdRestartHandoffMode): string {
|
||||
if (mode === "kickstart") {
|
||||
return `service_target="$1"
|
||||
domain="$2"
|
||||
plist_path="$3"
|
||||
sleep 1
|
||||
if ! launchctl kickstart -k "$service_target" >/dev/null 2>&1; then
|
||||
launchctl enable "$service_target" >/dev/null 2>&1
|
||||
if launchctl bootstrap "$domain" "$plist_path" >/dev/null 2>&1; then
|
||||
launchctl kickstart -k "$service_target" >/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
`;
|
||||
}
|
||||
|
||||
return `service_target="$1"
|
||||
domain="$2"
|
||||
plist_path="$3"
|
||||
wait_pid="$4"
|
||||
if [ -n "$wait_pid" ] && [ "$wait_pid" -gt 1 ] 2>/dev/null; then
|
||||
attempts=0
|
||||
while kill -0 "$wait_pid" >/dev/null 2>&1; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 100 ]; then
|
||||
break
|
||||
fi
|
||||
sleep 0.1
|
||||
done
|
||||
else
|
||||
sleep 1
|
||||
fi
|
||||
if ! launchctl start "$service_target" >/dev/null 2>&1; then
|
||||
launchctl enable "$service_target" >/dev/null 2>&1
|
||||
if launchctl bootstrap "$domain" "$plist_path" >/dev/null 2>&1; then
|
||||
launchctl start "$service_target" >/dev/null 2>&1 || launchctl kickstart -k "$service_target" >/dev/null 2>&1 || true
|
||||
else
|
||||
launchctl kickstart -k "$service_target" >/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
`;
|
||||
}
|
||||
|
||||
export function scheduleDetachedLaunchdRestartHandoff(params: {
|
||||
env?: Record<string, string | undefined>;
|
||||
mode: LaunchdRestartHandoffMode;
|
||||
waitForPid?: number;
|
||||
}): LaunchdRestartHandoffResult {
|
||||
const target = resolveLaunchdRestartTarget(params.env);
|
||||
const waitForPid =
|
||||
typeof params.waitForPid === "number" && Number.isFinite(params.waitForPid)
|
||||
? Math.floor(params.waitForPid)
|
||||
: 0;
|
||||
try {
|
||||
const child = spawn(
|
||||
"/bin/sh",
|
||||
[
|
||||
"-c",
|
||||
buildLaunchdRestartScript(params.mode),
|
||||
"openclaw-launchd-restart-handoff",
|
||||
target.serviceTarget,
|
||||
target.domain,
|
||||
target.plistPath,
|
||||
String(waitForPid),
|
||||
],
|
||||
{
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
env: { ...process.env, ...params.env },
|
||||
},
|
||||
);
|
||||
child.unref();
|
||||
return { ok: true, pid: child.pid ?? undefined };
|
||||
} catch (err) {
|
||||
return {
|
||||
ok: false,
|
||||
detail: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue