fix: use SO_EXCLUSIVEADDRUSE on Windows for callback server - #18137
fix: use SO_EXCLUSIVEADDRUSE on Windows for callback server#18137viacheslav-rostovtsev wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a custom _ExclusiveWSGIServer class to enforce exclusive address binding on Windows using SO_EXCLUSIVEADDRUSE. The review feedback suggests defining allow_reuse_address = False directly on the custom subclass instead of modifying the global WSGIServer.allow_reuse_address attribute, which avoids global side effects and prevents potential WSAEINVAL errors on Windows.
| class _ExclusiveWSGIServer(wsgiref.simple_server.WSGIServer): | ||
| """Custom WSGIServer. | ||
|
|
||
| Enforces exclusive address binding on Windows. | ||
| Setting `WSGIServer.allow_reuse_address` is not enough, since it sets `SO_REUSEADDR` | ||
| and not `SO_EXCLUSIVEADDRUSE`. `SO_REUSEADDR` alone allows other processes to bind | ||
| to the same address and port on Windows. | ||
| """ | ||
|
|
||
| def server_bind(self): | ||
| if sys.platform == "win32" and hasattr(socket, "SO_EXCLUSIVEADDRUSE"): | ||
| self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) | ||
| super().server_bind() |
There was a problem hiding this comment.
To avoid modifying the global WSGIServer.allow_reuse_address attribute and to ensure that SO_EXCLUSIVEADDRUSE works correctly on Windows, we should define allow_reuse_address = False directly on _ExclusiveWSGIServer.
On Windows, if SO_REUSEADDR is set (which happens if allow_reuse_address is True), attempting to set SO_EXCLUSIVEADDRUSE will fail with WSAEINVAL (OSError: [WinError 10022]). Explicitly setting allow_reuse_address = False on this subclass guarantees that SO_REUSEADDR is not set, preventing this error.
| class _ExclusiveWSGIServer(wsgiref.simple_server.WSGIServer): | |
| """Custom WSGIServer. | |
| Enforces exclusive address binding on Windows. | |
| Setting `WSGIServer.allow_reuse_address` is not enough, since it sets `SO_REUSEADDR` | |
| and not `SO_EXCLUSIVEADDRUSE`. `SO_REUSEADDR` alone allows other processes to bind | |
| to the same address and port on Windows. | |
| """ | |
| def server_bind(self): | |
| if sys.platform == "win32" and hasattr(socket, "SO_EXCLUSIVEADDRUSE"): | |
| self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) | |
| super().server_bind() | |
| class _ExclusiveWSGIServer(wsgiref.simple_server.WSGIServer): | |
| """Custom WSGIServer. | |
| Enforces exclusive address binding on Windows. | |
| Setting WSGIServer.allow_reuse_address is not enough, since it sets SO_REUSEADDR | |
| and not SO_EXCLUSIVEADDRUSE. SO_REUSEADDR alone allows other processes to bind | |
| to the same address and port on Windows. | |
| """ | |
| allow_reuse_address = False | |
| def server_bind(self): | |
| if sys.platform == "win32" and hasattr(socket, "SO_EXCLUSIVEADDRUSE"): | |
| self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) | |
| super().server_bind() |
| # Fail fast if the address/port is occupied | ||
| wsgiref.simple_server.WSGIServer.allow_reuse_address = False | ||
| # Use _ExclusiveWSGIServer so that other apps cannot bind to the same address/port on Windows. |
There was a problem hiding this comment.
Modifying the global class attribute wsgiref.simple_server.WSGIServer.allow_reuse_address is risky because it mutates the behavior of the standard library's WSGIServer globally for the entire Python process. This can lead to unexpected side effects in other parts of the application or other libraries that rely on the default behavior of WSGIServer.
Since we are now using a custom subclass _ExclusiveWSGIServer, we can define allow_reuse_address = False directly as a class attribute on _ExclusiveWSGIServer. This achieves the same "fail fast" behavior locally without global side effects.
| # Fail fast if the address/port is occupied | |
| wsgiref.simple_server.WSGIServer.allow_reuse_address = False | |
| # Use _ExclusiveWSGIServer so that other apps cannot bind to the same address/port on Windows. | |
| # Use _ExclusiveWSGIServer to fail fast if the address/port is occupied, and to prevent other apps from binding to it on Windows. |
No description provided.