INNER CODE UNIT · Python
preflight_check_port
hi-godot/godot-ai · src/godot_ai/__init__.py:67
def preflight_check_port(
port: int, *, label: str, setting: str, host: str = "127.0.0.1"
) -> socket.socket | None:
"""Exit with a distinctive stderr message + exit code when `port` is taken.
Returns the bound, listening socket when the plugin asked this launch to
wait for the port (see below): the caller hands it to the real server so
the port is never free between the wait ending and the server listening.
Returns ``None`` on the ordinary fail-fast path.
#647: when a foreign process (e.g. a docker container) already owns the
HTTP or WebSocket port, uvicorn/websockets fail with an opaque bind error
(or, for the WS port, a warning that leaves the server half-alive). Probe
the bind up front and fail fast with a message humans can act on and an
exit code (EXIT_PORT_IN_USE) the Godot plugin can recognize.
Ports that fail to bind for other reasons (EACCES, Windows winnat
exclusion ranges) keep their existing downstream failure modes.