Have you ever seen a socket.error: [Errno 32] Broken pipe message when running a Python Web server and wondered what that means?
The rule is that when a process tries to write to a socket that has already received an RST packet, the SIGPIPE signal is sent to that process which causes the Broken pipe socket.error exception.
Here are two scenarios that you can try that cause SIGPIPE signal to be fired.
1. Server may send an RST packet to a client to abort the socket connection but the client ignores the packet and continues to write to the socket.
To test that behavior install Cynic, run it
To test it run the client2.py:
The rule is that when a process tries to write to a socket that has already received an RST packet, the SIGPIPE signal is sent to that process which causes the Broken pipe socket.error exception.
Here are two scenarios that you can try that cause SIGPIPE signal to be fired.
1. Server may send an RST packet to a client to abort the socket connection but the client ignores the packet and continues to write to the socket.
To test that behavior install Cynic, run it
$ cynic INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPHtmlResponse' on port 2000 INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPJsonResponse' on port 2001 INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPNoBodyResponse' on port 2002 INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPSlowResponse' on port 2003 INFO [2012-06-08 05:06:37,040] server: Starting 'RSTResponse' on port 2020 INFO [2012-06-08 05:06:37,040] server: Starting 'RandomDataResponse' on port 2021 INFO [2012-06-08 05:06:37,040] server: Starting 'NoResponse' on port 2022 INFO [2012-06-08 05:06:37,041] server: Starting 'LogRecordHandler' on port /tmp/_cynic.sockand then run the client1.py:
import socket
# connect to Cynic's RSTResponse service on port 2020
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 2020))
# first read gets an RST packet
try:
s.recv(1024)
except socket.error as e:
print e
print
# write after getting the RST causes SIGPIPE signal
# to be sent to this process which causes a socket.error
# exception
s.send('hello')
and see what happens:$ python ./client1.py [Errno 104] Connection reset by peer Traceback (most recent call last): File "./client1.py", line 17, in s.send('hello') socket.error: [Errno 32] Broken pipe2. Server can send an RST to the client’s SYN request to indicate that there is no process wating for connections on the host at the specified port, but the client tries to write to the socket anyway.
To test it run the client2.py:
import socket
# connect to the port that nobody is listening on
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# gets us an RST packet
try:
s.connect(('', 30301))
except socket.error as e:
print e
print
# write after getting RST causes SIGPIPE signal
# to be sent to this process which causes an exception
s.send('hello')
Here is the output: $ python ./client2.py [Errno 111] Connection refused Traceback (most recent call last): File "./sigpipe2.py", line 15, in s.send('hello') socket.error: [Errno 32] Broken pipeI hope that clarifies a SIGPIPE’s nature a little bit.
Комментариев нет:
Отправить комментарий