Skip to content

Commit 924bc33

Browse files
committed
fix(systemd): bound effective command lookup
1 parent 7d9b0f5 commit 924bc33

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/daemon/systemd-service-files.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { parseSystemdEnvAssignments, parseSystemdExecStart } from "./systemd-uni
1919

2020
const SYSTEMD_GATEWAY_DOTENV_FILENAME = "gateway.systemd.env";
2121
const SYSTEMD_NODE_DOTENV_FILENAME = "node.systemd.env";
22+
const SYSTEMD_MANAGER_QUERY_TIMEOUT_MS = 5_000;
2223

2324
export function resolveSystemdUnitPathForName(env: GatewayServiceEnv, name: string): string {
2425
const home = normalizeWindowsPathSeparators(resolveDaemonHomeDir(env));
@@ -48,7 +49,12 @@ async function readSystemdManagerExecStart(
4849
opts?: GatewayServiceReadOptions,
4950
): Promise<string[] | null> {
5051
const manager = "org.freedesktop.systemd1";
51-
const query = (args: string[]) => execBusctlUser(env, ["--json=short", ...args], opts?.timeoutMs);
52+
const timeoutMs =
53+
opts?.timeoutMs && opts.timeoutMs > 0 ? opts.timeoutMs : SYSTEMD_MANAGER_QUERY_TIMEOUT_MS;
54+
const deadlineAt = Date.now() + timeoutMs;
55+
// Both D-Bus calls share one deadline so every caller reaches the local fallback promptly.
56+
const query = (args: string[]) =>
57+
execBusctlUser(env, ["--json=short", ...args], Math.max(1, deadlineAt - Date.now()));
5258
const loaded = await query([
5359
"call",
5460
manager,

‎src/daemon/systemd.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,9 @@ describe("readSystemdServiceExecStart", () => {
13721372
execFileMock.mockReset();
13731373
execFileMock.mockImplementation((command, args, options, callback) => {
13741374
expect(command).toBe("busctl");
1375-
expect(options.timeout).toBe(1234);
1375+
expect(options.timeout).toEqual(expect.any(Number));
1376+
expect(options.timeout).toBeGreaterThan(0);
1377+
expect(options.timeout).toBeLessThanOrEqual(1234);
13761378
expect(options.killSignal).toBe("SIGKILL");
13771379
if (args.includes("LoadUnit")) {
13781380
expect(args).toEqual([
@@ -1423,6 +1425,23 @@ describe("readSystemdServiceExecStart", () => {
14231425
expect(execFileMock).toHaveBeenCalledTimes(2);
14241426
});
14251427

1428+
it("bounds manager lookup for callers without a timeout before local fallback", async () => {
1429+
mockReadGatewayServiceFile(["[Service]", "ExecStart=/usr/bin/openclaw gateway run"]);
1430+
execFileMock.mockReset();
1431+
execFileMock.mockImplementation((command, _args, options, callback) => {
1432+
expect(command).toBe("busctl");
1433+
expect(options.timeout).toEqual(expect.any(Number));
1434+
expect(options.timeout).toBeGreaterThan(0);
1435+
expect(options.timeout).toBeLessThanOrEqual(5_000);
1436+
callback(createExecFileError("manager query timed out"), "", "manager query timed out");
1437+
});
1438+
1439+
const command = await readSystemdServiceExecStart({ HOME: TEST_SERVICE_HOME });
1440+
1441+
expect(command?.programArguments).toEqual(["/usr/bin/openclaw", "gateway", "run"]);
1442+
expect(execFileMock).toHaveBeenCalledTimes(1);
1443+
});
1444+
14261445
it("loads OPENCLAW_GATEWAY_TOKEN from EnvironmentFile", async () => {
14271446
const readFileSpy = mockReadGatewayServiceFile(
14281447
["[Service]", "ExecStart=/usr/bin/openclaw gateway run", "EnvironmentFile=%h/.openclaw/.env"],

0 commit comments

Comments
 (0)