Skip to content

Commit a0a652a

Browse files
fix(test): close zalouser fixture databases before restoring parent state env (#119796)
ClawSweeper re-review finding: the ingress fixture restored OPENCLAW_STATE_DIR before closing cached per-agent databases, so lease release could open a shared-state write against the parent fixture env. Move both closes ahead of the env restore (canonical fixture order) and add a nested-environment regression asserting the parent state dir stays untouched.
1 parent 88cf58f commit a0a652a

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Teardown must close fixture-owned databases before restoring the parent state env.
2+
import fs from "node:fs/promises";
3+
import path from "node:path";
4+
import { openOpenClawAgentDatabase } from "openclaw/plugin-sdk/sqlite-runtime-testing";
5+
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
6+
import { describe, expect, it } from "vitest";
7+
import { withZalouserIngressTestQueue } from "./ingress.test-support.js";
8+
9+
describe("withZalouserIngressTestQueue", () => {
10+
it("does not write through the parent state environment during teardown", async () => {
11+
const parentDir = await fs.realpath(
12+
await fs.mkdtemp(path.join(resolvePreferredOpenClawTmpDir(), "openclaw-zalouser-parent-")),
13+
);
14+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
15+
process.env.OPENCLAW_STATE_DIR = parentDir;
16+
try {
17+
await withZalouserIngressTestQueue(async () => {
18+
// A processed inbound message opens the per-agent DB under the child state
19+
// dir; releasing its lease after env restoration would open a shared-state
20+
// write transaction against this parent dir instead (PR #119809 review).
21+
openOpenClawAgentDatabase({ agentId: "main" });
22+
});
23+
// With the correct close-before-restore order the parent state dir stays
24+
// untouched; the old order created openclaw.sqlite* files here.
25+
expect(await fs.readdir(parentDir)).toEqual([]);
26+
} finally {
27+
if (previousStateDir === undefined) {
28+
delete process.env.OPENCLAW_STATE_DIR;
29+
} else {
30+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
31+
}
32+
await fs.rm(parentDir, { recursive: true, force: true });
33+
}
34+
});
35+
});

‎extensions/zalouser/src/ingress.test-support.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,19 @@ export async function withZalouserIngressTestQueue<T>(
7676
try {
7777
return await fn(queue);
7878
} finally {
79-
if (previousStateDir === undefined) {
80-
delete process.env.OPENCLAW_STATE_DIR;
81-
} else {
82-
process.env.OPENCLAW_STATE_DIR = previousStateDir;
83-
}
8479
// Agent close releases leases through shared state; closing shared state first
8580
// can reopen it during teardown and leave Windows handles under the state dir.
81+
// Both closes must run before OPENCLAW_STATE_DIR is restored: cached agent
82+
// databases captured the child env, and lease release after restoration would
83+
// write through the parent fixture's shared state instead.
8684
closeOpenClawAgentDatabasesForTest();
8785
closeOpenClawStateDatabaseForTest();
8886
await fs.rm(stateDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 25 });
87+
if (previousStateDir === undefined) {
88+
delete process.env.OPENCLAW_STATE_DIR;
89+
} else {
90+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
91+
}
8992
}
9093
}
9194

0 commit comments

Comments
 (0)