From 1be815aac49bc0dc3937eb3a618fc9f1a6deb51a Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 10 May 2011 19:16:29 +0200 Subject: [PATCH] Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in order to accept exactly one connection. Patch by Daniel Evers. --- Doc/library/socket.rst | 4 ++-- Lib/test/test_socket.py | 7 +++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ Modules/socketmodule.c | 11 +++++++---- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index bfb8ae94146..14cd4ff5ce6 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -652,8 +652,8 @@ correspond to Unix system calls applicable to sockets. .. method:: socket.listen(backlog) Listen for connections made to the socket. The *backlog* argument specifies the - maximum number of queued connections and should be at least 1; the maximum value - is system-dependent (usually 5). + maximum number of queued connections and should be at least 0; the maximum value + is system-dependent (usually 5), the minimum value is forced to 0. .. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \ diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index a948541825a..4100c34452c 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -788,6 +788,13 @@ class GeneralModuleTests(unittest.TestCase): fp.close() self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") + def testListenBacklog0(self): + srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + srv.bind((HOST, 0)) + # backlog = 0 + srv.listen(0) + srv.close() + @unittest.skipUnless(thread, 'Threading required for this test.') class BasicTCPTest(SocketConnectedTest): diff --git a/Misc/ACKS b/Misc/ACKS index 5c33b2e0a2b..399de8cba9a 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -268,6 +268,7 @@ Carey Evans Tim Everett Paul Everitt David Everly +Daniel Evers Greg Ewing Martijn Faassen Clovis Fabricio diff --git a/Misc/NEWS b/Misc/NEWS index 4309858ec5d..ac9ac79da25 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -86,6 +86,9 @@ Core and Builtins Library ------- +- Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in + order to accept exactly one connection. Patch by Daniel Evers. + - Issue #11164: Stop trying to use _xmlplus in the xml module. - Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index e2fdb5e12eb..bc11b23c567 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -2220,8 +2220,10 @@ sock_listen(PySocketSockObject *s, PyObject *arg) if (backlog == -1 && PyErr_Occurred()) return NULL; Py_BEGIN_ALLOW_THREADS - if (backlog < 1) - backlog = 1; + /* To avoid problems on systems that don't allow a negative backlog + * (which doesn't make sense anyway) we force a minimum value of 0. */ + if (backlog < 0) + backlog = 0; res = listen(s->sock_fd, backlog); Py_END_ALLOW_THREADS if (res < 0) @@ -2234,8 +2236,9 @@ PyDoc_STRVAR(listen_doc, "listen(backlog)\n\ \n\ Enable a server to accept connections. The backlog argument must be at\n\ -least 1; it specifies the number of unaccepted connection that the system\n\ -will allow before refusing new connections."); +least 0 (if it is lower, it is set to 0); it specifies the number of\n\ +unaccepted connections that the system will allow before refusing new\n\ +connections."); /*