Skip to content

fix: close_tab() recovers session state when closing the currently attached tab#393

Open
mvanhorn wants to merge 1 commit into
browser-use:mainfrom
mvanhorn:fix/379-close-tab-dangling-session-state
Open

fix: close_tab() recovers session state when closing the currently attached tab#393
mvanhorn wants to merge 1 commit into
browser-use:mainfrom
mvanhorn:fix/379-close-tab-dangling-session-state

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented May 27, 2026

Summary

close_tab() now switches the harness to a surviving real tab (or a fresh about: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() issued Target.closeTarget against the attached targetId without updating the daemon session that switch_tab() previously registered. After close_tab() the daemon still believed the now-dead targetId/sessionId was active, so the next call into js(), page_info(), current_tab(), or _mark_tab() landed on a dead session and failed until the caller manually switch_tab()'d.

Repro:

new_tab("https://example.com")
close_tab()
page_info()  # fails: not attached to live tab

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 via list_tabs(include_chrome=False) and switch_tab()s to it before closing; if no other real tab exists, it opens a fresh about:blank first. The fast path for explicit non-current targets is unchanged.
  • Error path: if Target.closeTarget fails 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:

  • Happy path: two tabs, no-arg close, current tab + daemon session land on the survivor.
  • Single-tab fallback: about:blank created, session points at it, page_info() succeeds.
  • Explicit non-current close: current tab + session unchanged.
  • Error path: simulated close failure restores original session intact.

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.

  • Bug Fixes
    • When closing the current tab, switch to a surviving real tab first; if none, open about:blank.
    • If closing a non-current tab, leave the current session unchanged.
    • If close fails after switching, restore the original session.
    • Added unit tests covering survivor switch, blank fallback, non-current close, and failure rollback.

Written for commit d0f0807. Summary will update on new commits. Review in cubic

…tached tab

Implements the work described in 2026-05-26-115-fix-close-tab-dangling-session-state-plan.md.

Closes browser-use#379.
Copy link
Copy Markdown
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.

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()
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot May 27, 2026

Choose a reason for hiding this comment

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

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>
Fix with Cubic

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.

close_tab() on the current tab leaves dangling session state

1 participant