Repository navigation
Conversation
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.
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.