Skip to content

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

fix: retire background connection checks on restart

Afloat782f0a3214c6
diff --git a/server/connection-health-check-restart.test.ts b/server/connection-health-check-restart.test.ts
new file mode 100644
index 0000000000..f158e561d3
--- /dev/null
+++ b/server/connection-health-check-restart.test.ts
@@ -0,0 +1,47 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { createBackgroundCheck } from "./connection-health-check.ts";
+
+describe("background connection checks across restarts", () => {
+ it("does not let a cancelled in-flight check create a second timer after restart", async () => {
+ let finish!: (outcome: string) => void;
+ const result = new Promise<string>((resolve) => { finish = resolve; });
+ const pending = new Set<() => void>();
+ const record = vi.fn();
+ const check = createBackgroundCheck({
+ due: () => ["server"], check: () => result, record,
+ asleep: () => false, wokeAt: () => null,
+ setTimer: (run) => { pending.add(run); return { cancel: () => { pending.delete(run); } }; },
+ });
+ check.start();
+ const first = [...pending][0]!;
+ pending.delete(first);
+ first();
+ check.stop();
+ check.start();
+ finish("ok");
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(record).not.toHaveBeenCalled();
+ expect(pending.size).toBe(1);
+ check.stop();
+ expect(pending.size).toBe(0);
+ });
+
+ it("ignores a timer callback from a previous start even if cancellation came too late", async () => {
+ const callbacks: Array<() => void> = [];
+ const probe = vi.fn(async () => "ok");
+ const check = createBackgroundCheck({
+ due: () => ["server"], check: probe, record: vi.fn(),
+ asleep: () => false, wokeAt: () => null,
+ setTimer: (run) => { callbacks.push(run); return { cancel: () => undefined }; },
+ });
+ check.start();
+ check.stop();
+ check.start();
+ callbacks[0]!();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(probe).not.toHaveBeenCalled();
+ expect(callbacks).toHaveLength(2);
+ check.stop();
+ });
+});
diff --git a/server/connection-health-check.ts b/server/connection-health-check.ts
index 80b86d2522..ad7734edbc 100644
--- a/server/connection-health-check.ts
+++ b/server/connection-health-check.ts
@@ -40,6 +40,7 @@ export function createBackgroundCheck<Ref, Outcome>(deps: BackgroundCheckDeps<Re
let timer: { cancel(): void } | null = null;
let running: AbortController | null = null;
let stopped = true;
+ let generation = 0;
/** One check, if one is due and the machine is awake for it. Resolves with what was checked, or null. */
const tick = async (): Promise<Ref | null> => {
@@ -58,10 +59,10 @@ export function createBackgroundCheck<Ref, Outcome>(deps: BackgroundCheckDeps<Re
running = null;
}
};
- const schedule = (ms: number) => {
+ const schedule = (ms: number, started: number) => {
timer = setTimer(() => {
- if (stopped) return;
- void tick().finally(() => { if (!stopped) schedule(jitteredWait(CHECK_EVERY_MS, random)); });
+ if (stopped || started !== generation) return;
+ void tick().finally(() => { if (!stopped && started === generation) schedule(jitteredWait(CHECK_EVERY_MS, random), started); });
}, ms);
};
@@ -69,7 +70,7 @@ export function createBackgroundCheck<Ref, Outcome>(deps: BackgroundCheckDeps<Re
start(): void {
if (!stopped) return;
stopped = false;
- schedule(FIRST_CHECK_MS + jitteredWait(CHECK_EVERY_MS, random));
+ schedule(FIRST_CHECK_MS + jitteredWait(CHECK_EVERY_MS, random), ++generation);
},
stop(): void {
stopped = true;