What's the meaning the "backlog" in the socket.listen(backlog) is?

Karen Shaeffer klsshaeffer at icloud.com
Wed Feb 17 04:01:30 EST 2021



> On Feb 17, 2021, at 12:25 AM, Karen Shaeffer via Python-list <python-list at python.org> wrote:
> 
> 
> 
>> On Feb 16, 2021, at 8:10 PM, Jason Friedman <jsf80238 at gmail.com> wrote:
>> 
>>> 
>>> I set listen(2) and expect to see "error" when more clients than "the
>>> maximum number of queued connections" trying to connect the server. But, no
>>> error!! Even 4 clients can run normally without problem.
>>> 
>>> Am I misunderstanding the meaning of this argument?
>>> 
>> 
>> https://docs.python.org/3/library/socket.html#socket.socket.listen:
>> Enable a server to accept connections. If backlog is specified ... it
>> specifies the number of unaccepted connections that the system will allow
>> before refusing new connections.
>> 
>> So I read that to say that the listen method will accept as many connection
>> as it can handle, and will put into a backlog those it cannot. Your
>> argument of 2 tells the method to allow up to 2 not-yet-accepted connection
>> requests.
>> -- 
> 
> Hi Jason,
> The backlog parameter specifies the number of connection requests that the kernel network stack has waiting for the application layer to process. So, it’s at a lower level than the listen system call, which is the application layer API into the kernel network stack that simply establishes the socket. Listen doesn’t actually touch any data. Once the accept system call has touched it, then the network stack no longer counts it in the backlog. Backlog is a network stack queue associated with the application socket. The backlog is managed by the network driver software. Moving bits down there is extremely efficient. And the driver wouldn’t process and pass packets up the stack, if the backlog was maxed out. That would be very inefficient — rather the driver would drop the packets at the network driver ring buffer in such case. That’s at the API into the network stack looking up from the network driver.
> 
> Here is a good description from the perspective of the bottom of the tcp ip stack API into the network card driver, discussing the processing of packets depending on state of the backlog for the socket.
> 
> http://veithen.io/2014/01/01/how-tcp-backlog-works-in-linux.html <http://veithen.io/2014/01/01/how-tcp-backlog-works-in-linux.html>
> 
The tcp ip stack software is the archetypical example of copy-on-write code. The packet comes over the wire and is copied onto the network driver ring buffer. Then, if the socket backlog is less than full, the network driver software copies the packet from the network driver ring buffer onto the very bottom layer of the ip stack. The data is never moved (copied) again, as it flows up the layers of the tcp ip stack, only pointers are used by each layer of the stack to maintain access to the data. Finally, the application will copy the data from the kernel memory over to application user space memory with a socket read call. It’s very beautiful code.

Karen.



More information about the Python-list mailing list