{"record":{"id":"10d9f304bcdde06a","repo":"NanmiCoder/MediaCrawler","slug":"cannot-connect-to-existing-browser-on-port-self-d","errorCode":null,"errorMessage":"Cannot connect to existing browser on port {self.debug_port} after waiting {timeout}s. Please ensure:\n  1. Your browser is running\n  2. Remote debugging is enabled (chrome://inspect/#remote-debugging)\n  3. The debug port is {self.debug_port} (configure via CDP_DEBUG_PORT)","messagePattern":"Cannot connect to existing browser on port (.+?) after waiting (.+?)s\\. Please ensure:\n  1\\. Your browser is running\n  2\\. Remote debugging is enabled \\(chrome://inspect/#remote-debugging\\)\n  3\\. The debug port is (.+?) \\(configure via CDP_DEBUG_PORT\\)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/cdp_browser.py","lineNumber":179,"sourceCode":"        # The user may need time to enable remote debugging or confirm the connection dialog\n        timeout = config.BROWSER_LAUNCH_TIMEOUT\n        utils.logger.info(\n            f\"[CDPBrowserManager] Waiting up to {timeout}s for browser CDP connection...\"\n        )\n        connected = False\n        for i in range(timeout):\n            if await self._test_cdp_connection(self.debug_port):\n                connected = True\n                break\n            if i % 5 == 0 and i > 0:\n                utils.logger.info(\n                    f\"[CDPBrowserManager] Still waiting for browser... ({i}s elapsed) \"\n                    \"Please enable remote debugging: chrome://inspect/#remote-debugging\"\n                )\n            await asyncio.sleep(1)\n\n        if not connected:\n            raise RuntimeError(\n                f\"Cannot connect to existing browser on port {self.debug_port} \"\n                f\"after waiting {timeout}s. Please ensure:\\n\"\n                \"  1. Your browser is running\\n\"\n                \"  2. Remote debugging is enabled (chrome://inspect/#remote-debugging)\\n\"\n                f\"  3. The debug port is {self.debug_port} (configure via CDP_DEBUG_PORT)\"\n            )\n\n        # Connect via CDP (reuse existing method)\n        await self._connect_via_cdp(playwright)\n\n        # Create browser context (reuse existing method, will prefer existing context)\n        browser_context = await self._create_browser_context(playwright_proxy, user_agent)\n        self.browser_context = browser_context\n\n        utils.logger.info(\"[CDPBrowserManager] Successfully connected to existing browser\")\n        return browser_context\n\n    async def _get_browser_path(self) -> str:","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/NanmiCoder/MediaCrawler/blob/d6f7c5bb906b6dac40ddf343ef9e26438a3de092/tools/cdp_browser.py#L161-L197","documentation":"Raised by CDPBrowserManager when connecting to an existing (already running) browser: it polls _test_cdp_connection(debug_port) once per second for `timeout` seconds and, never succeeding, reports it could not reach Chrome's DevTools endpoint. The message walks through the three prerequisites: browser running, --remote-debugging-port enabled, and the port matching CDP_DEBUG_PORT.","triggerScenarios":"HEADLESS/OFF mode expecting a user-launched browser: Chrome was started normally (no --remote-debugging-port flag), or it listens on a different port than config.CDP_DEBUG_PORT, or the browser exits/crashes during the wait window. The CDP HTTP endpoint http://localhost:<port>/json/version never answers.","commonSituations":"First-time setup where the user forgets to relaunch Chrome with the debugging flag; CDP_DEBUG_PORT default (9222) clashing with another Chrome instance; SELinux/firewall blocking loopback DevTools; Chrome 111+ refusing remote debugging on the default profile without a dedicated --user-data-dir.","solutions":["Relaunch your browser with the flag: chrome --remote-debugging-port=9222 (match CDP_DEBUG_PORT) and a separate --user-data-dir if needed","Verify the endpoint answers: curl http://127.0.0.1:9222/json/version — if it fails, the browser is not exposing CDP","Align the port: set CDP_DEBUG_PORT to the port your browser actually uses, or vice versa","Kill other Chrome instances holding 9222 with a stale/non-debug profile","If you don't need an existing browser, use the managed-launch path (which starts its own browser) instead"],"exampleFix":"# before\nchrome  # normal launch, CDP not exposed -> timeout after Ns\n\n# after\ngoogle-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cdp-profile &\ncurl -s http://127.0.0.1:9222/json/version  # sanity check before starting the crawler","handlingStrategy":"validation","validationCode":"import aiohttp\n\nasync def cdp_alive(port: int) -> bool:\n    try:\n        async with aiohttp.ClientSession() as s:\n            async with s.get(f\"http://127.0.0.1:{port}/json/version\", timeout=aiohttp.ClientTimeout(total=2)) as r:\n                return r.status == 200\n    except Exception:\n        return False\n\n# before starting the crawler in 'existing browser' mode:\nif not await cdp_alive(config.CDP_DEBUG_PORT):\n    raise SystemExit(f\"start chrome with --remote-debugging-port={config.CDP_DEBUG_PORT} first\")","typeGuard":null,"tryCatchPattern":"try:\n    ctx = await mgr.init(color, headless=False)\nexcept RuntimeError as e:\n    if \"Cannot connect to existing browser\" in str(e):\n        # actionable recovery: launch the browser ourselves with the flag\n        subprocess.Popen([browser, f\"--remote-debugging-port={mgr.debug_port}\", \"--user-data-dir=/tmp/cdp\"])\n        ctx = await mgr.init(color, headless=False)  # retry after launch\n    else:\n        raise","preventionTips":["Pre-flight check http://127.0.0.1:<port>/json/version before connecting","Launch Chrome with --remote-debugging-port and a dedicated --user-data-dir","Keep CDP_DEBUG_PORT consistent between browser launch and crawler config"],"tags":["browser","cdp","chrome","debugging","timeout"],"backgroundTag":null,"analyzedSha":"d6f7c5bb906b6dac40ddf343ef9e26438a3de092","analyzedAt":"2026-08-15T01:39:07.505Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}