Skip to content

fix: use SO_EXCLUSIVEADDRUSE on Windows for callback server - #18137

Draft
viacheslav-rostovtsev wants to merge 1 commit into
googleapis:mainfrom
viacheslav-rostovtsev:dev/virost/fix/callback-port-fix
Draft

fix: use SO_EXCLUSIVEADDRUSE on Windows for callback server#18137
viacheslav-rostovtsev wants to merge 1 commit into
googleapis:mainfrom
viacheslav-rostovtsev:dev/virost/fix/callback-port-fix

Conversation

@viacheslav-rostovtsev

Copy link
Copy Markdown
Member

No description provided.

@viacheslav-rostovtsev
viacheslav-rostovtsev requested a review from a team as a code owner August 18, 2026 06:56
@viacheslav-rostovtsev
viacheslav-rostovtsev marked this pull request as draft August 18, 2026 06:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +489 to +501
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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()

Comment on lines +438 to +440
# 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant