Repository navigation
Description
Browser Use Version
0.12.2
Bug Description, Steps to Reproduce, Screenshots
Title
Agent.close() with keep_alive=True drops CDP WebSocket connections, triggering unnecessary reconnection cycle
Body
Context
We maintain a pool of pre-authenticated browsers using Browser(keep_alive=True). At startup, a lightweight agent logs each browser into our target application, then the browser sits idle in the pool until a real task arrives. This avoids repeating login for every task and lets us reuse authenticated sessions across sequential agent runs.
Bug Description
When Agent.close() completes with keep_alive=True, all CDP WebSocket connections are dropped. In 0.12.2, the built-in reconnection logic recovers (after ~1.7s), but this is unnecessary overhead — the connections shouldn't be dropped in the first place when the intent is to keep the browser alive.
Observed Behavior (0.12.2)
Immediately after warmup agent completes on a keep_alive=True browser:
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] WebSocket reconnection attempt 1/3...
INFO [BrowserSession] [SessionManager] Cleared all owned data (targets, sessions, mappings)
WARNING [BrowserSession] WebSocket reconnection attempt 1/3...
INFO [BrowserSession] [SessionManager] Cleared all owned data (targets, sessions, mappings)
...
INFO [BrowserSession] WebSocket reconnected after 1.7s (attempt 1)
INFO [BrowserSession] WebSocket reconnected after 1.7s (attempt 1)
This happens for every Agent.close() on a keep_alive=True browser — multiple WebSocket handlers all disconnect and reconnect, clearing session manager data each time.
Expected Behavior
With keep_alive=True, Agent.close() should preserve the CDP WebSocket connections and session state. The browser should remain fully connected and ready for the next Agent.run() without any disconnection/reconnection cycle.
Impact
- ~1.7s recovery delay after every agent run before the browser is usable again
- Session manager data cleared on each reconnection ("Cleared all owned data (targets, sessions, mappings)")
- In pre-0.12.x (which lacked WebSocket reconnection), this same issue caused permanent CDP connection collapse — the browser became unusable after the first agent completed
- Multiple reconnection warnings per browser pollute logs in pooled setups
Reproduction
from browser_use import Agent, Browser
browser = Browser(keep_alive=True)
# First agent — e.g. a login warmup
agent1 = Agent(task="Navigate to example.com", browser=browser, llm=llm)
await agent1.run()
# Immediately after: CDP WebSocket handlers exit, reconnection cycle begins
# Browser recovers after ~1.7s, but all session data was clearedRoot Cause (from investigation)
In agent/service.py, Agent.close() with keep_alive=True tears down the event bus, which appears to cascade into closing CDP WebSocket connections. The 0.12.2 reconnection logic masks the issue but doesn't prevent the unnecessary disconnect.
Environment
- browser-use: 0.12.2
- Python: 3.13
- OS: Cloud Foundry (Linux) with Browserless container via CDP
- Setup: Pool of 3-5
Browser(keep_alive=True)instances, each warmed up by a login agent, then reused for sequential tasks
Failing Python Code
from browser_use import Agent, Browser
browser = Browser(keep_alive=True)
# First agent — e.g. a login warmup
agent1 = Agent(task="Navigate to example.com", browser=browser, llm=llm)
await agent1.run()
# Immediately after: CDP WebSocket handlers exit, reconnection cycle begins
# Browser recovers after ~1.7s, but all session data was clearedLLM Model
No response
Operating System & Browser Versions
No response
Full DEBUG Log Output
The relevant section is:
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] CDP WebSocket message handler exited unexpectedly (connection closed)
WARNING [BrowserSession] WebSocket reconnection attempt 1/3...
INFO [BrowserSession] [SessionManager] Cleared all owned data (targets, sessions, mappings)
WARNING [BrowserSession] WebSocket reconnection attempt 1/3...
INFO [BrowserSession] [SessionManager] Cleared all owned data (targets, sessions, mappings)
...
INFO [BrowserSession] WebSocket reconnected after 1.7s (attempt 1)
INFO [BrowserSession] WebSocket reconnected after 1.7s (attempt 1)
Activity
MrLeit commentedon Mar 16, 2026
Additional finding:
keep_alive=Truebreaks CDP (remote browser) connectionsWhen using a remote browser via CDP (e.g. Browserless),
Agent.close()withkeep_alive=Truedestroys the connection entirely.The
keep_alive=Truepath inclose()callsevent_bus.stop(clear=False)and then setsevent_queue = None. This causes all CDP WebSocket message handlers to exit with "connection closed" warnings. The reconnection logic recovers after ~1.7s, but clears allSessionManagerdata (targets, sessions, mappings) — destroying the authenticated session state thatkeep_aliveis supposed to preserve.This works fine with local browsers because the browser process keeps running independently of the event bus. But with CDP, the WebSocket is the only link to the browser — once the handlers exit, the connection is effectively dead. The reconnected WebSocket has no handlers feeding events to the new event bus.
Workaround: Skip the
event_bus.stop()/event_queue = Nonecleanup entirely whenkeep_alive=True. Only run the non-event-bus parts ofclose()(skill service cleanup, gc).Expected:
Agent.close()withkeep_alive=Trueshould preserve the event bus and CDP WebSocket handlers so the browser session remains usable for subsequent agents.fix(agent): preserve CDP connections when keep_alive=True
fix: preserve CDP WebSocket connections when keep_alive=True (browser…
fix: preserve CDP WebSocket connections when keep_alive=True (browser…
jda5 commentedon Mar 25, 2026
I'm having this problem too!
Nbk7822 commentedon Mar 27, 2026
CDP connection management for long-running sessions is brutal. We sidestepped this entirely by making the VM the unit of persistence, not the WebSocket. Agent connects to Chrome inside a container, and if the connection drops, the Chrome instance is still running, agent just reconnects. No state loss, no session teardown. The container stays up as long as you need it. We don't have session time caps for this reason. Here's how we structured it: open-computer-use
fix: preserve CDP WebSocket connections on Agent.close() with keep_al…
MrLeit commentedon Mar 31, 2026
The next problem after addressing this is (as @Nbk7822 mentioned) CDP connection stability. I've raised the dependent ticket #4579.
For now I've stopped trying to use persistent session state and keep_alive as it is too buggy. This of course has performance and token wastage implications.
MrLeit commentedon Jun 7, 2026
We moved away from keep alive on browsers and added static login when creating them and before handing back to the agent. This is a small time overhead but is relatively quick and completely deterministic.
IgorGanapolsky commentedon Jul 22, 2026
Reliability note
Production automation failures usually reduce to a contract issue before a model/prompt issue:
If you have one failing execution + expected vs actual, reply with stack/version and I will send a free root-cause checklist.