diff --git a/llmhost/cli.py b/llmhost/cli.py index 50861e4..b10b307 100644 --- a/llmhost/cli.py +++ b/llmhost/cli.py @@ -227,6 +227,11 @@ def port_free(port): # 0.0.0.0:port bind while it does. for addr in ("0.0.0.0", "127.0.0.1"): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if sys.platform.startswith("linux"): + # Servers bind with SO_REUSEADDR, so connections lingering in TIME_WAIT (e.g. from + # a server that just stopped) don't block them; without it the port looks busy. + # On Linux this still fails if anything is listening. (On macOS it would not.) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: s.bind((addr, port)) except OSError: