SeleniumHQ/selenium · error · ConnectionError

Selenium server is already running, or something else is usi

Error message

Selenium server is already running, or something else is using port {self.port}

What it means

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.

Source

Thrown at py/selenium/webdriver/remote/server.py:210

            java_path,
            "-jar",
            path,
            "standalone",
            "--port",
            str(self.port),
            "--log-level",
            self.log_level,
            *self.args,
        ]
        if self.host is not None:
            command.extend(["--host", self.host])

        host = self.host if self.host is not None else "localhost"

        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                sock.connect((host, self.port))
            raise ConnectionError(f"Selenium server is already running, or something else is using port {self.port}")
        except ConnectionRefusedError:
            print("Starting Selenium server...")
            self.process = subprocess.Popen(command, env=self.env)
            print(f"Selenium server running as process: {self.process.pid}")
            if not self._wait_for_server(timeout=self.startup_timeout):
                raise TimeoutError(f"Timed out waiting for Selenium server at {self.status_url}")
            print("Selenium server is ready")
        return self.process

    def stop(self):
        """Stop the server."""
        if self.process is None:
            raise RuntimeError("Selenium server isn't running")
        else:
            if self.process.poll() is None:
                self.process.terminate()
                self.process.wait()
            self.process = None

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Pick a free port: pass port=0 or select via a socket bind, or choose a unique port per worker.
  2. Ensure the previous server was stopped: call server.stop() (and join the process) before reusing.
  3. Free the port: find and kill the stray listener (e.g. lsof -ti:4444 | xargs kill).

Example fix

# before
server = Server(port=4444)
server.start()  # another instance already on 4444

# after
import socket
with socket.socket() as s:
    s.bind(('localhost', 0)); port = s.getsockname()[1]
server = Server(port=port)
server.start()
Defensive patterns

Strategy: validation

Validate before calling

import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    in_use = s.connect_ex((host or 'localhost', port)) == 0
if in_use:
    raise RuntimeError(f'port {port} already in use; choose another')

Try / catch

try:
    server.start()
except ConnectionError:
    # port occupied; free it or pick a new port and retry once

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/6f89a308f1012c70. Report an issue: GitHub.