Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(cache-sweep): handle queued operation rejections

Afloata25d0b49bbfe
diff --git a/server/cache-sweep/sweeper-failures.test.ts b/server/cache-sweep/sweeper-failures.test.ts
new file mode 100644
index 0000000000..bca0e89e87
--- /dev/null
+++ b/server/cache-sweep/sweeper-failures.test.ts
@@ -0,0 +1,18 @@
+import { spawnSync } from "node:child_process";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+
+it.each(["sweep", "emergency", "tick", "timer", "queue"])("contains a %s failure without losing queue order", (mode) => {
+ const dir = mkdtempSync(join(tmpdir(), "omb-cache-failure-"));
+ try {
+ const child = spawnSync(process.execPath, [
+ "--experimental-strip-types", "--unhandled-rejections=strict", "server/testing/cache-sweep-failures.ts", dir, mode,
+ ], { encoding: "utf8", timeout: 10_000 });
+ expect(child.status, child.stderr).toBe(0);
+ expect(child.stdout).toContain("fixture survived and recovered");
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
diff --git a/server/cache-sweep/sweeper.ts b/server/cache-sweep/sweeper.ts
index 4366ca2bae..6476acbd3b 100644
--- a/server/cache-sweep/sweeper.ts
+++ b/server/cache-sweep/sweeper.ts
@@ -294,8 +294,9 @@ export function createCacheSweeper(deps: CacheSweeperDeps) {
/** One at a time: a sweep, the emergency pass and a clear never run together. */
const serial = <T>(work: () => Promise<T>): Promise<T> => {
- const next = (running ?? Promise.resolve()).catch(() => {}).then(work);
- running = next.finally(() => { if (running === next) running = null; });
+ const next = (running ?? Promise.resolve()).catch(() => {}).then(work)
+ .finally(() => { if (running === next) running = null; });
+ running = next;
return next;
};
diff --git a/server/testing/cache-sweep-failures.ts b/server/testing/cache-sweep-failures.ts
new file mode 100644
index 0000000000..0bdc9899a4
--- /dev/null
+++ b/server/testing/cache-sweep-failures.ts
@@ -0,0 +1,83 @@
+import assert from "node:assert/strict";
+import { join } from "node:path";
+import { mock } from "node:test";
+import { setImmediate } from "node:timers/promises";
+
+import { TICK_EVERY_MS } from "../cache-sweep/limits.ts";
+import { createCacheSweeper, volumeOf } from "../cache-sweep/sweeper.ts";
+
+const [dir, mode] = process.argv.slice(2);
+assert.ok(dir);
+assert.ok(mode);
+const room = { total: 100, free: 100 };
+const deferred = () => {
+ let resolve!: () => void;
+ const promise = new Promise<void>((done) => { resolve = done; });
+ return { promise, resolve };
+};
+const gates = [deferred(), deferred(), deferred()];
+const logged = deferred();
+const failure = new Error("fixture volume failure");
+let calls = 0;
+const made = createCacheSweeper({
+ dataDir: dir, home: dir, env: { OMB_CLOUD_ROLE: "home", OMB_CACHE_SWEEP: "on" },
+ tools: () => false, audienceHomes: () => [], keep: () => [], supported: () => true,
+ volume: async () => {
+ const index = calls++;
+ if (mode === "queue") {
+ await gates[index]?.promise;
+ if (index === 0) throw failure;
+ } else if (index === 0) {
+ return volumeOf(join(dir, "missing-volume"));
+ }
+ return room;
+ },
+ log: (line) => {
+ if (line.startsWith("cache sweep failed:")) {
+ assert.match(line, /ENOENT/);
+ logged.resolve();
+ }
+ },
+});
+
+if (mode === "timer") {
+ mock.timers.enable({ apis: ["setInterval"] });
+ try {
+ made.start(() => false);
+ mock.timers.tick(TICK_EVERY_MS);
+ await logged.promise;
+ await setImmediate();
+ } finally {
+ made.stop();
+ mock.timers.reset();
+ }
+} else if (mode === "queue") {
+ const first = made.emergency();
+ const rejected = assert.rejects(first, (error) => error === failure);
+ const second = made.emergency();
+ await setImmediate();
+ assert.equal(calls, 1);
+ gates[0]!.resolve();
+ await rejected;
+ await setImmediate();
+ assert.equal(calls, 2);
+ const third = made.emergency();
+ await setImmediate();
+ assert.equal(calls, 2);
+ gates[1]!.resolve();
+ assert.deepEqual(await second, { skipped: "room" });
+ await setImmediate();
+ assert.equal(calls, 3);
+ gates[2]!.resolve();
+ assert.deepEqual(await third, { skipped: "room" });
+ await setImmediate();
+} else {
+ assert.ok(mode === "sweep" || mode === "emergency" || mode === "tick");
+ made.start(() => false);
+ made.stop();
+ await assert.rejects(made[mode](), { code: "ENOENT" });
+ await setImmediate();
+}
+assert.deepEqual(await made.emergency(), { skipped: "room" });
+await setImmediate();
+console.log("fixture survived and recovered");