Bug: StorageStateWatchdog logs ERROR every auto-save interval once the CDP client is gone, and reset() leaves its loop running
Browser Use Version
0.13.10 and main at d8110c5 (same on 0.13.1)
Bug Description, Steps to Reproduce, Screenshots
Once a session loses its CDP client, StorageStateWatchdog's auto-save loop logs [StorageStateWatchdog] Error in monitoring loop: CDP client not initialized - browser may not be connected yet at ERROR every auto_save_interval (60 s as BrowserSession sets it up), and never stops on its own.
Two things combine:
- The "no client" guards in the watchdog are
if not self.browser_session.cdp_client:(storage_state_watchdog.py#L137, also #L226, #L349, #L362), butBrowserSession.cdp_clientis a property that asserts the client exists (session.py#L1339-L1342). So with no client the guard raisesAssertionErrorinstead of returning, and_monitor_storage_changescatches it and logs ERROR (#L119-L133). Called directly,get_current_cookies()andadd_cookies()raise the same way instead of returning[]/ warning (events are fine:watchdog_basealready skips handlers whileis_cdp_connectedis False). BrowserSession.reset()clears_cdp_client_rootand sets_storage_state_watchdog = Nonewithout stopping the watchdog's monitoring task (session.py#L662, #L676). The task is only cancelled fromon_BrowserStopEvent, so when a reset happens without that event reaching the watchdog (a crash, a stop that did not go throughBrowserStopEvent), the loop keeps running on the reset session, and keeps it referenced.
The repro below needs no browser: it starts monitoring with a stand-in client, then drops the client the way reset() does.
Failing Python Code
import asyncio
import logging
from importlib.metadata import version
from browser_use.browser.session import BrowserSession
from browser_use.browser.watchdogs.storage_state_watchdog import StorageStateWatchdog
class Capture(logging.Handler):
def __init__(self) -> None:
super().__init__(level=logging.DEBUG)
self.records: list[logging.LogRecord] = []
def emit(self, record: logging.LogRecord) -> None:
self.records.append(record)
async def main() -> None:
print("browser-use", version("browser-use"))
session = BrowserSession(headless=True)
StorageStateWatchdog.model_rebuild()
wd = StorageStateWatchdog(event_bus=session.event_bus, browser_session=session, auto_save_interval=0.05)
try:
changed = await wd._have_cookies_changed()
print("guard returned", changed)
except AssertionError as e:
print("guard raised AssertionError:", e)
cap = Capture()
logging.getLogger("browser_use").addHandler(cap)
session._cdp_client_root = object() # stand-in for a connected client
await wd._start_monitoring()
session._cdp_client_root = None # what reset() does
await asyncio.sleep(0.6)
errors = [r for r in cap.records if r.levelno >= logging.ERROR and "monitoring loop" in r.getMessage()]
print(f"monitoring loop ERROR lines in 0.6 s at 0.05 s interval: {len(errors)}")
if errors:
print("first:", errors[0].getMessage())
await wd._stop_monitoring()
asyncio.run(main())LLM Model
None (no agent or LLM involved)
Operating System & Browser Versions
macOS, Python 3.12; the repro launches no browser
Full DEBUG Log Output
browser-use 0.13.10
guard raised AssertionError: CDP client not initialized - browser may not be connected yet
monitoring loop ERROR lines in 0.6 s at 0.05 s interval: 11
first: [StorageStateWatchdog] Error in monitoring loop: CDP client not initialized - browser may not be connected yetFix: use the non-asserting browser_session.is_cdp_connected in the four guards, as AboutBlankWatchdog and watchdog_base already do, and stop the watchdog's monitoring in reset() before it is dropped. PR follows.
Source: browser-use/browser-use