Skip to content

Enhance browser_agent resilience: Automatic session takeover and lock recovery #22232

Description

@hsm207

What would you like to be added?

The browser_agent (specifically BrowserManager.ts) currently employs a restrictive "fail-fast" strategy when encountering a locked browser profile (e.g., when sessionMode: 'persistent' is used and an existing instance or orphaned process is active):

{
  "success": false,
  "summary": "The task could not be completed because the browser is already running for /home/user/.gemini/cli-browser-profile.",
  "fullErrorMessage": "The browser is already running for /home/user/.gemini/cli-browser-profile. Use --isolated to run multiple browser instances.\nCause: The browser is already running for /home/user/.gemini/cli-browser-profile. Use a different userDataDir or stop the running browser first."
}

I propose to make it more resilient with the following capabilities:

  1. Automatic Session Takeover: If persistent mode fails due to a profile lock, the agent should automatically attempt to connect to the existing instance (as if sessionMode: 'existing' were specified). This requires a graceful "fallback-and-probe" mechanism in BrowserManager.connectMcp().
  2. Orphaned Process Recovery: If an existing connection is not feasible but the profile remains locked, the BrowserManager should proactively detect orphaned chrome or chrome-devtools-mcp processes associated with the Gemini CLI profile and offer (or automatically execute) a "Self-Healing" routine (process termination + lock file removal).
  3. Intelligent Error Remediation: Replace the current static error messages with an interactive recovery flow. Utilizing the ask_user tool, the agent should present the user with a "Choice" menu featuring numbered options to resolve the lock (e.g., 1. Attempt Session Takeover, 2. Force Termination & Reset, or 3. Launch Isolated Session).

Why is this needed?

Forcing the user to manually pkill processes or close browser windows is a friction point that degrades the autonomous agent experience. The agent has sufficient context (profile path, PIDs) to resolve this internally without user intervention.

Additional context

Activity

  1. added
    status/need-triage
    area/agent
    on Mar 12, 2026
  2. added
    priority/p2
    and removed
    status/need-triage
    on Mar 12, 2026
  3. added
    🔒 maintainer only
    on Mar 12, 2026
  4. added
    workstream-rollup
    on Mar 16, 2026
  5. m13v commented on Apr 26, 2026

    @m13v

    i went down this exact rabbit hole building a local agent that opens persistent Chrome profiles for autofill extraction. the auto-takeover path sounds clean until you hit the case where the lock file is stale but the SingletonSocket is still bound to a real PID running under a different user shell. spent two weeks bouncing between psutil checks and CDP probes before realizing the only reliable signal is trying to attach via remote-debugging-port and timing out fast, like 800ms. lock file inspection lies. process name matching lies harder, especially with chrome-devtools-mcp wrappers in the tree. the orphan recovery step also needs to nuke SingletonCookie, SingletonLock, and SingletonSocket together or chrome silently refuses to start the next launch.

  6. added
    kind/customer-issue
    priority/p3
    on May 8, 2026
  7. gemini-cli commented on May 8, 2026

    @gemini-cli
    Contributor

    A feature request to enhance the resilience of the browser agent.

  8. added
    priority/p1
    on May 8, 2026
  9. gemini-cli commented on May 8, 2026

    @gemini-cli
    Contributor

    Categorized as area/agent and kind/feature. Priority set to priority/p1.

  10. removed
    priority/p1
    priority/p2
    on May 14, 2026
  11. IRONICBo commented on Oct 1, 2026

    @IRONICBo

    Current main makes these modes materially different: persistent launches ~/.gemini/cli-browser-profile, while existing uses --autoConnect and warns that saved logins become visible. A profile lock alone should not silently cross that privacy boundary or justify killing Chrome. Persist an ownership manifest beside the dedicated profile (pid, process start time, nonce, expected debug endpoint), then allow takeover or cleanup only when all fields and a browser-side marker match; otherwise keep the explicit choice. I maintain socai; attaching to authenticated Chrome is explicit, serialized, and exposes read-only social operations rather than raw CDP.

  12. wasabihu commented on Nov 27, 2026

    @wasabihu

    Strongly agree with @IRONICBo on the trust boundary: a lock on ~/.gemini/cli-browser-profile should never silently fall back to existing (--autoConnect), since that quietly exposes every saved login in the user's primary browser.

    Two OS-level / architectural lessons from hitting these exact profile-lock and orphan-browser issues while building sessfork (an open-source local session forker + Rust MCP server):

    1. Prevent orphans at the kernel level so Singleton* recovery becomes rare:

      • Windows: bind the spawned browser/MCP subprocess tree to a Win32 Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE. Even if the parent CLI crashes or is force-killed, the OS tears down the entire browser tree immediately and releases the profile locks with zero orphans.
      • macOS / Linux: put the spawned browser in its own process group (setpgid) and tie a tiny watchdog to an anonymous pipe from the parent CLI; unlike PR_SET_PDEATHSIG, pipe EOF fires deterministically on both macOS and Linux when the parent dies abruptly, so the whole process group exits cleanly before leaving stale SingletonLock / SingletonSocket files behind.
    2. Avoid shared userDataDir locks altogether for authenticated headless workflows:
      When users reach for persistent mainly to keep logged-in state across runs (or parallel agents), sharing one mutable Chrome profile directory inevitably hits SingletonLock contention. Injecting domain-scoped storageState (cookies + localStorage for explicitly allowlisted origins only) into a fresh isolated headless context gives authenticated access with zero lock contention and without exposing the rest of the user's browser profile—pairing nicely with @IRONICBo's ownership-manifest check for true persistent sessions.

  13. jrin0340-420 commented on Nov 27, 2026

    @jrin0340-420

    Is this my particular repository and what can we do to help fix it

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/agentkind/customer-issuekind/featurepriority/p3status/bot-triagedworkstream-rollup🔒 maintainer only

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions