Skip to content

Commit f5eea31

Browse files
ayaangazaliobviyus
andauthored
fix(state): tolerate disappearing SQLite sidecars (#125451)
Keep shared state database opens alive when SQLite removes a transient WAL, shared-memory, or rollback-journal sidecar during permission hardening. Ignore only sidecar `ENOENT` after a positive existence check. Keep main database loss and every other permission error fatal. Co-authored-by: Ayaan Gazali <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent e1f4342 commit f5eea31

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/state/openclaw-state-db-permissions.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync, mkdirSync } from "node:fs";
22
import path from "node:path";
33
import { createDedupeCache } from "../infra/dedupe.js";
4+
import { hasErrnoCode } from "../infra/errno.js";
45
import { applyPrivateModeSync } from "../infra/private-mode.js";
56
import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js";
67
import { createSubsystemLogger } from "../logging/subsystem.js";
@@ -48,7 +49,18 @@ export function ensureOpenClawStatePermissions(pathname: string, env: NodeJS.Pro
4849
}
4950
for (const candidate of resolveSqliteDatabaseFilePaths(pathname)) {
5051
if (existsSync(candidate)) {
51-
bestEffortChmodSync(candidate, OPENCLAW_STATE_FILE_MODE);
52+
try {
53+
bestEffortChmodSync(candidate, OPENCLAW_STATE_FILE_MODE);
54+
} catch (error) {
55+
// SQLite removes -wal/-shm at checkpoint or close, so a concurrent opener
56+
// can delete a sidecar between this check and the chmod. A vanished sidecar
57+
// has nothing left to harden. The main database keeps its fail-loud
58+
// boundary: swallowing its ENOENT would fall through to an open that
59+
// creates a fresh empty database instead of surfacing the loss.
60+
if (candidate === pathname || !hasErrnoCode(error, "ENOENT")) {
61+
throw error;
62+
}
63+
}
5264
}
5365
}
5466
}

‎src/state/openclaw-state-db.permissions.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@ const chmodFailHook = vi.hoisted(() => ({
1313
error: undefined as Error | undefined,
1414
calls: 0,
1515
failProbe: true,
16+
removeTargetSuffix: undefined as string | undefined,
17+
targets: [] as string[],
1618
}));
1719

1820
vi.mock("node:fs", async (importOriginal) => {
1921
const actual = await importOriginal<typeof import("node:fs")>();
2022
const chmodSync: typeof actual.chmodSync = ((target: unknown, mode: unknown) => {
2123
chmodFailHook.calls += 1;
24+
chmodFailHook.targets.push(String(target));
25+
if (
26+
chmodFailHook.removeTargetSuffix &&
27+
String(target).endsWith(chmodFailHook.removeTargetSuffix)
28+
) {
29+
// Remove the file after existsSync reaches chmod to reproduce the exact race.
30+
actual.unlinkSync(String(target));
31+
}
2232
const isProbe = String(target).includes(".openclaw-chmod-probe-");
2333
if (chmodFailHook.error && (chmodFailHook.failProbe || !isProbe)) {
2434
throw chmodFailHook.error;
@@ -53,6 +63,8 @@ describe("state database permission hardening without chmod support", () => {
5363
chmodFailHook.error = undefined;
5464
chmodFailHook.calls = 0;
5565
chmodFailHook.failProbe = true;
66+
chmodFailHook.removeTargetSuffix = undefined;
67+
chmodFailHook.targets = [];
5668
closeOpenClawStateDatabaseForTest();
5769
if (stateDir) {
5870
fs.rmSync(stateDir, { recursive: true, force: true });
@@ -135,6 +147,40 @@ describe("state database permission hardening without chmod support", () => {
135147
);
136148
});
137149

150+
it.each(["-wal", "-shm", "-journal"])(
151+
"opens when the %s sidecar disappears before chmod",
152+
(suffix) => {
153+
stateDir = fs.mkdtempSync(join(tmpdir(), "openclaw-state-chmod-"));
154+
const options = { env: { OPENCLAW_STATE_DIR: stateDir } };
155+
openOpenClawStateDatabase(options);
156+
closeOpenClawStateDatabaseForTest();
157+
const sidecarPath = join(stateDir, "state", `openclaw.sqlite${suffix}`);
158+
fs.writeFileSync(sidecarPath, "");
159+
chmodFailHook.removeTargetSuffix = suffix;
160+
161+
const database = openOpenClawStateDatabase(options);
162+
163+
expect(database.db.isOpen).toBe(true);
164+
expect(fs.existsSync(sidecarPath)).toBe(false);
165+
expect(chmodFailHook.targets).toContain(sidecarPath);
166+
},
167+
);
168+
169+
it("rethrows when the main database vanishes between the existence check and the chmod", () => {
170+
// resolveSqliteDatabaseFilePaths lists the unsuffixed database first. Losing
171+
// it must stay fatal: swallowing that ENOENT would fall through to a SQLite
172+
// open that creates a fresh empty database instead of surfacing the loss.
173+
stateDir = fs.mkdtempSync(join(tmpdir(), "openclaw-state-chmod-"));
174+
const options = { env: { OPENCLAW_STATE_DIR: stateDir } };
175+
openOpenClawStateDatabase(options);
176+
closeOpenClawStateDatabaseForTest();
177+
chmodFailHook.removeTargetSuffix = "openclaw.sqlite";
178+
179+
expect(() => openOpenClawStateDatabase(options)).toThrow(/ENOENT/);
180+
// Guards against a vacuous pass if the main file were skipped before chmod.
181+
expect(chmodFailHook.targets.some((target) => target.endsWith("openclaw.sqlite"))).toBe(true);
182+
});
183+
138184
it("repairs the schema when chmodSync throws ENOTSUP", () => {
139185
stateDir = fs.mkdtempSync(join(tmpdir(), "openclaw-state-chmod-"));
140186
openOpenClawStateDatabase({ env: { OPENCLAW_STATE_DIR: stateDir } });

0 commit comments

Comments
 (0)