Skip to content

Commit 18fa493

Browse files
committed
gh-109532: fix socket HOWTO inaccuracy about send() on broken connection
Correct the claim that send() returns 0 bytes on a broken connection. In practice, send() on a broken connection raises OSError (EPIPE) rather than returning 0. Only recv() returns 0 bytes to indicate disconnection. Add a clarifying comment to the mysend example noting this distinction.
1 parent 072cd7c commit 18fa493

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Doc/howto/sockets.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ request, then reads a reply. That's it. The socket is discarded. This means tha
167167
a client can detect the end of the reply by receiving 0 bytes.
168168

169169
But if you plan to reuse your socket for further transfers, you need to realize
170-
that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket
171-
``send`` or ``recv`` returns after handling 0 bytes, the connection has been
172-
broken. If the connection has *not* been broken, you may wait on a ``recv``
170+
that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a
171+
``recv`` returns 0 bytes, the connection has been broken. A ``send`` on a
172+
broken connection will raise an :exc:`OSError` instead. If the connection has
173+
*not* been broken, you may wait on a ``recv``
173174
forever, because the socket will *not* tell you that there's nothing more to
174175
read (for now). Now if you think about that a bit, you'll come to realize a
175176
fundamental truth of sockets: *messages must either be fixed length* (yuck), *or
@@ -201,6 +202,8 @@ length message::
201202
sent = self.sock.send(msg[totalsent:])
202203
if sent == 0:
203204
raise RuntimeError("socket connection broken")
205+
# Note: in practice, send() on a broken connection
206+
# raises OSError rather than returning 0.
204207
totalsent = totalsent + sent
205208

206209
def myreceive(self):

0 commit comments

Comments
 (0)