Skip to content

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

fix(browser): reject event waits on already detached sessions

Afloat6656cb4104d4
diff --git a/server/browser/cdp/connection.test.ts b/server/browser/cdp/connection.test.ts
index dc85c7680e..8c1894b2f3 100644
--- a/server/browser/cdp/connection.test.ts
+++ b/server/browser/cdp/connection.test.ts
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { PassThrough } from "node:stream";
import { fileURLToPath } from "node:url";
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { CdpConnection } from "./connection.ts";
import { CdpProtocolError, DriverError } from "./errors.ts";
@@ -135,6 +135,37 @@ describe("CdpConnection", () => {
await expect(child.send("Page.enable")).rejects.toMatchObject({ code: "closed" });
});
+ it("rejects a new event wait on an already detached renderer without registering a timer", async () => {
+ vi.useFakeTimers();
+ const browser = fakeBrowser();
+ const connection = new CdpConnection(browser.transport);
+ try {
+ browser.reply({ method: "Target.attachedToTarget", params: { sessionId: "S1", targetInfo: { targetId: "T1", type: "iframe" } } });
+ const child = connection.session("S1")!;
+ browser.reply({ method: "Target.detachedFromTarget", params: { sessionId: "S1" } });
+ await expect(child.waitFor("Page.loadEventFired")).rejects.toMatchObject({ code: "closed" });
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ connection.close();
+ vi.useRealTimers();
+ }
+ });
+
+ it("rejects a new browser event wait after the transport has closed", async () => {
+ vi.useFakeTimers();
+ const browser = fakeBrowser();
+ const connection = new CdpConnection(browser.transport);
+ try {
+ browser.drop("browser exited");
+ await expect(connection.root.waitFor("Target.targetCreated")).rejects.toBeInstanceOf(DriverError);
+ await expect(connection.root.waitFor("Target.targetCreated")).rejects.toMatchObject({ code: "closed" });
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ connection.close();
+ vi.useRealTimers();
+ }
+ });
+
it("times a call out, and fails every call when the browser goes away", async () => {
const browser = fakeBrowser();
const connection = new CdpConnection(browser.transport, { defaultTimeoutMs: 20 });
diff --git a/server/browser/cdp/connection.ts b/server/browser/cdp/connection.ts
index 38d9e99796..5b29c58e95 100644
--- a/server/browser/cdp/connection.ts
+++ b/server/browser/cdp/connection.ts
@@ -208,6 +208,7 @@ export class CdpSession {
/** The next `method` event that `match` accepts; rejects on timeout or when the session goes. */
waitFor(method: string, { match = () => true, timeoutMs = 30_000 }: { match?: (params: Params) => boolean; timeoutMs?: number } = {}): Promise<Params> {
+ if (this.#detached) return Promise.reject(new DriverError("closed", `The ${this.kind} closed while waiting for ${method}.`));
return new Promise<Params>((resolve, reject) => {
const done = () => {
off();