[Python-bugs-list] [ python-Bugs-211710 ] socket.send() can do partial writes on some systems.

noreply@sourceforge.net noreply@sourceforge.net
Thu, 18 Oct 2001 23:04:50 -0700


Bugs item #211710, was opened at 2000-08-11 13:25
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=211710&group_id=5470

Category: Python Library
Group: Platform-specific
Status: Closed
Resolution: None
Priority: 5
Submitted By: scott cotton (scottc)
Assigned to: Jeremy Hylton (jhylton)
Summary: socket.send() can do partial writes on some systems.

Initial Comment:
0811 15:57 chronis:~% uname -a
FreeBSD chronis.pobox.com 4.0-STABLE FreeBSD 4.0-STABLE #0: Tue Mar 21 01:05:14 EST 2000     root@chronis.pobox.com:/usr/src/sys/compile/MYBSD  i386
0811 15:57 chronis:~% 


0811 15:57 chronis:~% cat scratch/sendtstsrv.py     
import socket         
                      
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind("chronis.pobox.com", 8010) 

while 1:
    s.listen(1)
    conn, addr = s.accept()
    print "connected from", addr
    while 1:
        data = conn.recv(1024)
        if not data: break
    print "done"
    conn.close()

0811 15:58 chronis:~% python scratch/sendtstsrv.py &
[2] 76562

0811 15:58 chronis:~% cat scratch/sendtst.py 
#!/usr/bin/env python

s = "0" * 10000000



import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect("chronis.pobox.com", 8010)
sent = sock.send(s)
if sent != len(s):
    print "sent %d/%d chars" % (sent, len(s))
else:
    print "sent all chars"

0811 15:58 chronis:~% python !$
python scratch/sendtst.py
connected from ('208.210.124.49', 1258)
sent 17520/10000000 chars
done
0811 15:58 chronis:~%            


NOTE: there is nothing in any man page for send() under linux (RH 6.1), Solaris (SunOS 5.5.1), OSF1 V4.0, or FreeBSD{3,4} that states that send() must not perform a
partial write, though of those systems, it only seems
to reproducibly do partial writes under FreeBSD4.0 Stable.

Additionally, W.Richard Stevens' Unix Network Programming Vol 1, second edition states that """
A read or write on a stream socket might input or output fewer bytes than requested, but this is not an error condition.""" (page 77).  Later, Stevens says of send() and recv() "These two functions are similar to the standard read and write functions, but one additional argument is required". (page 354).

scott

----------------------------------------------------------------------

Comment By: Richard Jones (richard)
Date: 2001-10-18 23:04

Message:
Logged In: YES 
user_id=6405

We have patched httplib.HTTPConnection.send() so it does 
the following:

   sent = 0
   while sent < len(str):
       sent = sent + self.sock.send(str[sent:])

Could this be the default behaviour in the socket module 
perhaps?



----------------------------------------------------------------------

Comment By: Richard Jones (richard)
Date: 2001-10-18 22:57

Message:
Logged In: YES 
user_id=6405

Why is this bug closed? The standard library uses send() 
in a number of places - we've just run up against it in 
httplib. Again, on FreeBSD, we're seeing that send() is 
only sending a part of the data we want it to.


----------------------------------------------------------------------

Comment By: Jeremy Hylton (jhylton)
Date: 2000-09-15 11:50

Message:
scott-- Is there a bug here?  It seems clear that the send system call is not required to send all the bytes you asked it to send.  It returns the number of bytes it did send.  If you want to make sure all your data is sent, you can wrap send in a while loop that calls send until all the data is consumed.


----------------------------------------------------------------------

Comment By: Jeremy Hylton (jhylton)
Date: 2000-09-07 15:01

Message:
Please do triage on this bug.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=211710&group_id=5470