Skip to content

socket.sendall() throws Broken Pipe on second write attempt #9

Description

@butlern

If the remote side of your connection goes away, this means that the first write to the socket after the connection goes away is LOST.

To replicate, run netcat -lk 4242

Then in a python shell (instantiate your socket and connect to it. Before the first sendall, Ctrl-C your netcat server):

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.settimeout(2)
>>> s.connect(('localhost', 4242))
>>> s.sendall('\n'.encode('utf8'))
>>> s.sendall('\n'.encode('utf8'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 32] Broken pipe

Not sure if there is a socket flag to avoid this. A hack would be to just s.sendall('\n'.encode('utf8') first before attempting to actually writing data.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions