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

Karen Shaeffer klsshaeffer at icloud.com
Wed Feb 17 03:25:46 EST 2021



> 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>


Humbly,
Karen



More information about the Python-list mailing list