{"record":{"id":"d4df3787c57c6b2e","repo":"xtekky/gpt4free","slug":"failed-to-start-shared-chrome-on-port-port","errorCode":null,"errorMessage":"Failed to start shared Chrome on port {port}","messagePattern":"Failed to start shared Chrome on port (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/requests/cdp.py","lineNumber":289,"sourceCode":"        for _ in range(40):\n            time.sleep(0.5)\n            try:\n                with urllib.request.urlopen(\n                    f\"http://{host}:{port}/json\", timeout=1\n                ) as response:\n                    if response.status == 200:\n                        _shared_browser_port = port\n                        return _shared_browser_port\n            except Exception:\n                pass\n\n        if _shared_browser_process:\n            try:\n                _shared_browser_process.terminate()\n            except Exception:\n                pass\n            _shared_browser_process = None\n        raise RuntimeError(f\"Failed to start shared Chrome on port {port}\")\n\n\nclass CDPSession:\n    def __init__(\n        self,\n        port: Optional[int] = None,\n        host: Optional[str] = None,\n        user_data_dir: Optional[str] = None,\n        headless: bool = True,\n    ):\n        if port is None:\n            port = BrowserConfig.port\n        if host is None:\n            host = BrowserConfig.host\n        if host is None:\n            host = \"127.0.0.1\"\n        self.port = port\n        self.host = host","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/cdp.py#L271-L307","documentation":"Raised in g4f/requests/cdp.py when the shared Chromium process was spawned on a dynamically chosen free port but never answered HTTP on that port within the startup window (the /json version probe loop failed). The code then terminates the child process and gives up with this RuntimeError, meaning Chrome launched but its CDP HTTP endpoint did not become reachable.","triggerScenarios":"Chrome starts extremely slowly (cold profile, heavy disk I/O) and misses the readiness deadline; Chrome crashes immediately on startup (bad flags, sandbox denial in containers, missing --no-sandbox in root containers); the chosen port got taken between bind-check and Chrome launch; zombie chrome processes exhausting resources.","commonSituations":"Running as root in Docker without --no-sandbox; SELinux/AppArmor blocking the child; slow network filesystems for the user_data_dir; leftover zombie Chrome processes from previous crashed runs.","solutions":["Kill leftover chrome/chromium processes from previous runs, then retry.","If running in a container as root, ensure the browser is launched with --no-sandbox (adjust browser args used by g4f's launcher).","Retry the operation — the port race and slow-start cases are transient.","Check that the user_data_dir location is writable and on fast local disk.","Manually verify the browser can start: run the chrome binary with --remote-debugging-port=9222 and curl http://127.0.0.1:9222/json/version."],"exampleFix":"# before: chrome fails to answer CDP in container as root\n# after: ensure sandbox is disabled for containerized chrome\nCHROMIUM_FLAGS=\"--no-sandbox --disable-gpu\" python your_g4f_script.py","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(2):\n    try:\n        session = CDPSession(); await session.start()\n        break\n    except RuntimeError as e:\n        if \"Failed to start shared Chrome\" in str(e) and attempt == 0:\n            await kill_stale_chrome()  # cleanup helper\n            continue\n        raise","preventionTips":["Reap zombie chrome processes on startup and after crashes.","Run containerized Chrome with --no-sandbox and adequate CPU/memory.","Keep the CDP user_data_dir on local writable disk."],"tags":["cdp","chrome-startup","retryable","docker","sandbox"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}