Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(ego-browser): stop a pending navigation once an unresponsive-Page wait ran out

Zhuowei Wange29cead53df0
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/page-pending-navigation.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/page-pending-navigation.mjs
index 954cfcf..a831281 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/page-pending-navigation.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/page-pending-navigation.mjs
@@ -1,8 +1,8 @@
// A main-frame navigation that has started but not committed suspends every
// renderer-bound CDP command until the new document commits or the navigation
// stops. These cases pin the harness behavior on such a Page: public calls
-// stay inside one bounded wait, the error names the Page and what to do, and
-// Page.stopLoading recovers it.
+// stay inside one bounded wait, then stop the navigation and say which
+// document the Page shows, so the next call answers at once.
export function pagePendingNavigationCase() {
return `
const task = await taskSpace(taskName);
@@ -57,13 +57,15 @@ export function pagePendingNavigationCase() {
);
// A navigation that goto did not start (a link click, a script, raw CDP)
- // can still be left pending; public calls then stay inside one bounded
- // wait and the error names the Page and what to do. Chromium answers
- // Page.navigate only once the response arrives, so the raw call is given
- // up on while the navigation stays pending.
- await page
- .cdp("Page.navigate", { url: heldUrl + "&raw=1" }, { timeout: 500 })
- .catch(() => {});
+ // can still be left pending; a public call then stays inside one bounded
+ // wait, stops that navigation, and says what the Page shows now. Chromium
+ // answers Page.navigate only once the response arrives, so the raw call
+ // is given up on while the navigation stays pending.
+ const leavePending = (suffix) =>
+ page
+ .cdp("Page.navigate", { url: heldUrl + suffix }, { timeout: 500 })
+ .catch(() => {});
+ await leavePending("&raw=1");
const snapshotStartedAt = Date.now();
let snapshotError;
try {
@@ -88,19 +90,41 @@ export function pagePendingNavigationCase() {
"(" + baseUrl + "/",
"the error names the Page URL"
);
+ assertEqual(
+ snapshotError?.stopped,
+ true,
+ "the pending navigation is stopped once the wait ran out"
+ );
+ assertIncludes(
+ snapshotError?.message,
+ "shows " + baseUrl + "/nav-target",
+ "the error says which document the Page shows now"
+ );
assertIncludes(
snapshotError?.message,
- 'page.cdp("Page.stopLoading")',
- "the error tells the agent how to recover"
+ "page.goto(url, { timeout })",
+ "the error tells the agent how to load the pending document"
);
assert(
snapshotElapsed < 20_000,
"snapshot gives up after one bounded wait, not one per setup command: " +
snapshotElapsed
);
+ const afterStopStartedAt = Date.now();
+ assertIncludes(
+ await page.snapshot(),
+ "Navigation target",
+ "the next call answers on the previous document"
+ );
+ assert(
+ Date.now() - afterStopStartedAt < 5_000,
+ "the next call does not wait another full period: " +
+ (Date.now() - afterStopStartedAt)
+ );
// goto on such a Page replaces the pending navigation with its own, times
// out within its budget, and leaves the Page usable again.
+ await leavePending("&raw=2");
const secondGotoStartedAt = Date.now();
let secondGotoError;
try {
@@ -175,12 +199,22 @@ export function pagePendingNavigationFreshProcessCase() {
elapsed
);
- await page.cdp("Page.stopLoading");
+ assertEqual(
+ error?.stopped,
+ true,
+ "the new process stops the pending navigation after its wait"
+ );
+ const recoveredStartedAt = Date.now();
assertIncludes(
await page.snapshot(),
"Navigation target",
"the Page recovers in the new process as well"
);
+ assert(
+ Date.now() - recoveredStartedAt < 5_000,
+ "the next call in the new process answers at once: " +
+ (Date.now() - recoveredStartedAt)
+ );
await page.close();
`;
}
diff --git a/package/ego-browser/skill-body/SKILL.md b/package/ego-browser/skill-body/SKILL.md
index f8b6be7..f64ed54 100644
--- a/package/ego-browser/skill-body/SKILL.md
+++ b/package/ego-browser/skill-body/SKILL.md
@@ -222,9 +222,10 @@ inside the callback or pass one JSON-serializable value as its second argument.
`goto()` and `reload()` stop a navigation that has not committed by their
timeout, so the Page stays usable on its previous document; retry with a longer
-timeout. A Page whose navigation is pending for any other reason fails with
-`EGO_PAGE_UNRESPONSIVE`; the error says how to recover
-(`page.cdp("Page.stopLoading")`, a new Page).
+timeout. Any other call on a Page whose navigation is pending (after a link
+click, for example) waits one bounded period, then fails with
+`EGO_PAGE_UNRESPONSIVE` and stops that navigation; the error says which
+document the Page shows now and how to load the pending one.
Work efficiently:
diff --git a/package/ego-browser/src/browser-runtime.ts b/package/ego-browser/src/browser-runtime.ts
index 6f0b56c..be15359 100644
--- a/package/ego-browser/src/browser-runtime.ts
+++ b/package/ego-browser/src/browser-runtime.ts
@@ -86,6 +86,12 @@ export class PageUnresponsiveError extends CdpRequestTimeoutError {
override readonly code = "EGO_PAGE_UNRESPONSIVE";
readonly waitedMs: number;
readonly targetId?: string;
+ /**
+ * The pending navigation was stopped once the wait ran out and the Page
+ * answers again; `currentUrl` is what its main frame showed afterwards.
+ */
+ stopped = false;
+ currentUrl?: string;
constructor(
method: string,
diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjs
index e47007c..d0264f2 100644
--- a/package/ego-browser/src/page-model.test.mjs
+++ b/package/ego-browser/src/page-model.test.mjs
@@ -7584,6 +7584,102 @@ test("snapshot names the Page and the next step when the Page does not answer",
});
});
+test("snapshot stops a pending navigation after its wait and says what the Page shows", async () => {
+ await withFixture(async (fixture) => {
+ const baseCdp = fixture.services.cdp;
+ const stops = [];
+ const task = taskForRound(fixture, "round-a", {
+ async ensureFrameSessions() {
+ throw new PageUnresponsiveError(
+ "Page.getFrameTree",
+ 15_000,
+ "target-1",
+ "session-1",
+ );
+ },
+ async cdp(method, params, sessionId, timeoutMs) {
+ if (method === "Target.getTargetInfo") {
+ return { targetInfo: { url: "https://example.test/pending" } };
+ }
+ if (method === "Page.stopLoading") {
+ stops.push({ sessionId, timeoutMs });
+ return {};
+ }
+ if (method === "Page.getFrameTree" && stops.length > 0) {
+ return {
+ result: {
+ frameTree: {
+ frame: {
+ id: "frame-1",
+ loaderId: "loader-old",
+ url: "https://example.test/previous",
+ },
+ },
+ },
+ };
+ }
+ return baseCdp(method, params, sessionId, timeoutMs);
+ },
+ });
+ const page = await openTestPage(task, "https://example.test/previous");
+
+ await assert.rejects(
+ () => page.snapshot(),
+ (error) => {
+ assert.equal(error.code, "EGO_PAGE_UNRESPONSIVE");
+ assert.equal(error.stopped, true);
+ assert.equal(error.currentUrl, "https://example.test/previous");
+ assert.match(
+ error.message,
+ /^Page p1 \(https:\/\/example\.test\/pending\) did not answer Page\.getFrameTree within 15000ms: a main-frame navigation had started but not committed\. It was stopped once that wait ran out, so the Page answers again and shows https:\/\/example\.test\/previous\./,
+ );
+ assert.match(error.message, /Retry the call/);
+ assert.match(error.message, /page\.goto\(url, \{ timeout \}\)/);
+ assert.doesNotMatch(error.message, /page\.cdp\("Page\.stopLoading"\)/);
+ return true;
+ },
+ );
+ assert.deepEqual(stops, [{ sessionId: "session-1", timeoutMs: 1_000 }]);
+ });
+});
+
+test("snapshot names a probably hung renderer when stopping does not help", async () => {
+ await withFixture(async (fixture) => {
+ const baseCdp = fixture.services.cdp;
+ const task = taskForRound(fixture, "round-a", {
+ async ensureFrameSessions() {
+ throw new PageUnresponsiveError(
+ "Page.getFrameTree",
+ 15_000,
+ "target-1",
+ "session-1",
+ );
+ },
+ async cdp(method, params, sessionId, timeoutMs) {
+ if (method === "Page.stopLoading") {
+ throw new CdpRequestTimeoutError(method, timeoutMs, sessionId);
+ }
+ return baseCdp(method, params, sessionId, timeoutMs);
+ },
+ });
+ const page = await openTestPage(task, "https://example.test/hung");
+
+ await assert.rejects(
+ () => page.snapshot(),
+ (error) => {
+ assert.equal(error.code, "EGO_PAGE_UNRESPONSIVE");
+ assert.equal(error.stopped, false);
+ assert.match(
+ error.message,
+ /did not answer Page\.getFrameTree within 15000ms, and still did not answer after Page\.stopLoading, so its renderer is probably hung\./,
+ );
+ assert.match(error.message, /task\.newPage\(\)/);
+ return true;
+ },
+ );
+ });
+});
+
test("snapshot describes a Page that stops answering after the first document check", async () => {
await withFixture(async (fixture) => {
let calls = 0;
diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.ts
index f9cab75..fba745a 100644
--- a/package/ego-browser/src/page-model.ts
+++ b/package/ego-browser/src/page-model.ts
@@ -624,6 +624,9 @@ const MISSING_TARGET_PROBE_TIMEOUT_MS = 1_000;
// event ahead of the response, so no settle delay is needed.
const POPUP_SYNC_TIMEOUT_MS = 1_000;
const PAGE_UNRESPONSIVE_URL_TIMEOUT_MS = 1_000;
+// Page.stopLoading is answered by the browser process; the frame-tree read
+// afterwards is what shows whether the renderer answers again.
+const PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS = 1_000;
// The native tab list can lag the CDP target event by a few milliseconds.
const POPUP_TAB_LIST_RETRIES = 10;
const POPUP_TAB_LIST_RETRY_MS = 20;
@@ -3408,8 +3411,11 @@ class Page {
/**
* Replace the runtime's generic "did not answer" error with one that names
- * this Page and tells the agent what can be done about it. Thrown from here
- * so the stack shows the public call instead of a transport timer.
+ * this Page and what happened next. The wait that just ran out was this
+ * call's whole budget, so a main-frame navigation that did not commit in it
+ * is stopped here, as goto does at its own timeout: the Page then answers
+ * again instead of costing every later call another full wait. Thrown from
+ * here so the stack shows the public call instead of a transport timer.
*/
async #describeUnresponsivePage(
error: PageUnresponsiveError,
@@ -3431,19 +3437,67 @@ class Page {
} catch {
// The URL is a courtesy; the error stands without it.
}
+ const recovered = error.sessionId
+ ? await this.#stopPendingNavigation(error.sessionId)
+ : "unknown";
const described = new PageUnresponsiveError(
error.method,
error.waitedMs,
page.targetId,
error.sessionId,
);
- described.message =
- `Page ${this.label}${url ? ` (${url})` : ""} did not answer ${error.method} within ${error.waitedMs}ms. ` +
- "Chromium suspends every renderer command while a main-frame navigation has started but not committed, and a hung renderer behaves the same way. " +
- 'Retry once the pending navigation commits, call page.goto(url, { timeout }) with a longer timeout, stop the pending navigation with page.cdp("Page.stopLoading"), or continue on a new Page from task.newPage().';
+ const head = `Page ${this.label}${url ? ` (${url})` : ""} did not answer ${error.method} within ${error.waitedMs}ms`;
+ if (recovered === "hung") {
+ described.message = `${head}, and still did not answer after Page.stopLoading, so its renderer is probably hung. Continue on a new Page from task.newPage(), or close this Page and open it again.`;
+ } else if (recovered === "unknown") {
+ described.message =
+ `${head}. ` +
+ "Chromium suspends every renderer command while a main-frame navigation has started but not committed, and a hung renderer behaves the same way. " +
+ 'Retry once the pending navigation commits, call page.goto(url, { timeout }) with a longer timeout, stop the pending navigation with page.cdp("Page.stopLoading"), or continue on a new Page from task.newPage().';
+ } else {
+ described.stopped = true;
+ described.currentUrl = recovered.url;
+ described.message =
+ `${head}: a main-frame navigation had started but not committed. ` +
+ `It was stopped once that wait ran out, so the Page answers again${recovered.url ? ` and shows ${recovered.url}` : ""}. ` +
+ "Retry the call to continue on that document; to load the pending one, call page.goto(url, { timeout }) with a longer timeout, or continue on a new Page from task.newPage().";
+ }
return described;
}
+ /**
+ * Stop whatever main-frame navigation keeps this Page from answering and
+ * read its main frame once it does. "hung" means the Page still answers
+ * nothing: a hung renderer, which Page.stopLoading cannot help.
+ */
+ async #stopPendingNavigation(
+ sessionId: string,
+ ): Promise<{ url?: string } | "hung"> {
+ try {
+ await this.#services.cdp(
+ "Page.stopLoading",
+ {},
+ sessionId,
+ PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS,
+ );
+ const response = await this.#services.cdp(
+ "Page.getFrameTree",
+ {},
+ sessionId,
+ PAGE_UNRESPONSIVE_STOP_TIMEOUT_MS,
+ );
+ const frame =
+ response?.result?.frameTree?.frame || response?.frameTree?.frame;
+ return {
+ ...(typeof frame?.url === "string" && frame.url.length > 0
+ ? { url: frame.url }
+ : {}),
+ };
+ } catch {
+ return "hung";
+ }
+ }
+
async #refMapForAction(
page: PageTarget,
sessionId: string,