Skip to content

Commit a6d500c

Browse files
committed
fix(update): bind lifecycle lock to script timeout
1 parent 03887c4 commit a6d500c

3 files changed

Lines changed: 60 additions & 25 deletions

File tree

‎src/infra/package-lifecycle.test.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ describe("package lifecycle completion", () => {
6363
});
6464
});
6565

66-
it("does not expire a live lock within the full lifecycle script budget", async () => {
66+
it.each([
67+
["default update", 30 * 60_000],
68+
["automatic update", 45 * 60_000],
69+
["explicit longer update", 75 * 60_000],
70+
])("records the %s lifecycle budget on its lock", async (_name, scriptTimeoutMs) => {
6771
await withTestDir({ prefix: "openclaw-package-lifecycle-lock-" }, async (packageRoot) => {
6872
const markerPath = path.join(packageRoot, PACKAGE_LIFECYCLE_PENDING_RELATIVE_PATH);
6973
await fs.writeFile(markerPath, "pending\n");
@@ -89,15 +93,20 @@ describe("package lifecycle completion", () => {
8993
}
9094
};
9195

92-
const first = completePendingPackageLifecycle({ packageRoot, runScript });
96+
const startedAt = Date.now();
97+
const first = completePendingPackageLifecycle({
98+
packageRoot,
99+
runScript,
100+
timeoutMs: scriptTimeoutMs,
101+
});
93102
await firstPreinstall;
94-
const staleAtOldThreshold = new Date(Date.now() - 20 * 60_000);
95-
await fs.utimes(
96-
path.join(packageRoot, ".openclaw-lifecycle-lock"),
97-
staleAtOldThreshold,
98-
staleAtOldThreshold,
99-
);
100-
const second = completePendingPackageLifecycle({ packageRoot, runScript });
103+
const lockStat = await fs.stat(path.join(packageRoot, ".openclaw-lifecycle-lock"));
104+
expect(lockStat.mtimeMs).toBeGreaterThanOrEqual(startedAt + scriptTimeoutMs * 2 - 1_000);
105+
const second = completePendingPackageLifecycle({
106+
packageRoot,
107+
runScript,
108+
timeoutMs: scriptTimeoutMs,
109+
});
101110
const secondResult = second.then(
102111
(value) => ({ value }),
103112
(error: unknown) => ({ error }),

‎src/infra/package-lifecycle.ts‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
} from "../../scripts/lib/package-lifecycle-marker.mjs";
88

99
const PACKAGE_LIFECYCLE_LOCK_RELATIVE_PATH = ".openclaw-lifecycle-lock";
10-
const PACKAGE_LIFECYCLE_TIMEOUT_MS = 20 * 60_000;
10+
const DEFAULT_PACKAGE_LIFECYCLE_SCRIPT_TIMEOUT_MS = 20 * 60_000;
1111
const PACKAGE_LIFECYCLE_LOCK_POLL_MS = 100;
12+
const PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS = 20 * 60_000;
1213

1314
export type PackageLifecycleScript = Readonly<{
1415
name: "preinstall" | "postinstall";
@@ -25,10 +26,9 @@ const PACKAGE_LIFECYCLE_SCRIPTS: readonly PackageLifecycleScript[] = [
2526
relativePath: path.join("scripts", "postinstall-bundled-plugins.mjs"),
2627
},
2728
];
28-
// Each script can consume its full timeout. Keep one extra window so scheduler and
29-
// filesystem overhead cannot make a live owner look stale between scripts.
30-
const PACKAGE_LIFECYCLE_LOCK_STALE_MS =
31-
PACKAGE_LIFECYCLE_TIMEOUT_MS * (PACKAGE_LIFECYCLE_SCRIPTS.length + 1);
29+
function resolveLifecycleBudgetMs(scriptTimeoutMs: number): number {
30+
return scriptTimeoutMs * PACKAGE_LIFECYCLE_SCRIPTS.length;
31+
}
3232

3333
function hasErrorCode(error: unknown, code: string): boolean {
3434
return error instanceof Error && "code" in error && error.code === code;
@@ -68,23 +68,41 @@ async function ensurePendingMarker(markerPath: string): Promise<void> {
6868
}
6969
}
7070

71-
async function acquireLifecycleLock(paths: ReturnType<typeof resolveLifecyclePaths>) {
72-
const deadline = Date.now() + PACKAGE_LIFECYCLE_TIMEOUT_MS;
71+
async function acquireLifecycleLock(
72+
paths: ReturnType<typeof resolveLifecyclePaths>,
73+
scriptTimeoutMs: number,
74+
) {
75+
let waitDeadline =
76+
Date.now() +
77+
resolveLifecycleBudgetMs(scriptTimeoutMs) +
78+
PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS;
7379
while (await isPackageLifecyclePending(paths)) {
7480
try {
7581
await fs.mkdir(paths.lock);
82+
const ownerDeadline = new Date(Date.now() + resolveLifecycleBudgetMs(scriptTimeoutMs));
83+
try {
84+
// The directory mtime carries the owner's full script budget across processes.
85+
// Recovery grace also protects the short mkdir-to-utimes ownership window.
86+
await fs.utimes(paths.lock, ownerDeadline, ownerDeadline);
87+
} catch (error) {
88+
await fs.rmdir(paths.lock).catch(() => undefined);
89+
throw error;
90+
}
7691
return async () => await fs.rmdir(paths.lock).catch(() => undefined);
7792
} catch (error) {
7893
if (!hasErrorCode(error, "EEXIST")) {
7994
throw error;
8095
}
8196
const stat = await fs.stat(paths.lock).catch(() => null);
82-
// Lifecycle subprocesses share this deadline. An older lock cannot still own live work.
83-
if (stat && Date.now() - stat.mtimeMs >= PACKAGE_LIFECYCLE_LOCK_STALE_MS) {
84-
await fs.rmdir(paths.lock).catch(() => undefined);
85-
continue;
97+
if (stat) {
98+
const staleAt = stat.mtimeMs + PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS;
99+
waitDeadline = Math.max(waitDeadline, staleAt);
100+
if (Date.now() >= staleAt) {
101+
await fs.rmdir(paths.lock).catch(() => undefined);
102+
continue;
103+
}
86104
}
87-
if (Date.now() >= deadline) {
105+
if (Date.now() >= waitDeadline) {
88106
throw new Error("timed out waiting for another OpenClaw package lifecycle", {
89107
cause: error,
90108
});
@@ -97,13 +115,17 @@ async function acquireLifecycleLock(paths: ReturnType<typeof resolveLifecyclePat
97115
return null;
98116
}
99117

100-
function runPackageLifecycleScript(packageRoot: string, script: PackageLifecycleScript): void {
118+
function runPackageLifecycleScript(
119+
packageRoot: string,
120+
script: PackageLifecycleScript,
121+
timeoutMs: number,
122+
): void {
101123
const scriptPath = path.join(packageRoot, script.relativePath);
102124
const result = spawnSync(process.execPath, [scriptPath], {
103125
cwd: packageRoot,
104126
env: process.env,
105127
stdio: "inherit",
106-
timeout: PACKAGE_LIFECYCLE_TIMEOUT_MS,
128+
timeout: timeoutMs,
107129
});
108130
if (result.error) {
109131
throw result.error;
@@ -118,14 +140,16 @@ function runPackageLifecycleScript(packageRoot: string, script: PackageLifecycle
118140
export async function completePendingPackageLifecycle(params: {
119141
packageRoot: string;
120142
runScript?: (script: PackageLifecycleScript) => void | Promise<void>;
143+
timeoutMs?: number;
121144
}): Promise<boolean> {
122145
const packageRoot = path.resolve(params.packageRoot);
146+
const scriptTimeoutMs = params.timeoutMs ?? DEFAULT_PACKAGE_LIFECYCLE_SCRIPT_TIMEOUT_MS;
123147
const paths = resolveLifecyclePaths(packageRoot);
124148
if (!(await isPackageLifecyclePending(paths))) {
125149
return false;
126150
}
127151

128-
const releaseLock = await acquireLifecycleLock(paths);
152+
const releaseLock = await acquireLifecycleLock(paths, scriptTimeoutMs);
129153
if (!releaseLock) {
130154
return false;
131155
}
@@ -137,7 +161,8 @@ export async function completePendingPackageLifecycle(params: {
137161
// Postinstall alone clears the canonical marker after all lifecycle work succeeds.
138162
await ensurePendingMarker(paths.pending);
139163
const runScript =
140-
params.runScript ?? ((script) => runPackageLifecycleScript(packageRoot, script));
164+
params.runScript ??
165+
((script) => runPackageLifecycleScript(packageRoot, script, scriptTimeoutMs));
141166
for (const script of PACKAGE_LIFECYCLE_SCRIPTS) {
142167
await runScript(script);
143168
}

‎src/infra/package-update-steps.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,7 @@ export async function runGlobalPackageUpdateSteps(params: {
11111111
try {
11121112
await completePendingPackageLifecycle({
11131113
packageRoot: verificationPackageRoot,
1114+
timeoutMs: params.timeoutMs,
11141115
runScript: async (script) => {
11151116
const lifecycleStep = await params.runStep({
11161117
name: `pnpm package ${script.name}`,

0 commit comments

Comments
 (0)