Skip to content

Commit 978f9b1

Browse files
fix(test): zalo lifecycle suites fail Windows teardown with EBUSY on agent state DB (#119809)
* fix(test): close zalo agent state databases before lifecycle temp-dir removal (#119796) * fix(test): close zalouser agent state databases before ingress temp-dir removal (#119796) * 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 f5add81 commit 978f9b1

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎extensions/zalo/src/monitor.lifecycle.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
createRuntimeEnv,
1313
setActivePluginRegistry,
1414
} from "openclaw/plugin-sdk/plugin-test-runtime";
15+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
1516
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1617
import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
1718
import type { ResolvedZaloAccount } from "./accounts.js";
@@ -98,9 +99,12 @@ describe("monitorZaloProvider lifecycle", () => {
9899
getUpdatesMock.mockReset();
99100
getUpdatesMock.mockImplementation(() => new Promise(() => {}));
100101
setActivePluginRegistry(createEmptyPluginRegistry());
102+
// Agent close releases leases through shared state; closing shared state first
103+
// can reopen it during teardown and leave Windows handles under the state dir.
104+
closeOpenClawAgentDatabasesForTest();
101105
closeOpenClawStateDatabaseForTest();
102106
if (testStateDir) {
103-
await fs.rm(testStateDir, { recursive: true, force: true });
107+
await fs.rm(testStateDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 25 });
104108
testStateDir = undefined;
105109
}
106110
if (previousStateDir === undefined) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Zalo tests cover lifecycle teardown release of per-agent state databases.
2+
import { openOpenClawAgentDatabase } from "openclaw/plugin-sdk/sqlite-runtime-testing";
3+
import { afterEach, describe, expect, it } from "vitest";
4+
import { resetLifecycleTestState } from "./monitor-mocks-test-support.js";
5+
6+
describe("resetLifecycleTestState", () => {
7+
afterEach(async () => {
8+
await resetLifecycleTestState();
9+
});
10+
11+
it("closes per-agent state databases so teardown can remove the state dir", async () => {
12+
// A processed inbound update opens the per-agent DB through the real session
13+
// accessor inside the envelope builder; teardown must release that handle or
14+
// Windows cannot remove the temp state dir (EBUSY, issue #119796).
15+
const handle = openOpenClawAgentDatabase({ agentId: "main" });
16+
expect(handle.db.isOpen).toBe(true);
17+
18+
await resetLifecycleTestState();
19+
20+
expect(handle.db.isOpen).toBe(false);
21+
});
22+
});

‎extensions/zalo/src/test-support/monitor-mocks-test-support.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
createRuntimeEnv,
1313
setActivePluginRegistry,
1414
} from "openclaw/plugin-sdk/plugin-test-runtime";
15+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
1516
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
1617
import { vi, type Mock } from "vitest";
1718
import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
@@ -114,9 +115,17 @@ const importCachedWebhookModule = createLazyRuntimeModule(
114115
);
115116

116117
export async function resetLifecycleTestState() {
118+
// Agent close releases leases through shared state; closing shared state first
119+
// can reopen it during teardown and leave Windows handles under the state dir.
120+
closeOpenClawAgentDatabasesForTest();
117121
closeOpenClawStateDatabaseForTest();
118122
if (lifecycleStateDir) {
119-
await fs.rm(lifecycleStateDir, { recursive: true, force: true });
123+
await fs.rm(lifecycleStateDir, {
124+
recursive: true,
125+
force: true,
126+
maxRetries: 20,
127+
retryDelay: 25,
128+
});
120129
lifecycleStateDir = undefined;
121130
}
122131
if (previousLifecycleStateDir === undefined) {
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
closeOpenClawStateDatabaseForTest,
66
createChannelIngressQueueForTests,
77
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
8+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
89
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
910
import { expect, vi } from "vitest";
1011
import type { createZalouserIngressMonitor } from "./ingress.js";
@@ -75,13 +76,19 @@ export async function withZalouserIngressTestQueue<T>(
7576
try {
7677
return await fn(queue);
7778
} finally {
79+
// Agent close releases leases through shared state; closing shared state first
80+
// 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.
84+
closeOpenClawAgentDatabasesForTest();
85+
closeOpenClawStateDatabaseForTest();
86+
await fs.rm(stateDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 25 });
7887
if (previousStateDir === undefined) {
7988
delete process.env.OPENCLAW_STATE_DIR;
8089
} else {
8190
process.env.OPENCLAW_STATE_DIR = previousStateDir;
8291
}
83-
closeOpenClawStateDatabaseForTest();
84-
await fs.rm(stateDir, { recursive: true, force: true });
8592
}
8693
}
8794

0 commit comments

Comments
 (0)