Skip to content

bpo-36003: change socketserver.TCPServer reuse_addr and backlog default options #11875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Doc/library/socketserver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,22 @@ Server Objects
.. attribute:: allow_reuse_address

Whether the server will allow the reuse of an address. This defaults to
:const:`False`, and can be set in subclasses to change the policy.
:const:`True` on POSIX, and can be set in subclasses to change the policy.

.. versionchanged:: 3.8
default changed from :const:`False` to :const:`True` on POSIX

.. attribute:: request_queue_size

The size of the request queue. If it takes a long time to process a single
request, any requests that arrive while the server is busy are placed into a
queue, up to :attr:`request_queue_size` requests. Once the queue is full,
further requests from clients will get a "Connection denied" error. The default
value is usually 5, but this can be overridden by subclasses.
value is 0, meaning a default reasonable value is chosen, but this can be
overridden by subclasses.

.. versionchanged:: 3.8
default changed from 5 to 0

.. attribute:: socket_type

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@ Changes in the Python API
:exc:`dbm.gnu.error` or :exc:`dbm.ndbm.error`) instead of :exc:`KeyError`.
(Contributed by Xiang Zhang in :issue:`33106`.)

* :class:`socketserver.TCPServer` ``allow_reuse_address`` default changed from
``False`` to ``True`` on POSIX.
:attr:`socketserver.TCPServer` ``request_queue_size`` default changed from
``5`` to ``None`` in order to choose a default reasonable value (usually 128).
:class:`http.server.HTTPServer` and :class:`xmlrpc.server.SimpleXMLRPCServer`
also inherited this change.
(Contributed by Giampaolo Rodola in :issue:`36003`)

CPython bytecode changes
------------------------
Expand Down
2 changes: 0 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@

class HTTPServer(socketserver.TCPServer):

allow_reuse_address = 1 # Seems to make sense in testing environment

def server_bind(self):
"""Override server_bind to store the server name."""
socketserver.TCPServer.server_bind(self)
Expand Down
2 changes: 0 additions & 2 deletions Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,6 @@ class ConfigSocketReceiver(ThreadingTCPServer):
A simple TCP socket-based logging config receiver.
"""

allow_reuse_address = 1

def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
handler=None, ready=None, verify=None):
ThreadingTCPServer.__init__(self, (host, port), handler)
Expand Down
5 changes: 3 additions & 2 deletions Lib/socketserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,10 @@ class TCPServer(BaseServer):

socket_type = socket.SOCK_STREAM

request_queue_size = 5
request_queue_size = 0

allow_reuse_address = False
allow_reuse_address = \
os.name not in ('nt', 'cygwin') and hasattr(socket, 'SO_REUSEADDR')
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the OS check here? Is SO_REUSEADDR available but not appropriate for use on Windows? If so, I suggest adding a comment.

Also, why use os.name rather than sys.platform?


def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
"""Constructor. May be extended, do not override."""
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,6 @@ class TestTCPServer(ControlMixin, ThreadingTCPServer):
the server will set up the socket and listen on it.
"""

allow_reuse_address = True

def __init__(self, addr, handler, poll_interval=0.5,
bind_and_activate=True):
class DelegatingTCPRequestHandler(StreamRequestHandler):
Expand Down
2 changes: 0 additions & 2 deletions Lib/xmlrpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,6 @@ class SimpleXMLRPCServer(socketserver.TCPServer,
from SimpleXMLRPCDispatcher to change this behavior.
"""

allow_reuse_address = True

# Warning: this is for debugging purposes only! Never set this to True in
# production code, as will be sending out sensitive information (exception
# and stack trace details) when exceptions are raised inside
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`socketserver.TCPServer.allow_reuse_address` default changed from ``False``
to ``True`` on POSIX. `socketserver.TCPServer.request_queue_size` default
changed from ``5`` to ``None`` in order to choose a default reasonable value
(usually 128). `http.server.HTTPServer` and
`xmlrpc.server.SimpleXMLRPCServer` also inherited this change. (Contributed
by Giampaolo Rodola in :issue:`36003`)