Skip to content

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

fix: bound connector polling after failed status reads

Afloatfee09f4d86ad
diff --git a/src/lib/connector-poll-failures.test.ts b/src/lib/connector-poll-failures.test.ts
new file mode 100644
index 0000000000..e26ebae3a1
--- /dev/null
+++ b/src/lib/connector-poll-failures.test.ts
@@ -0,0 +1,45 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { CONNECTOR_POLL_MS, CONNECTOR_POLL_TRIES, pollConnector } from "./connector-poll";
+
+beforeEach(() => vi.useFakeTimers());
+afterEach(() => vi.useRealTimers());
+
+describe("a Connect whose status request fails", () => {
+ it("ends after the attempt budget even when every read rejects", async () => {
+ const read = vi.fn(async () => { throw new Error("connection unavailable"); });
+ const onEnd = vi.fn();
+ pollConnector(undefined, read, onEnd);
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS * CONNECTOR_POLL_TRIES);
+ expect(onEnd).toHaveBeenCalledExactlyOnceWith("timed_out");
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS * 10);
+ expect(read).toHaveBeenCalledTimes(CONNECTOR_POLL_TRIES);
+ });
+
+ it("recovers from a rejected read when the next status confirms the connection", async () => {
+ const read = vi.fn().mockRejectedValueOnce(new Error("temporary outage")).mockResolvedValue({ connected: true });
+ const onEnd = vi.fn();
+ pollConnector(undefined, read, onEnd);
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS * 2);
+ expect(onEnd).toHaveBeenCalledExactlyOnceWith("connected");
+ });
+
+ it("handles a synchronous status-reader failure within the same attempt budget", async () => {
+ const onEnd = vi.fn();
+ pollConnector(undefined, () => { throw new Error("reader unavailable"); }, onEnd);
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS * CONNECTOR_POLL_TRIES);
+ expect(onEnd).toHaveBeenCalledExactlyOnceWith("timed_out");
+ });
+
+ it("ignores a late rejection after the panel stops polling", async () => {
+ let fail!: (error: Error) => void;
+ const onEnd = vi.fn();
+ const stop = pollConnector(undefined, () => new Promise((_resolve, reject) => { fail = reject; }), onEnd);
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS);
+ stop();
+ fail(new Error("late response failure"));
+ await vi.advanceTimersByTimeAsync(CONNECTOR_POLL_MS * CONNECTOR_POLL_TRIES);
+ expect(onEnd).not.toHaveBeenCalled();
+ expect(vi.getTimerCount()).toBe(0);
+ });
+});
diff --git a/src/lib/connector-poll.ts b/src/lib/connector-poll.ts
index 8a837928c9..e90a7942d5 100644
--- a/src/lib/connector-poll.ts
+++ b/src/lib/connector-poll.ts
@@ -32,7 +32,7 @@ export function pollConnector(
clearInterval(timer);
};
const timer = setInterval(() => {
- void read().then((state) => {
+ void Promise.resolve().then(read).catch(() => undefined).then((state) => {
if (ended) return;
const result = connectorPollEnd(before, state, ++tries);
if (!result) return;