Skip to content

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

fix(routines): retire pending progress when its folder is removed

Afloat1e32a2f466cf
diff --git a/server/routine-progress-save.test.ts b/server/routine-progress-save.test.ts
index 4292727aee..edbe696a9b 100644
--- a/server/routine-progress-save.test.ts
+++ b/server/routine-progress-save.test.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, rmSync } from "node:fs";
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
@@ -60,3 +60,25 @@ it("a removed progress directory is not recreated by a later flush", () => {
progress.flush();
expect(write).not.toHaveBeenCalled();
});
+
+it("does not recreate a directory removed after a timed save failed", () => {
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ const file = join(directory, "routines.json");
+ const write = vi.fn(() => {
+ mkdirSync(directory, { recursive: true });
+ writeFileSync(file, JSON.stringify({ progress: "old data" }));
+ }).mockImplementationOnce(() => { throw new Error("disk unavailable"); });
+ const progress = new RunProgressSave(() => file, write, 100);
+ progress.soon();
+ vi.advanceTimersByTime(100);
+ expect(write).toHaveBeenCalledTimes(1);
+ rmSync(directory, { recursive: true });
+ progress.flush();
+ expect(existsSync(directory)).toBe(false);
+ expect(write).toHaveBeenCalledTimes(1);
+ // Removing the directory retired this pending state, even if a new data
+ // directory is later created at the same path.
+ mkdirSync(directory);
+ progress.flush();
+ expect(existsSync(file)).toBe(false);
+});
diff --git a/server/routine-progress-save.ts b/server/routine-progress-save.ts
index 93e51c5856..d645a14840 100644
--- a/server/routine-progress-save.ts
+++ b/server/routine-progress-save.ts
@@ -46,6 +46,10 @@ export class RunProgressSave {
/** Writes what is waiting now, if anything is. */
flush(): void {
if (!this.pending) return;
+ if (!existsSync(dirname(this.file()))) {
+ this.written();
+ return;
+ }
clearTimeout(this.timer);
this.timer = undefined;
this.write();