{"record":{"id":"ba3d6ada6810bb42","repo":"xtekky/gpt4free","slug":"failed-to-create-new-tab-target-on-port-self-port","errorCode":null,"errorMessage":"Failed to create new tab target on port {self.port}","messagePattern":"Failed to create new tab target on port (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/requests/cdp.py","lineNumber":349,"sourceCode":"\n        # Create a new tab target\n        ws_url = None\n        for _ in range(10):\n            try:\n                req = urllib.request.Request(\n                    f\"http://{self.host}:{self.port}/json/new\", method=\"PUT\"\n                )\n                with urllib.request.urlopen(req, timeout=2) as response:\n                    target = json.loads(response.read().decode(\"utf-8\"))\n                    ws_url = target.get(\"webSocketDebuggerUrl\")\n                    self.target_id = target.get(\"id\")\n                    if ws_url:\n                        break\n            except Exception:\n                await asyncio.sleep(0.5)\n\n        if not ws_url:\n            raise RuntimeError(f\"Failed to create new tab target on port {self.port}\")\n\n        await self.connect(ws_url)\n\n    async def connect(self, ws_url: str):\n        \"\"\"Connect to the target WebSocket debugger.\"\"\"\n        self.session = aiohttp.ClientSession()\n        self.ws = await self.session.ws_connect(ws_url)\n        self._closing = False\n\n        # Start receiver loop\n        self._receive_task = asyncio.create_task(self._receiver_loop())\n\n        # Enable essential domains\n        await self.call(\"Page.enable\")\n        await self.call(\"DOM.enable\")\n        await self.call(\"Runtime.enable\")\n        await self.call(\"Network.enable\")\n        await self.call(\"Emulation.setFocusEmulationEnabled\", enabled=True)","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/requests/cdp.py#L331-L367","documentation":"Raised by the async CDPSession in g4f/requests/cdp.py after it repeatedly (with 0.5s backoff) tried PUT http://host:port/json/new to open a new tab target and never got a response containing a webSocketDebuggerUrl. Without a target the session cannot open its WebSocket, so it fails fast instead of connecting blind.","triggerScenarios":"The browser process died between port discovery and tab creation; the CDP endpoint is up but /json/new is blocked or returns an error (some hardened Chrome builds restrict /json endpoints via --remote-allow-origins or remote debugging flags); wrong host/port in CDPSession(port=..., host=...); too many open tabs exhausting targets.","commonSituations":"Pointing CDPSession at a port where a non-Chrome service listens; Chrome 111+ requiring --remote-allow-origins for some CDP HTTP endpoints; racing with another tool (or a prior crashed session) that closed the browser.","solutions":["Confirm the browser is alive on that port: curl http://HOST:PORT/json/version.","Verify host/port passed to CDPSession match the actually running Chrome instance (or omit them to let g4f auto-discover).","Restart the browser/Chrome process and retry session creation.","When launching your own Chrome for CDP, include --remote-debugging-port and (Chrome 111+) --remote-allow-origins=*."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import urllib.request, json\n\ndef cdp_alive(host: str, port: int) -> bool:\n    try:\n        with urllib.request.urlopen(f\"http://{host}:{port}/json/version\", timeout=2) as r:\n            return r.status == 200\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    session = CDPSession(port=port, host=host)\n    await session.start()\nexcept RuntimeError as e:\n    if \"Failed to create new tab target\" in str(e):\n        # browser dead or wrong port: rediscover or restart, then retry once\n        restart_shared_browser()\n        session = CDPSession(); await session.start()\n    else:\n        raise","preventionTips":["Health-check /json/version before constructing a session against a manually managed port.","Prefer auto-discovery (omit port) so g4f finds or launches a live browser.","Launch Chrome with --remote-debugging-port and --remote-allow-origins=* on Chrome 111+."],"tags":["cdp","tab-target","websocket","connection","chrome"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}