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

推荐订阅源

博客园 - Franky
J
Java Code Geeks
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
L
LangChain Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
Martin Fowler
Martin Fowler
月光博客
月光博客
Y
Y Combinator Blog
U
Unit 42
D
Docker
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

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): retry transient stale session locks · opencl...
steipete · 2026-05-31 · via Recent Commits to openclaw:main

@@ -3,6 +3,7 @@ import fsSync from "node:fs";

33

import fs from "node:fs/promises";

44

import os from "node:os";

55

import path from "node:path";

6+

import { fileURLToPath } from "node:url";

67

import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";

78

import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

89

import { SessionWriteLockStaleError } from "./session-write-lock-error.js";

@@ -87,6 +88,19 @@ async function writeCurrentProcessLock(lockPath: string, extra?: Record<string,

8788

);

8889

}

899091+

function readFilePathToString(filePath: Parameters<typeof fs.readFile>[0]): string | undefined {

92+

if (typeof filePath === "string") {

93+

return filePath;

94+

}

95+

if (Buffer.isBuffer(filePath)) {

96+

return filePath.toString("utf8");

97+

}

98+

if (filePath instanceof URL) {

99+

return fileURLToPath(filePath);

100+

}

101+

return undefined;

102+

}

103+90104

async function withSymlinkedSessionPaths(

91105

run: (params: {

92106

sessionReal: string;

@@ -453,6 +467,157 @@ describe("acquireSessionWriteLock", () => {

453467

});

454468

});

455469470+

it("retries when a stale lock report disappears before diagnostics", async () => {

471+

await withTempSessionLockFile(async ({ sessionFile, lockPath }) => {

472+

const owner = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)", "openclaw"], {

473+

stdio: "ignore",

474+

});

475+

if (!owner.pid) {

476+

throw new Error("missing lock owner pid");

477+

}

478+

await fs.writeFile(

479+

lockPath,

480+

JSON.stringify({

481+

pid: owner.pid,

482+

createdAt: new Date(Date.now() - 120_000).toISOString(),

483+

}),

484+

"utf8",

485+

);

486+487+

const originalReadFile = fs.readFile.bind(fs);

488+

let lockReads = 0;

489+

const readFileSpy = vi.spyOn(fs, "readFile").mockImplementation((async (

490+

filePath,

491+

options,

492+

) => {

493+

const lockFilePath = readFilePathToString(filePath);

494+

if (lockFilePath && path.basename(lockFilePath) === path.basename(lockPath)) {

495+

lockReads += 1;

496+

if (lockReads === 3) {

497+

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

498+

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

499+

throw Object.assign(new Error("lock disappeared"), { code: "ENOENT" });

500+

}

501+

}

502+

return await originalReadFile(filePath, options as never);

503+

}) as typeof fs.readFile);

504+505+

try {

506+

const lock = await acquireSessionWriteLock({ sessionFile, timeoutMs: 500, staleMs: 10 });

507+

await lock.release();

508+

expect(lockReads).toBeGreaterThanOrEqual(3);

509+

await expectPathMissing(lockPath);

510+

} finally {

511+

readFileSpy.mockRestore();

512+

owner.kill("SIGTERM");

513+

}

514+

});

515+

});

516+517+

it("retries when a stale lock report is replaced before diagnostics", async () => {

518+

await withTempSessionLockFile(async ({ sessionFile, lockPath }) => {

519+

const owner = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)", "openclaw"], {

520+

stdio: "ignore",

521+

});

522+

if (!owner.pid) {

523+

throw new Error("missing lock owner pid");

524+

}

525+

await fs.writeFile(

526+

lockPath,

527+

JSON.stringify({

528+

pid: owner.pid,

529+

createdAt: new Date(Date.now() - 120_000).toISOString(),

530+

}),

531+

"utf8",

532+

);

533+534+

const originalReadFile = fs.readFile.bind(fs);

535+

let lockReads = 0;

536+

const readFileSpy = vi.spyOn(fs, "readFile").mockImplementation((async (

537+

filePath,

538+

options,

539+

) => {

540+

const lockFilePath = readFilePathToString(filePath);

541+

if (lockFilePath && path.basename(lockFilePath) === path.basename(lockPath)) {

542+

lockReads += 1;

543+

if (lockReads === 3) {

544+

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

545+

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

546+

await fs.writeFile(

547+

lockFilePath,

548+

JSON.stringify({ pid: owner.pid, createdAt: new Date().toISOString() }),

549+

"utf8",

550+

);

551+

setTimeout(() => {

552+

void fs.rm(lockFilePath, { force: true });

553+

}, 10);

554+

throw Object.assign(new Error("lock disappeared"), { code: "ENOENT" });

555+

}

556+

}

557+

return await originalReadFile(filePath, options as never);

558+

}) as typeof fs.readFile);

559+560+

try {

561+

const lock = await acquireSessionWriteLock({ sessionFile, timeoutMs: 500, staleMs: 10 });

562+

await lock.release();

563+

expect(lockReads).toBeGreaterThanOrEqual(3);

564+

await expectPathMissing(lockPath);

565+

} finally {

566+

readFileSpy.mockRestore();

567+

owner.kill("SIGTERM");

568+

}

569+

});

570+

});

571+572+

it("retries when a stale lock report is replaced by a fresh payload-less lock", async () => {

573+

await withTempSessionLockFile(async ({ sessionFile, lockPath }) => {

574+

const owner = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)", "openclaw"], {

575+

stdio: "ignore",

576+

});

577+

if (!owner.pid) {

578+

throw new Error("missing lock owner pid");

579+

}

580+

await fs.writeFile(

581+

lockPath,

582+

JSON.stringify({

583+

pid: owner.pid,

584+

createdAt: new Date(Date.now() - 120_000).toISOString(),

585+

}),

586+

"utf8",

587+

);

588+589+

const originalReadFile = fs.readFile.bind(fs);

590+

let lockReads = 0;

591+

const readFileSpy = vi.spyOn(fs, "readFile").mockImplementation((async (

592+

filePath,

593+

options,

594+

) => {

595+

const lockFilePath = readFilePathToString(filePath);

596+

if (lockFilePath && path.basename(lockFilePath) === path.basename(lockPath)) {

597+

lockReads += 1;

598+

if (lockReads === 3) {

599+

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

600+

await fs.writeFile(lockFilePath, "", "utf8");

601+

setTimeout(() => {

602+

void fs.rm(lockFilePath, { force: true });

603+

}, 10);

604+

}

605+

}

606+

return await originalReadFile(filePath, options as never);

607+

}) as typeof fs.readFile);

608+609+

try {

610+

const lock = await acquireSessionWriteLock({ sessionFile, timeoutMs: 800, staleMs: 10 });

611+

await lock.release();

612+

expect(lockReads).toBeGreaterThanOrEqual(3);

613+

await expectPathMissing(lockPath);

614+

} finally {

615+

readFileSpy.mockRestore();

616+

owner.kill("SIGTERM");

617+

}

618+

});

619+

});

620+456621

it("watchdog releases stale in-process locks", async () => {

457622

const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-lock-"));

458623

const stderrSpy = vi.spyOn(process.stderr, "write").mockImplementation(() => true);

@@ -600,6 +765,14 @@ describe("acquireSessionWriteLock", () => {

600765

});

601766

});

602767768+

it("preserves one acquire timeout budget across retries", () => {

769+

expect(testing.resolveRemainingAcquireTimeoutMs(500, 1_000, 1_125)).toBe(375);

770+

expect(testing.resolveRemainingAcquireTimeoutMs(500, 1_000, 1_500)).toBe(0);

771+

expect(testing.resolveRemainingAcquireTimeoutMs(Number.POSITIVE_INFINITY, 1_000, 9_000)).toBe(

772+

Number.POSITIVE_INFINITY,

773+

);

774+

});

775+603776

it("uses resolved stale policy when cleaning stale lock files", async () => {

604777

const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-lock-policy-"));

605778

const sessionsDir = path.join(root, "sessions");