[Python-Dev] noreply@sourceforge.net: [Python-bugs-list] [Bug #111620] lots of use of send() without verifyi ng amount of d (original) (raw)

Guido van Rossum guido@beopen.com
Fri, 11 Aug 2000 17:05:32 -0500


[Gordon]

[I wrote, about send()] > Yes, it returns the number of bytes sent. For TCP/IP it is not > an error to send less than the argument. It's only an error if > the other end dies at the time of actual send.

[and...] > Just open a TCP/IP connection and send huge (64K or so) > buffers. Current Python behavior is no different than C on > Linux, HPUX and Windows. And I just demonstrated it. Strangely enough, sending from Windows (where the dos say "send returns the total number of bytes sent, which can be less than the number indicated by len") it always sent the whole buffer, even when that was 1M on a non- blocking socket. (I select()'ed the socket first, to make sure it could send something). But from Linux, the largest buffer sent was 54,020 and typical was 27,740. No errors.

OK. So send() can do a partial write, but only on a stream connection. And most standard library code doesn't check for that condition, nor does (probably) much other code that used the standard library as an example. Worse, it seems that on some platforms send() never does a partial write (I couldn't reproduce it on Red Hat 6.1 Linux), so even stress testing may not reveal the lurking problem.

Possible solutions:

  1. Do nothing. Pro: least work. Con: subtle bugs remain.

  2. Fix all code that's broken in the standard library, and try to encourage others to fix their code. Book authors need to be encouraged to add a warning. Pro: most thorough. Con: hard to fix every occurrence, especially in 3rd party code.

  3. Fix the socket module to raise an exception when less than the number of bytes sent occurs. Pro: subtle bug exposed when it happens. Con: breaks code that did the right thing!

  4. Fix the socket module to loop back on a partial send to send the remaining bytes. Pro: no more short writes. Con: what if the first few send() calls succeed and then an error is returned? Note: code that checks for partial writes will be redundant!

I'm personally in favor of (4), despite the problem with errors after the first call.

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)