Skip to content

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

Do not refresh a shared computer from a poll whose client has gone.

Ryan3e5eca441620
diff --git a/server/shared-computers.test.ts b/server/shared-computers.test.ts
index 1c05835449..300cf9289e 100644
--- a/server/shared-computers.test.ts
+++ b/server/shared-computers.test.ts
@@ -326,6 +326,26 @@ describe("owner partition (audit H1, H2)", () => {
broker.close();
rmSync(dir, { recursive: true, force: true });
});
+ it("a poll that ends after the desktop pairs again does not hand the next job to the old registration", async () => {
+ vi.useFakeTimers({ now: Date.now() });
+ const broker = new SharedComputers(id => id === "old" || id === "new");
+ const computer = registration({ computer: true });
+ broker.register(computer, { session: "old", person: alice.person }, secret);
+ const gone = new AbortController();
+ const dropped = broker.poll(computer.id, "old", secret, gone.signal);
+ await vi.advanceTimersByTimeAsync(5_000);
+ broker.register(computer, { session: "new", person: alice.person }, other);
+ await vi.advanceTimersByTimeAsync(1);
+ gone.abort();
+ await expect(dropped).resolves.toBeNull();
+ const operation = { computer_id: computer.id, action: "computer_tools" as const };
+ const result = broker.request(operation, alice.person, () => true);
+ const job = await broker.poll(computer.id, "new", other);
+ expect(job?.operation).toEqual(operation);
+ broker.complete(computer.id, "new", other, job!.id, { ok: true });
+ await expect(result).resolves.toEqual({ ok: true });
+ broker.close();
+ });
it("a second session cannot squat a lent computer's id, even after its owner goes offline", async () => {
vi.useFakeTimers();
const broker = new SharedComputers(() => true);
diff --git a/server/shared-computers.ts b/server/shared-computers.ts
index c95b2b2ca7..ace06a61f1 100644
--- a/server/shared-computers.ts
+++ b/server/shared-computers.ts
@@ -367,8 +367,12 @@ export class SharedComputers {
entry.wake = wake;
});
this.authorize(id, session, secret);
- entry.seen = Date.now();
- this.lent?.seen(entry.owner.person, id);
+ // A poll whose client already left, or that a newer poll replaced, must not mark this registration
+ // just seen. After a re-pair that would make the old session look like the live one.
+ if (entry.polls === mine && !gone?.aborted) {
+ entry.seen = Date.now();
+ this.lent?.seen(entry.owner.person, id);
+ }
return next();
}
private send(entry: Computer, job: Job) {