Skip to content

Conversation

@seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Dec 4, 2025

why

  • this PR fixes the the intermittent “main world not ready for frame” error, which stemmed from us re‑attaching to targets that Chrome had already auto‑attached. Every extra Target.attachToTarget produces a new flattened session ID, so we ended up with two independent root CDP sessions for the same page.
  • when a cross-origin iframe (OOPIF) later tried to resolve its execution context, some code paths looked it up via the duplicate session, (the one that never saw Runtime.executionContextCreated)
  • this causedexecutionContexts.waitForMainWorld to time out even though the context already existed

what changed

  • inside the bootstrap() function (inside packages/core/lib/v3/understudy/context.ts), after grabbing existing targets via Target.getTargets, we now avoid directly calling Target.attachToTarget for any entry whose attached flag is already true
  • this means we keep exactly one session per target, which means that frame lookup uses the same session that actually observed its Runtime events

test plan

  • existing tests were catching this (most often shadow-iframe.spec.ts)
  • this test should no longer be flakey

Summary by cubic

Skip attaching to targets that are already attached to avoid duplicate attach calls when auto-attach has already run. This prevents redundant work and suppresses re-attach errors.

Written for commit 688f44f. Summary will update automatically on new commits.

@changeset-bot
Copy link

changeset-bot bot commented Dec 4, 2025

🦋 Changeset detected

Latest commit: 688f44f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@seanmcguire12 seanmcguire12 marked this pull request as ready for review December 4, 2025 01:45
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 4, 2025

Greptile Overview

Greptile Summary

Fixed intermittent "main world not ready for frame" errors by preventing duplicate CDP session creation. Added check in bootstrap() to skip Target.attachToTarget for targets already attached via auto-attach, ensuring each target has exactly one session that receives all Runtime events.

Confidence Score: 5/5

  • This PR is safe to merge - it's a minimal, well-targeted fix
  • The change is a simple one-line guard that prevents a race condition without introducing new logic or side effects
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/core/lib/v3/understudy/context.ts 5/5 Added guard to skip re-attaching already-attached targets, preventing duplicate CDP sessions
.changeset/fruity-clowns-pick.md 5/5 Changeset properly documents the fix as a patch release

Sequence Diagram

sequenceDiagram
    participant Chrome
    participant bootstrap
    participant conn as CdpConnection
    participant session as CDP Session
    
    Note over bootstrap: bootstrap() called
    bootstrap->>conn: enableAutoAttach()
    
    Note over Chrome,conn: Auto-attach occurs for some targets
    Chrome->>conn: Target.attachedToTarget (auto)
    conn->>session: Create session
    session->>session: Runtime.executionContextCreated
    
    bootstrap->>conn: getTargets()
    conn-->>bootstrap: List of targets (some with attached=true)
    
    loop For each target
        alt Target NOT already attached
            bootstrap->>conn: attachToTarget(targetId)
            conn->>session: Create new session
            Note over session: This session never sees<br/>Runtime events!
        else Target already attached (NEW)
            bootstrap->>bootstrap: Skip (continue)
            Note over bootstrap: Prevents duplicate session
        end
    end
    
    Note over session: OOPIF tries to resolve context
    session->>session: waitForMainWorld(frameId)
    alt With duplicate session (OLD)
        session-->>session: Timeout: "main world not ready"
    else Single session (NEW)
        session-->>session: Success: context found
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants