-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[v2] fix: handle page close events and avoid errors on closed targets #1497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Conversation
- Add handling for page close events to update active page when current page closes - Add error handling for CDP operations on closed targets - Remove unnecessary inline comments 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 1 file
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="lib/StagehandContext.ts">
<violation number="1" location="lib/StagehandContext.ts:184">
P2: This close handler will be registered multiple times for the same page. The `handleNewPlaywrightPage` method is called twice per new page (directly and in the `finally` block of `init`), and `attachFrameNavigatedListener` also registers a close handler with similar logic. Consider adding a guard to prevent duplicate handler registration, or consolidating the close handling logic into one place.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Greptile SummaryConsolidated page close handling to prevent duplicate handlers and added automatic active page switching when tabs close. When the active page closes, the code now switches to another open tab. Added error guards for CDP operations to gracefully handle "Target closed" errors.
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Context as StagehandContext
participant PW as PlaywrightPage
participant CDP as CDPSession
participant Handler as Close Handler
Note over Context,PW: New Page Creation Flow
PW->>Context: page event fires
Context->>Context: handleNewPlaywrightPage(pwPage)
Context->>PW: isClosed() check
alt Page not in pageMap
Context->>Handler: Register close handler (once)
Handler-->>Context: Handler registered
end
Context->>Context: createStagehandPage()
Context->>Context: setActivePage()
Context->>Context: attachFrameNavigatedListener()
Note over Context,CDP: Attach CDP Listener Flow
Context->>PW: isClosed() check
Context->>CDP: newCDPSession(pwPage)
alt Target closed error
CDP-->>Context: Error (Target closed)
Context->>Context: Catch and return silently
else Success
CDP-->>Context: Session created
Context->>CDP: session.send("Page.enable")
CDP->>CDP: Listen for Page.frameNavigated
end
Note over Handler,Context: Page Close Flow
PW->>Handler: close event fires
Handler->>Context: Get shPage from pageMap
Handler->>Context: unregisterFrameId(shPage.frameId)
alt Closed page is active
Handler->>Context: Iterate through pages()
Handler->>Context: Find first non-closed page
Handler->>Context: setActivePage(foundPage)
end
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 file reviewed, 1 comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 file reviewed, 1 comment
| for (const p of this.intContext.pages()) { | ||
| const sp = this.pageMap.get(p); | ||
| if (sp && sp !== shPage) { | ||
| this.setActivePage(sp); | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: check that candidate page p is not closed before setting as active
| for (const p of this.intContext.pages()) { | |
| const sp = this.pageMap.get(p); | |
| if (sp && sp !== shPage) { | |
| this.setActivePage(sp); | |
| break; | |
| } | |
| } | |
| for (const p of this.intContext.pages()) { | |
| if (!p.isClosed()) { | |
| const sp = this.pageMap.get(p); | |
| if (sp && sp !== shPage) { | |
| this.setActivePage(sp); | |
| break; | |
| } | |
| } | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/StagehandContext.ts
Line: 191:197
Comment:
**style:** check that candidate page `p` is not closed before setting as active
```suggestion
for (const p of this.intContext.pages()) {
if (!p.isClosed()) {
const sp = this.pageMap.get(p);
if (sp && sp !== shPage) {
this.setActivePage(sp);
break;
}
}
}
```
How can I resolve this? If you propose a fix, please make it concise.
Summary
Test plan
🤖 Generated with Claude Code
Summary by cubic
Automatically switch the active page when the current tab closes and add targeted error handling to prevent crashes when CDP targets are closed. Improves multi-tab stability.
Written for commit 4eb04e3. Summary will update on new commits.