openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
49 lines (48 loc) • 1.91 kB
JavaScript
import { existsSync, statSync } from "node:fs";
import { join } from "node:path";
import { parentPort, workerData } from "node:worker_threads";
import { DatabaseSync } from "node:sqlite";
//#region src/infra/bun-sqlite-close-probe.worker.ts
const retained = [];
parentPort?.on("message", () => retained.length);
function probe() {
const e = workerData;
const t = new DatabaseSync(join(e, "unrelated.sqlite"));
retained.push(t);
t.exec("CREATE TABLE unrelated(x); INSERT INTO unrelated VALUES (42)");
for (const n of ["close", "dispose"]) {
const r = join(e, `${n}.sqlite`);
const i = new DatabaseSync(r);
retained.push(i);
if (i.prepare("PRAGMA journal_mode=WAL").get()?.journal_mode !== "wal") return "SQLite close check cannot establish WAL mode";
i.exec("PRAGMA wal_autocheckpoint=0; CREATE TABLE data(x); INSERT INTO data VALUES (1),(2),(3)");
const a = i.prepare("SELECT x FROM data");
a.get();
const o = i.prepare("SELECT x FROM data");
const s = o.iterate();
s.next();
retained.push(a, o, s, i.prepare("SELECT x FROM data"));
const c = i.prepare("PRAGMA page_count").get()?.page_count;
const l = i.prepare("PRAGMA page_size").get()?.page_size;
const u = ["-wal", "-shm"].map((e) => r + e);
if (!u.every(existsSync) || typeof c !== "number" || typeof l !== "number") return "SQLite close check WAL preconditions are unavailable";
if (n === "close") i.close();
else i[Symbol.dispose]();
if (u.some(existsSync) || statSync(r).size !== c * l) return `SQLite ${n} retained native WAL resources`;
let d = false;
try {
a.get();
} catch {
d = true;
}
if (!d || t.prepare("SELECT x FROM unrelated").get()?.x !== 42) return `SQLite ${n} failed statement or sibling isolation`;
}
return "";
}
try {
parentPort?.postMessage(probe(), []);
} catch (e) {
parentPort?.postMessage(`SQLite close check failed: ${String(e)}`, []);
}
//#endregion
export {};