{"record":{"id":"6f89a308f1012c70","repo":"SeleniumHQ/selenium","slug":"selenium-server-is-already-running-or-something-e","errorCode":null,"errorMessage":"Selenium server is already running, or something else is using port {self.port}","messagePattern":"Selenium server is already running, or something else is using port (.+?)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"py/selenium/webdriver/remote/server.py","lineNumber":210,"sourceCode":"            java_path,\n            \"-jar\",\n            path,\n            \"standalone\",\n            \"--port\",\n            str(self.port),\n            \"--log-level\",\n            self.log_level,\n            *self.args,\n        ]\n        if self.host is not None:\n            command.extend([\"--host\", self.host])\n\n        host = self.host if self.host is not None else \"localhost\"\n\n        try:\n            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n                sock.connect((host, self.port))\n            raise ConnectionError(f\"Selenium server is already running, or something else is using port {self.port}\")\n        except ConnectionRefusedError:\n            print(\"Starting Selenium server...\")\n            self.process = subprocess.Popen(command, env=self.env)\n            print(f\"Selenium server running as process: {self.process.pid}\")\n            if not self._wait_for_server(timeout=self.startup_timeout):\n                raise TimeoutError(f\"Timed out waiting for Selenium server at {self.status_url}\")\n            print(\"Selenium server is ready\")\n        return self.process\n\n    def stop(self):\n        \"\"\"Stop the server.\"\"\"\n        if self.process is None:\n            raise RuntimeError(\"Selenium server isn't running\")\n        else:\n            if self.process.poll() is None:\n                self.process.terminate()\n                self.process.wait()\n            self.process = None","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/py/selenium/webdriver/remote/server.py#L192-L228","documentation":"Raised in `start()` after a probe successfully opens a TCP connection to (host, port) — meaning something is ALREADY listening there. Because the runner assumes it owns that port for the new server, a live listener is treated as a conflict. It is a ConnectionError.","triggerScenarios":"Calling start() twice on the same Server instance, or starting a second instance pointed at a port already held by a previous Selenium Grid/standalone, a browser driver, or any unrelated process. Also when a prior server crashed without releasing the port and the OS hasn't recycled it.","commonSituations":"Forgetting to call stop() in a previous test run; a leftover background java process from a killed pytest session; a fixed port colliding across parallel test workers; reusing port 4444 that a Docker grid already occupies.","solutions":["Pick a free port: pass port=0 or select via a socket bind, or choose a unique port per worker.","Ensure the previous server was stopped: call server.stop() (and join the process) before reusing.","Free the port: find and kill the stray listener (e.g. lsof -ti:4444 | xargs kill)."],"exampleFix":"# before\nserver = Server(port=4444)\nserver.start()  # another instance already on 4444\n\n# after\nimport socket\nwith socket.socket() as s:\n    s.bind(('localhost', 0)); port = s.getsockname()[1]\nserver = Server(port=port)\nserver.start()","handlingStrategy":"validation","validationCode":"import socket\nwith socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n    in_use = s.connect_ex((host or 'localhost', port)) == 0\nif in_use:\n    raise RuntimeError(f'port {port} already in use; choose another')","typeGuard":null,"tryCatchPattern":"try:\n    server.start()\nexcept ConnectionError:\n    # port occupied; free it or pick a new port and retry once","preventionTips":["Always pair start() with stop() in a try/finally.","Use ephemeral ports in tests to avoid cross-worker collisions."],"tags":["network","configuration","port-conflict","server-runner"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}