[v2]: handle target closed errors on popups - #1710
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 6143b9c The changes in this PR will be included in the next version bump. 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 |
Contributor
Greptile SummaryThis PR addresses race conditions with fast-closing popups by adding graceful error handling for CDP session attachment failures. Key Changes:
The implementation correctly handles the race condition where popups close before Stagehand can attach CDP sessions, treating these as expected outcomes rather than failures. Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Popup Opens] --> B{Page Already Closed?}
B -->|Yes| C[Skip Initialization]
B -->|No| D[Create StagehandPage]
D --> E{Page Closed During Init?}
E -->|Yes - StagehandTargetClosedError| F[Catch and Continue]
E -->|Yes - isTargetGoneError| F
E -->|Yes - page.isClosed| F
E -->|No| G[Page Initialized]
G --> H[Attach CDP Session]
H --> I{Target Still Available?}
I -->|No| J[Return Early]
I -->|Yes| K[Create CDP Session]
K --> L{CDP Session Created?}
L -->|Error - Target Gone| M[Catch and Return]
L -->|Error - Other| N[Re-throw Error]
L -->|Success| O[Session Attached]
F --> P[Continue Without Page]
M --> P
P --> Q[Normal Operation]
O --> Q
Last reviewed commit: 6143b9c |
Contributor
There was a problem hiding this comment.
No issues found across 5 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant B as Browser (Playwright)
participant C as StagehandContext
participant P as StagehandPage
participant U as Utils
Note over B,U: Initialization / New Popup Detected
C->>B: Detect new page/popup
C->>B: NEW: page.isClosed()
alt Page is already gone
C-->>C: Skip initialization
else Page active
C->>P: createStagehandPage(pwPage)
P->>U: NEW: isTargetGone(pwPage)
U-->>P: false
P->>C: getCDPSession()
C->>B: Playwright: newCDPSession()
alt CHANGED: Race Condition (Popup closes during attach)
B-->>C: Error ("Target closed" / "No target with id")
C-->>P: Error
P->>U: NEW: isTargetGoneError(err)
U-->>P: true
P-->>C: Throw StagehandTargetClosedError
Note over C: NEW: Catch/Swallow during Init
C->>C: Log & Continue (Treat as benign)
else Success Path
B-->>C: CDPSession
C-->>P: CDPSession
P->>P: Cache CDPSession
P-->>C: StagehandPage instance
end
end
Note over B,U: Subsequent Page Operations
P->>P: getCDPSession()
P->>U: NEW: Check cache & isTargetGone()
opt NEW: Target gone but session was cached
P->>P: Remove from cdpClients cache
P-->>C: Throw StagehandTargetClosedError
end
C->>B: CHANGED: attachFrameNavigatedListener(pwPage)
alt NEW: Page closes before listener attach
C->>B: pwPage.newCDPSession()
B-->>C: Error (Target gone)
C->>U: isTargetGoneError(err)
U-->>C: true
C-->>C: Gracefully exit listener setup
end
miguelg719
approved these changes
Feb 19, 2026
Merged
seanmcguire12
pushed a commit
that referenced
this pull request
Feb 19, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to v2, this PR will be updated. # Releases ## @browserbasehq/stagehand@2.5.8 ### Patch Changes - [#1710](#1710) [`df76207`](df76207) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - handle target closed errors on rapidly opening/closing popups ## @browserbasehq/stagehand-evals@1.1.8 ### Patch Changes - Updated dependencies \[[`df76207`](df76207)]: - @browserbasehq/stagehand@2.5.8 ## @browserbasehq/stagehand-examples@1.0.17 ### Patch Changes - Updated dependencies \[[`df76207`](df76207)]: - @browserbasehq/stagehand@2.5.8 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Publish Stagehand 2.5.8 with a fix for “target closed” errors when popups are opened and closed quickly, improving stability. Updated evals (1.1.8) and examples (1.0.17) to depend on this patch. <sup>Written for commit fb39851. Summary will update on new commits. <a href="https://cubic.dev/pr/browserbase/stagehand/pull/1714">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. --> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
why
Target.attachToTarget/newCDPSessionerrorswhat changed
lib/utils.tsSummary by cubic
Handle rapidly opening/closing popups by detecting “target gone” scenarios and preventing CDP session attach errors. This reduces flakiness during auto-init and keeps fast-closing popups from surfacing as failures.
Written for commit 6143b9c. Summary will update on new commits. Review in cubic