fix: close_tab() recovers session state when closing the currently attached tab#393
Open
mvanhorn wants to merge 1 commit into
Open
fix: close_tab() recovers session state when closing the currently attached tab#393mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
…tached tab Implements the work described in 2026-05-26-115-fix-close-tab-dangling-session-state-plan.md. Closes browser-use#379.
Contributor
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/browser_harness/helpers.py">
<violation number="1" location="src/browser_harness/helpers.py:352">
P2: close_tab() leaks a fallback about:blank tab when Target.closeTarget fails in single-tab mode</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
| if other_tabs: | ||
| switch_tab(other_tabs[0]) | ||
| else: | ||
| new_tab() |
Contributor
There was a problem hiding this comment.
P2: close_tab() leaks a fallback about:blank tab when Target.closeTarget fails in single-tab mode
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/browser_harness/helpers.py, line 352:
<comment>close_tab() leaks a fallback about:blank tab when Target.closeTarget fails in single-tab mode</comment>
<file context>
@@ -326,11 +326,35 @@ def new_tab(url="about:blank"):
+ if other_tabs:
+ switch_tab(other_tabs[0])
+ else:
+ new_tab()
+ try:
+ cdp("Target.closeTarget", targetId=target_id)
</file context>
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.
Summary
close_tab()now switches the harness to a surviving real tab (or a freshabout:blank) before closing the currently attached tab, so the daemon session never points at a dead target.Why this matters
Before this change, the no-arg form of
close_tab()issuedTarget.closeTargetagainst the attachedtargetIdwithout updating the daemon session thatswitch_tab()previously registered. Afterclose_tab()the daemon still believed the now-deadtargetId/sessionIdwas active, so the next call intojs(),page_info(),current_tab(), or_mark_tab()landed on a dead session and failed until the caller manuallyswitch_tab()'d.Repro:
Maintainer @sauravpanda filed #379 with a clear cause + Option (a) fix sketch.
Changes
close_tab(target=None)now detects whether the target equals the currently attached tab. If so, it picks a surviving real tab vialist_tabs(include_chrome=False)andswitch_tab()s to it before closing; if no other real tab exists, it opens a freshabout:blankfirst. The fast path for explicit non-current targets is unchanged.Target.closeTargetfails after the switch, the original session is restored rather than left half-migrated.Testing
pytest tests/unit/test_helpers.py— 22/22 pass. Added 4 new test cases:page_info()succeeds.Fixes #379
Summary by cubic
Fixes a bug where closing the currently attached tab left the session pointing at a dead target. Now close_tab() switches to a live tab (or creates about:blank) before closing so follow-up calls work. Fixes #379.
Written for commit d0f0807. Summary will update on new commits. Review in cubic