fix(serve): Windows conflict probe uses SO_EXCLUSIVEADDRUSE (SO_REUSEADDR binds over live listeners on WinSock)
This commit is contained in:
@@ -19301,10 +19301,22 @@ def _port_bind_conflict(host: str, port: int) -> bool:
|
||||
except OSError:
|
||||
return False
|
||||
try:
|
||||
# Match uvicorn's bind flags (uvicorn/config.py bind_socket) so the
|
||||
# probe conflicts exactly when uvicorn's own bind would: SO_REUSEADDR
|
||||
# lets TIME_WAIT remnants pass while a live LISTEN socket still fails.
|
||||
probe.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
|
||||
import sys as _sys_mod
|
||||
|
||||
_exclusive = getattr(_socket, "SO_EXCLUSIVEADDRUSE", None)
|
||||
if _sys_mod.platform == "win32" and _exclusive is not None:
|
||||
# Windows: SO_REUSEADDR means "bind over anyone" — a probe (or
|
||||
# uvicorn bind) with it SUCCEEDS on top of a live LISTEN socket,
|
||||
# so it can never detect a conflict. SO_EXCLUSIVEADDRUSE makes
|
||||
# the probe fail with WSAEADDRINUSE exactly when another socket
|
||||
# holds the port (the reporter's 10048 shape in #93608).
|
||||
probe.setsockopt(_socket.SOL_SOCKET, _exclusive, 1)
|
||||
else:
|
||||
# POSIX: match uvicorn's bind flags (uvicorn/config.py
|
||||
# bind_socket) so the probe conflicts exactly when uvicorn's own
|
||||
# bind would: SO_REUSEADDR lets TIME_WAIT remnants pass while a
|
||||
# live LISTEN socket still fails.
|
||||
probe.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
|
||||
probe.bind((host, port))
|
||||
except OSError as exc:
|
||||
return _is_addr_in_use_error(exc)
|
||||
|
||||
Reference in New Issue
Block a user