[Solved] How to prevent errno 32 broken pipe?

Currently I am using an app built in python. When I run it in personal computer, it works without problems.

However, when I move it into a production server. It keeps showing me the error attached as below:.

I’ve done some research and I got the reason that the end user browser stops the connection while the server is still busy sending data.

I wonder why did it happen and what is the root cause that prevents it from running properly in production server, while it works on my personal computer. Any advice is appreciated

    Exception happened during processing of request from ('', 34226)
Traceback (most recent call last):
  File "/usr/lib/python2.7/", line 284, in
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/", line 310, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/", line 641, in __init__
  File "/usr/lib/python2.7/", line 694, in finish
  File "/usr/lib/python2.7/", line 303, in flush
error: [Errno 32] Broken pipe

Solution #1:

Your server process has received a SIGPIPE writing to a socket. This usually happens when you write to a socket fully closed on the other (client) side. This might be happening when a client program doesn’t wait till all the data from the server is received and simply closes a socket (using close function).

In a C program you would normally try setting to ignore SIGPIPE signal or setting a dummy signal handler for it. In this case a simple error will be returned when writing to a closed socket. In your case a python seems to throw an exception that can be handled as a premature disconnect of the client.

Respondent: Maksim Skurydzin

Solution #2:

The broken pipe error usually occurs if your request is blocked or takes too long and after request-side timeout, it’ll close the connection and then, when the respond-side (server) tries to write to the socket, it will throw a pipe broken error.

Respondent: Aaren Shar

Solution #3:

It depends on how you tested it, and possibly on differences in the TCP stack implementation of the personal computer and the server.

For example, if your sendall always completes immediately (or very quickly) on the personal computer, the connection may simply never have broken during sending. This is very likely if your browser is running on the same machine (since there is no real network latency).

In general, you just need to handle the case where a client disconnects before you’re finished, by handling the exception.

Remember that TCP communications are asynchronous, but this is much more obvious on physically remote connections than on local ones, so conditions like this can be hard to reproduce on a local workstation. Specifically, loopback connections on a single machine are often almost synchronous.

Respondent: Useless

Solution #4:

This might be because you are using two method for inserting data into database and this cause the site to slow down.

def add_subscriber(request, email=None):
    if request.method == 'POST':
        email = request.POST['email_field']
        e = Subscriber.objects.create(email=email).save()  <==== 
        return HttpResponseRedirect('/')
        return HttpResponseRedirect('/')

In above function, the error is where arrow is pointing. The correct implementation is below:

def add_subscriber(request, email=None):
    if request.method == 'POST':
        email = request.POST['email_field']
        e = Subscriber.objects.create(email=email)
        return HttpResponseRedirect('/')
        return HttpResponseRedirect('/')
Respondent: Kuldeep K. Rishi

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy