Skip to content

gh-162256: Do not set SO_REUSEADDR when binding port 0 - #162271

Open
tydavid5 wants to merge 2 commits into
python:mainfrom
tydavid5:fix/socketserver-no-reuseaddr-port0-162256
Open

tydavid5 wants to merge 2 commits into
python:mainfrom
tydavid5:fix/socketserver-no-reuseaddr-port0-162256

Conversation

@tydavid5

Copy link
Copy Markdown

Fixes gh-162256

SO_REUSEADDR is only useful when rebinding a fixed port that may still have connections in TIME_WAIT. For port 0 the kernel only hands out ports that are not in use, so setting it just lets another process claim the port between bind() and listen(), and then our listen() fails with EADDRINUSE. TCPServer.server_bind() now skips the option for AF_INET and AF_INET6 when the requested port is 0, matching the family check already used for SO_REUSEPORT. A fixed port still gets the option, and servers that set allow_reuse_address to False are unchanged. The fix also covers HTTPServer and SimpleXMLRPCServer, which both set allow_reuse_address and are commonly started on port 0.

Verification: the issue reproducer raised EADDRINUSE from server_activate() before the fix, and after the fix the rival bind fails instead and the server comes up normally. The new regression test passes with the fix and fails with it reverted. test_httpservers and test_xmlrpc pass; test_socketserver has the same 3 pre-existing UDP errors on the pristine tree (the sandbox blocks UDP datagrams), unrelated to this change.

Note: this change was written with the help of Muse, Meta's AI assistant powered by Muse Spark 1.3, used as a coding assistant, and the result was fully reviewed and tested.

SO_REUSEADDR is only useful when rebinding a fixed port that may still have connections in TIME_WAIT. For port 0 the kernel only hands out ports that are not in use, so setting it just lets another process claim the port between bind() and listen(), and then our listen() fails with EADDRINUSE. TCPServer.server_bind() now skips the option for AF_INET and AF_INET6 when the requested port is 0, matching the family check already used for SO_REUSEPORT. A fixed port still gets the option, and servers that set allow_reuse_address to False are unchanged. The fix also covers HTTPServer and SimpleXMLRPCServer, which both set allow_reuse_address and are commonly started on port 0.

Fixes pythongh-162256. Adds a regression test covering the port 0 case for AF_INET and AF_INET6 plus the fixed port control, and a news entry.
@python-cla-bot

python-cla-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

…he test

On macOS getsockopt returns the raw flag value 4 for a set SO_REUSEADDR,
not 1, so the fixed-port control failed on macOS with 4 != 1. Check for
non-zero instead, matching the convention in test_socket.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTPServer on port 0 can fail with EADDRINUSE because socketserver sets SO_REUSEADDR

1 participant