Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1be815a

Browse files
committed
Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
order to accept exactly one connection. Patch by Daniel Evers.
1 parent cda41d3 commit 1be815a

5 files changed

Lines changed: 20 additions & 6 deletions

File tree

Doc/library/socket.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ correspond to Unix system calls applicable to sockets.
652652
.. method:: socket.listen(backlog)
653653

654654
Listen for connections made to the socket. The *backlog* argument specifies the
655-
maximum number of queued connections and should be at least 1; the maximum value
656-
is system-dependent (usually 5).
655+
maximum number of queued connections and should be at least 0; the maximum value
656+
is system-dependent (usually 5), the minimum value is forced to 0.
657657

658658

659659
.. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \

Lib/test/test_socket.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,13 @@ def test_name_closed_socketio(self):
788788
fp.close()
789789
self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>")
790790

791+
def testListenBacklog0(self):
792+
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
793+
srv.bind((HOST, 0))
794+
# backlog = 0
795+
srv.listen(0)
796+
srv.close()
797+
791798

792799
@unittest.skipUnless(thread, 'Threading required for this test.')
793800
class BasicTCPTest(SocketConnectedTest):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ Carey Evans
268268
Tim Everett
269269
Paul Everitt
270270
David Everly
271+
Daniel Evers
271272
Greg Ewing
272273
Martijn Faassen
273274
Clovis Fabricio

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ Core and Builtins
8686
Library
8787
-------
8888

89+
- Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
90+
order to accept exactly one connection. Patch by Daniel Evers.
91+
8992
- Issue #11164: Stop trying to use _xmlplus in the xml module.
9093

9194
- Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch

Modules/socketmodule.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,10 @@ sock_listen(PySocketSockObject *s, PyObject *arg)
22202220
if (backlog == -1 && PyErr_Occurred())
22212221
return NULL;
22222222
Py_BEGIN_ALLOW_THREADS
2223-
if (backlog < 1)
2224-
backlog = 1;
2223+
/* To avoid problems on systems that don't allow a negative backlog
2224+
* (which doesn't make sense anyway) we force a minimum value of 0. */
2225+
if (backlog < 0)
2226+
backlog = 0;
22252227
res = listen(s->sock_fd, backlog);
22262228
Py_END_ALLOW_THREADS
22272229
if (res < 0)
@@ -2234,8 +2236,9 @@ PyDoc_STRVAR(listen_doc,
22342236
"listen(backlog)\n\
22352237
\n\
22362238
Enable a server to accept connections. The backlog argument must be at\n\
2237-
least 1; it specifies the number of unaccepted connection that the system\n\
2238-
will allow before refusing new connections.");
2239+
least 0 (if it is lower, it is set to 0); it specifies the number of\n\
2240+
unaccepted connections that the system will allow before refusing new\n\
2241+
connections.");
22392242

22402243

22412244
/*

0 commit comments

Comments
 (0)