Skip to content

Closing pages with nested frames leaves stale frame ownership references #3240

Open

Description

@vishalanton16

Description

A reused browser context retains references to closed pages through its child-frame ownership map. Repeatedly opening and closing pages with nested frames accumulates stale entries.

Reproduction

Using a browser context reused across iterations:

const html = `
  <iframe srcdoc="<iframe srcdoc='nested'></iframe>"></iframe>
`;

for (let index = 0; index < 100; index++) {
  const temporaryPage = await stagehand.browser.context.newPage();

  await temporaryPage.goto(
    "data:text/html," + encodeURIComponent(html),
  );

  await temporaryPage.frameLocator("iframe")
    .frameLocator("iframe")
    .locator("body")
    .innerText();

  await temporaryPage.close();
}

Inspect the extension's internal BrowserContext.frameOwnerPage map after page-close cleanup, or take a heap snapshot and inspect retained closed Page instances.

For a deterministic internal reproduction, register a page with a main frame, child frame, and grandchild frame; populate their ownership entries; then call cleanupByTarget for that page. Repeat 100 times.

Actual behavior

The internal cleanup fixture retained 200 stale child-frame ownership entries after closing 100 pages with two descendant frames each.

Removing a nested frame subtree can also leave descendant ownership and cached-frame entries behind.

Expected behavior

Closing a page should remove ownership entries for every frame belonging to it. Removing a frame subtree should clear all removed descendants and associated caches.

Pages still open in the context should retain their entries. Frame process swaps should preserve ownership needed by the replacement session.

Context shutdown should dispose page listeners and finish clearing bookkeeping even if an individual cleanup operation fails.

Activity

  1. oliarmak-web commented on Oct 19, 2026

    @oliarmak-web

    @vishalanton16, thanks for the deterministic breakdown. If you are not already preparing the fix, I would like to take this. I would first reproduce it with the internal fixture, then make cleanup remove descendant frame ownership and associated caches while preserving live pages and process swaps. Tests would cover page closure, nested subtree removal, repeated open and close cycles, and cleanup after a partial failure. Does that scope fit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions