




















@@ -4,10 +4,7 @@ import {
44drainFileLockManagerForTest,
55resetFileLockManagerForTest,
66} from "@openclaw/fs-safe/file-lock";
7-import {
8-removeReportedStaleLockIfStillStale,
9-shouldRemoveDeadOwnerOrExpiredLock,
10-} from "../infra/stale-lock-file.js";
7+import { shouldRemoveDeadOwnerOrExpiredLock } from "../infra/stale-lock-file.js";
118129export type FileLockOptions = {
1310retries: {
@@ -53,10 +50,6 @@ async function shouldReclaimPluginLock(params: {
5350});
5451}
555256-function isFileLockError(error: unknown, code: string): boolean {
57-return (error as { code?: unknown } | null)?.code === code;
58-}
59-6053function normalizeLockError(err: unknown): never {
6154if ((err as { code?: unknown }).code === FILE_LOCK_TIMEOUT_ERROR_CODE) {
6255throw Object.assign(new Error((err as Error).message), {
@@ -86,36 +79,24 @@ export async function acquireFileLock(
8679filePath: string,
8780options: FileLockOptions,
8881): Promise<FileLockHandle> {
89-while (true) {
90-try {
91-const lock = await acquireFsSafeFileLock(filePath, {
92-managerKey: FILE_LOCK_MANAGER_KEY,
93-staleMs: options.stale,
94-retry: options.retries,
95-allowReentrant: true,
96-payload: () => ({ pid: process.pid, createdAt: new Date().toISOString() }),
97-shouldReclaim: shouldReclaimPluginLock,
98-});
99-return { lockPath: lock.lockPath, release: lock.release };
100-} catch (err) {
101-if (isFileLockError(err, FILE_LOCK_STALE_ERROR_CODE)) {
102-const lockPath = (err as { lockPath?: string }).lockPath;
103-if (
104-lockPath &&
105-(await removeReportedStaleLockIfStillStale({
106- lockPath,
107-shouldRemove: (snapshot) =>
108-shouldRemoveDeadOwnerOrExpiredLock({
109-payload: snapshot.payload,
110-staleMs: options.stale,
111-}),
112-}))
113-) {
114-continue;
115-}
116-}
117-return normalizeLockError(err);
118-}
82+try {
83+const lock = await acquireFsSafeFileLock(filePath, {
84+managerKey: FILE_LOCK_MANAGER_KEY,
85+staleMs: options.stale,
86+retry: options.retries,
87+staleRecovery: "remove-if-unchanged",
88+allowReentrant: true,
89+payload: () => ({ pid: process.pid, createdAt: new Date().toISOString() }),
90+shouldReclaim: shouldReclaimPluginLock,
91+shouldRemoveStaleLock: (snapshot) =>
92+shouldRemoveDeadOwnerOrExpiredLock({
93+payload: snapshot.payload,
94+staleMs: options.stale,
95+}),
96+});
97+return { lockPath: lock.lockPath, release: lock.release };
98+} catch (err) {
99+return normalizeLockError(err);
119100}
120101}
121102此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。