惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
月光博客
月光博客
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
B
Blog
C
Check Point Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
量子位
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(agents): reclaim reported stale session locks · openc...
vincentkoc · 2026-05-07 · via Recent Commits to openclaw:main

@@ -62,6 +62,10 @@ type LockInspectionDetails = Pick<

62626363

const SESSION_LOCKS = createFileLockManager("openclaw.session-write-lock");

646465+

function isFileLockError(error: unknown, code: string): boolean {

66+

return (error as { code?: unknown } | null)?.code === code;

67+

}

68+6569

export type SessionWriteLockAcquireTimeoutConfig = {

6670

session?: {

6771

writeLock?: {

@@ -368,6 +372,33 @@ async function shouldReclaimContendedLockFile(

368372

}

369373

}

370374375+

function sessionLockHeldByThisProcess(normalizedSessionFile: string): boolean {

376+

return SESSION_LOCKS.heldEntries().some(

377+

(entry) => entry.normalizedTargetPath === normalizedSessionFile,

378+

);

379+

}

380+381+

async function removeReportedStaleLockIfStillStale(params: {

382+

lockPath: string;

383+

normalizedSessionFile: string;

384+

staleMs: number;

385+

}): Promise<boolean> {

386+

const nowMs = Date.now();

387+

const payload = await readLockPayload(params.lockPath);

388+

const inspected = inspectLockPayloadForSession({

389+

payload,

390+

staleMs: params.staleMs,

391+

nowMs,

392+

heldByThisProcess: sessionLockHeldByThisProcess(params.normalizedSessionFile),

393+

reclaimLockWithoutStarttime: true,

394+

});

395+

if (!(await shouldReclaimContendedLockFile(params.lockPath, inspected, params.staleMs, nowMs))) {

396+

return false;

397+

}

398+

await fs.rm(params.lockPath, { force: true });

399+

return true;

400+

}

401+371402

function shouldTreatAsOrphanSelfLock(params: {

372403

payload: LockFilePayload | null;

373404

heldByThisProcess: boolean;

@@ -502,42 +533,56 @@ export async function acquireSessionWriteLock(params: {

502533

const normalizedSessionFile = await resolveNormalizedSessionFile(sessionFile);

503534

const lockPath = `${normalizedSessionFile}.lock`;

504535

await fs.mkdir(sessionDir, { recursive: true });

505-

try {

506-

const lock = await SESSION_LOCKS.acquire(sessionFile, {

507-

staleMs,

508-

timeoutMs,

509-

retry: { minTimeout: 50, maxTimeout: 1000, factor: 1 },

510-

allowReentrant,

511-

metadata: { maxHoldMs },

512-

payload: () => {

513-

const createdAt = new Date().toISOString();

514-

const starttime = getProcessStartTime(process.pid);

515-

const lockPayload: LockFilePayload = { pid: process.pid, createdAt };

516-

if (starttime !== null) {

517-

lockPayload.starttime = starttime;

536+

while (true) {

537+

try {

538+

const lock = await SESSION_LOCKS.acquire(sessionFile, {

539+

staleMs,

540+

timeoutMs,

541+

retry: { minTimeout: 50, maxTimeout: 1000, factor: 1 },

542+

allowReentrant,

543+

metadata: { maxHoldMs },

544+

payload: () => {

545+

const createdAt = new Date().toISOString();

546+

const starttime = getProcessStartTime(process.pid);

547+

const lockPayload: LockFilePayload = { pid: process.pid, createdAt };

548+

if (starttime !== null) {

549+

lockPayload.starttime = starttime;

550+

}

551+

return lockPayload as Record<string, unknown>;

552+

},

553+

shouldReclaim: async ({ payload, nowMs, heldByThisProcess }) => {

554+

const inspected = inspectLockPayloadForSession({

555+

payload: payload as LockFilePayload | null,

556+

staleMs,

557+

nowMs,

558+

heldByThisProcess,

559+

reclaimLockWithoutStarttime: true,

560+

});

561+

return await shouldReclaimContendedLockFile(lockPath, inspected, staleMs, nowMs);

562+

},

563+

});

564+

return { release: lock.release };

565+

} catch (err) {

566+

if (isFileLockError(err, "file_lock_stale")) {

567+

const staleLockPath = (err as { lockPath?: string }).lockPath ?? lockPath;

568+

if (

569+

await removeReportedStaleLockIfStillStale({

570+

lockPath: staleLockPath,

571+

normalizedSessionFile,

572+

staleMs,

573+

})

574+

) {

575+

continue;

518576

}

519-

return lockPayload as Record<string, unknown>;

520-

},

521-

shouldReclaim: async ({ payload, nowMs, heldByThisProcess }) => {

522-

const inspected = inspectLockPayloadForSession({

523-

payload: payload as LockFilePayload | null,

524-

staleMs,

525-

nowMs,

526-

heldByThisProcess,

527-

reclaimLockWithoutStarttime: true,

528-

});

529-

return await shouldReclaimContendedLockFile(lockPath, inspected, staleMs, nowMs);

530-

},

531-

});

532-

return { release: lock.release };

533-

} catch (err) {

534-

if ((err as { code?: unknown }).code !== "file_lock_timeout") {

535-

throw err;

577+

}

578+

if (!isFileLockError(err, "file_lock_timeout")) {

579+

throw err;

580+

}

581+

const timeoutLockPath = (err as { lockPath?: string }).lockPath ?? lockPath;

582+

const payload = await readLockPayload(timeoutLockPath);

583+

const owner = typeof payload?.pid === "number" ? `pid=${payload.pid}` : "unknown";

584+

throw new SessionWriteLockTimeoutError({ timeoutMs, owner, lockPath: timeoutLockPath });

536585

}

537-

const timeoutLockPath = (err as { lockPath?: string }).lockPath ?? lockPath;

538-

const payload = await readLockPayload(timeoutLockPath);

539-

const owner = typeof payload?.pid === "number" ? `pid=${payload.pid}` : "unknown";

540-

throw new SessionWriteLockTimeoutError({ timeoutMs, owner, lockPath: timeoutLockPath });

541586

}

542587

}

543588