Hayden Thring
Hayden Thring

Reputation: 1820

Can python socket recieve fill if not read?

If you don't read from a python tcp socket, will it fill and cause an error ? In my code I use .send() and there seem to be an ack reply from the device I'm talking to. If i don't read these out, will they build up and create a problem ? Do it just keep storing them all infinitely ? Surely this would cause memory issue eventually ... thanks.

Upvotes: 0

Views: 289

Answers (1)

mata
mata

Reputation: 69092

If you don't read from a tcp socket then the recv buffer on the receiving end and the send buffer on the seinding end will fill up, at which point your program will block on further send() calls.

How much memory each process will use depends on the size of those buffers, which depends on the operating system and socket options. For example, on linux you would get into a situation like this:

$ ss -tpn
State  Recv-Q Send-Q  Local Address:Port Peer Address:Port   
ESTAB  0      2595384 127.0.0.1:3333     127.0.0.1:2222    users:(("python3",pid=13088,fd=3))
ESTAB  964588 0       127.0.0.1:2222     127.0.0.1:3333    users:(("python3",pid=13087,fd=4))

The first line shows the sending process (full send queue, ~2.6MB), the second line the receiving process (full recv queue, ~1MB).

This happens because during data transfer using TCP, with each ACK the receiver tells the sender how much data it is ready to accept for the next transmission. If the rec buffer is full, the send buffer will also fill up and then no more data can be sent.

Upvotes: 1

Related Questions