Skip to content

Commit 05ab514

Browse files
committed
fix(update): quiesce respawn-pending services
1 parent 6ee409c commit 05ab514

2 files changed

Lines changed: 138 additions & 2 deletions

File tree

‎src/cli/update-cli.test.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4598,6 +4598,137 @@ describe("update-cli", () => {
45984598
processOffSpy.mockRestore();
45994599
});
46004600

4601+
it.each([
4602+
{ platform: "darwin" as const, handoff: undefined },
4603+
{ platform: "linux" as const, handoff: "1" },
4604+
{ platform: "win32" as const, handoff: "1" },
4605+
])(
4606+
"quiesces a stopped loaded managed gateway on $platform before package replacement",
4607+
async ({ platform, handoff }) => {
4608+
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue(platform);
4609+
const tempDir = await createTrackedTempDir(`openclaw-update-stopped-loaded-${platform}-`);
4610+
const { nodeModules, entryPath } = await setupInstalledPackageRoot(tempDir);
4611+
primeServiceCommand(["node", entryPath, "gateway", "run"], {
4612+
OPENCLAW_SERVICE_MARKER: "openclaw",
4613+
OPENCLAW_SERVICE_KIND: "gateway",
4614+
});
4615+
serviceLoaded.mockResolvedValue(true);
4616+
serviceReadRuntime.mockResolvedValue({ status: "stopped", state: "stopped" });
4617+
mockFileBackedPathExists();
4618+
mockNpmGlobalRoot(nodeModules);
4619+
let finishStop: (() => void) | undefined;
4620+
let markStopStarted: (() => void) | undefined;
4621+
const stopStarted = new Promise<void>((resolve) => {
4622+
markStopStarted = resolve;
4623+
});
4624+
serviceStop.mockImplementationOnce(() => {
4625+
markStopStarted?.();
4626+
return new Promise<void>((resolve) => {
4627+
finishStop = resolve;
4628+
});
4629+
});
4630+
4631+
try {
4632+
await withEnvAsync({ OPENCLAW_UPDATE_RUN_HANDOFF: handoff }, async () => {
4633+
const updatePromise = updateCommand({ yes: true });
4634+
const firstOutcome = await Promise.race([
4635+
stopStarted.then(() => "stop" as const),
4636+
updatePromise.then(() => "update" as const),
4637+
]);
4638+
4639+
expect(firstOutcome).toBe("stop");
4640+
expect(serviceStop).toHaveBeenCalledOnce();
4641+
expect(packageInstallCommandCall()).toBeUndefined();
4642+
const pluginRecordCallsBeforeStop =
4643+
loadInstalledPluginIndexInstallRecords.mock.calls.length;
4644+
if (!finishStop) {
4645+
throw new Error("expected the managed service stop to remain pending");
4646+
}
4647+
finishStop();
4648+
await updatePromise;
4649+
expect(loadInstalledPluginIndexInstallRecords.mock.calls.length).toBeGreaterThan(
4650+
pluginRecordCallsBeforeStop,
4651+
);
4652+
});
4653+
} finally {
4654+
platformSpy.mockRestore();
4655+
}
4656+
4657+
expect(packageInstallCommandCall()).toBeDefined();
4658+
expect(loadInstalledPluginIndexInstallRecords).toHaveBeenCalled();
4659+
expect(serviceStop.mock.invocationCallOrder[0]).toBeLessThan(
4660+
requireValue(
4661+
loadInstalledPluginIndexInstallRecords.mock.invocationCallOrder.at(-1),
4662+
"owned managed update context capture order",
4663+
),
4664+
);
4665+
},
4666+
);
4667+
4668+
it.each([
4669+
{ name: "an unloaded Darwin LaunchAgent", platform: "darwin" as const, loaded: false },
4670+
{ name: "an ordinary stopped systemd unit", platform: "linux" as const, loaded: true },
4671+
{ name: "an ordinary stopped Scheduled Task", platform: "win32" as const, loaded: true },
4672+
])("leaves $name stopped during package replacement", async ({ platform, loaded }) => {
4673+
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue(platform);
4674+
const tempDir = await createTrackedTempDir(`openclaw-update-stopped-${platform}-`);
4675+
const { nodeModules, entryPath } = await setupInstalledPackageRoot(tempDir);
4676+
primeServiceCommand(["node", entryPath, "gateway", "run"]);
4677+
serviceLoaded.mockResolvedValue(loaded);
4678+
serviceReadRuntime.mockResolvedValue({ status: "stopped", state: "stopped" });
4679+
mockFileBackedPathExists();
4680+
mockNpmGlobalRoot(nodeModules);
4681+
4682+
try {
4683+
await withEnvAsync({ OPENCLAW_UPDATE_RUN_HANDOFF: undefined }, async () => {
4684+
await updateCommand({ yes: true });
4685+
});
4686+
} finally {
4687+
platformSpy.mockRestore();
4688+
}
4689+
4690+
expect(serviceStop).not.toHaveBeenCalled();
4691+
expect(serviceRestart).not.toHaveBeenCalled();
4692+
expect(packageInstallCommandCall()).toBeDefined();
4693+
});
4694+
4695+
it("restarts a quiesced stopped LaunchAgent after package replacement fails", async () => {
4696+
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
4697+
const tempDir = await createTrackedTempDir("openclaw-update-stopped-launchagent-failure-");
4698+
const { nodeModules, entryPath } = await setupInstalledPackageRoot(tempDir);
4699+
primeServiceCommand(["node", entryPath, "gateway", "run"]);
4700+
serviceLoaded.mockResolvedValue(true);
4701+
serviceReadRuntime.mockResolvedValue({ status: "stopped", state: "stopped" });
4702+
mockFileBackedPathExists();
4703+
mockNpmGlobalRoot(nodeModules);
4704+
vi.mocked(runCommandWithTimeout).mockImplementation(async (argv) => {
4705+
if (argv[0] === "npm" && argv[1] === "i" && argv[2] === "-g") {
4706+
throw new Error("package replacement failed");
4707+
}
4708+
return commandResult();
4709+
});
4710+
4711+
try {
4712+
await expect(updateCommand({ yes: true })).rejects.toThrow("package replacement failed");
4713+
} finally {
4714+
platformSpy.mockRestore();
4715+
}
4716+
4717+
expect(serviceStop).toHaveBeenCalledOnce();
4718+
expect(serviceRestart).toHaveBeenCalledOnce();
4719+
const packageInstallCallIndex = commandCalls().findIndex(
4720+
([argv]) => argv[0] === "npm" && argv[1] === "i" && argv[2] === "-g",
4721+
);
4722+
const packageInstallCallOrder = requireValue(
4723+
vi.mocked(runCommandWithTimeout).mock.invocationCallOrder[packageInstallCallIndex],
4724+
"package replacement call order",
4725+
);
4726+
expect(serviceStop.mock.invocationCallOrder[0]).toBeLessThan(packageInstallCallOrder);
4727+
expect(packageInstallCallOrder).toBeLessThan(
4728+
requireValue(serviceRestart.mock.invocationCallOrder[0], "service restart call order"),
4729+
);
4730+
});
4731+
46014732
it("does not inspect or mutate a Windows host service from an isolated install", async () => {
46024733
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
46034734
const tempDir = await createTrackedTempDir("openclaw-update-isolated-service-");

‎src/cli/update-cli/update-command-service.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ export async function maybeStopManagedServiceBeforeMutableUpdate(params: {
464464
};
465465
}
466466

467-
if (!serviceState.running) {
467+
// A loaded LaunchAgent can be between KeepAlive respawns. Other supervisors
468+
// need the handoff marker to distinguish that transition from operator-stopped state.
469+
const supervisorMayRespawn =
470+
serviceState.loaded &&
471+
(process.platform === "darwin" || process.env.OPENCLAW_UPDATE_RUN_HANDOFF === "1");
472+
if (!serviceState.running && !supervisorMayRespawn) {
468473
const windowsTaskAutoStartRecovery = await maybeSuspendWindowsTaskAutoStartForPackageUpdate({
469474
updateInstallKind: params.updateInstallKind,
470475
serviceEnv: serviceState.env,
@@ -554,7 +559,7 @@ export async function maybeStopManagedServiceBeforeMutableUpdate(params: {
554559
stopped: true,
555560
inspected: true,
556561
runtimeInspected: true,
557-
running: true,
562+
running: serviceState.running,
558563
...serviceOwnership,
559564
serviceEnv: serviceState.env,
560565
serviceDefinitionEnv: serviceState.command?.environment,

0 commit comments

Comments
 (0)