78👍
This isn’t really an issue with your site, more with the Django devserver: see this Django ticket. To put it bluntly, just ignore it as it is a known error, and won’t be fixed.
In that ticket’s comments a quite clear explanation is given:
According to many sources the ‘Broken Pipe’ is a normal browser quirk. For example, the browser reads from the socket and then decides that the image it’s been reading apparently didn’t change. The browser now this (forcefully) closes the connection because it does not need more data. The other end of this socket (the python runserver) now raises a socket exception telling the program that the client ‘Broke the socket pipe’.
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-"gettext()" vs "gettext_lazy()" in Django
- [Django]-How to create a fixture file
9👍
The nginx directive (checked answer) didn’t work for me, but combining monkey patches from Igor Katson and Michael_Scharf did:
def patch_broken_pipe_error():
"""Monkey Patch BaseServer.handle_error to not write
a stacktrace to stderr on broken pipe.
http://stackoverflow.com/a/22618740/362702"""
import sys
from SocketServer import BaseServer
from wsgiref import handlers
handle_error = BaseServer.handle_error
log_exception = handlers.BaseHandler.log_exception
def is_broken_pipe_error():
type, err, tb = sys.exc_info()
return repr(err) == "error(32, 'Broken pipe')"
def my_handle_error(self, request, client_address):
if not is_broken_pipe_error():
handle_error(self, request, client_address)
def my_log_exception(self, exc_info):
if not is_broken_pipe_error():
log_exception(self, exc_info)
BaseServer.handle_error = my_handle_error
handlers.BaseHandler.log_exception = my_log_exception
patch_broken_pipe_error()
- [Django]-Rendering a template variable as HTML
- [Django]-How would you create a 'manual' django migration?
- [Django]-Comma separated lists in django templates
4👍
Here is a way to prevent the to print the message to stderr. Just monkey patch the
BaseServer.handle_error
function. This is how I do it:
def patch_broken_pipe_error():
"""Monkey Patch BaseServer.handle_error to not write
a stacktrace to stderr on broken pipe.
https://stackoverflow.com/a/7913160"""
import sys
from SocketServer import BaseServer
handle_error = BaseServer.handle_error
def my_handle_error(self, request, client_address):
type, err, tb = sys.exc_info()
# there might be better ways to detect the specific erro
if repr(err) == "error(32, 'Broken pipe')":
# you may ignore it...
logging.getLogger('mylog').warn(err)
else:
handle_error(self, request, client_address)
BaseServer.handle_error = my_handle_error
patch_broken_pipe_error()
- [Django]-Django upgrading to 1.9 error "AppRegistryNotReady: Apps aren't loaded yet."
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-Django – Render the <label> of a single form field
4👍
In Django 3.2+ the "error" is an INFO
reported by logging
similar to:
INFO - Broken pipe from ('127.0.0.1', 59895)
@igor-katson has a good quick and dirty monkey patch. Updated for 3.2+ it can be:
# Suppress broken pipe errors
from django.core.servers.basehttp import WSGIServer
WSGIServer.handle_error = lambda *args, **kwargs: None
If you have a conditional check for being in development (runserver) mode in your settings.py
, you could put this code there. It will squelch that annoying log entry.
Keep in mind, WSGIServer.handle_error
also reports non-socket errors. If you had some other connectivity issue to debug this may get in the way.
- [Django]-Access request in django custom template tags
- [Django]-Feedback on using Google App Engine?
- [Django]-Django get a QuerySet from array of id's in specific order
3👍
I was able to get rid of this by
proxy_buffering off;
This stops response buffering of proxied server. This leads to other issues of the back-end application being locked for long if the client is on a extremely slow connection.
To make it conditional for particular requests, use X-Accel-Buffering=no in the response header.
- [Django]-Favorite Django Tips & Features?
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-Django-taggit – how do I display the tags related to each record
3👍
I came up with a quick and dirty monkey patch (i don’t know if it supresses any useful errors), that gets rid of this annoying error when using “./manage.py runserver” or running LiveServerTestCase tests.
Just insert it anywhere in your code, where you need that:
# Monkeypatch python not to print "Broken Pipe" errors to stdout.
import SocketServer
from wsgiref import handlers
SocketServer.BaseServer.handle_error = lambda *args, **kwargs: None
handlers.BaseHandler.log_exception = lambda *args, **kwargs: None
- [Django]-How would you create a 'manual' django migration?
- [Django]-Error when using django.template
- [Django]-How to mix queryset results?
1👍
I fixed it.
If you use links i.e, anchor tag, inside the page, you have to face the “Borken Pipe” problem. Just use inside the link tag href=’#’. Don’t leave the href attribute blank. It will avoid that type of error.
- [Django]-Feedback on using Google App Engine?
- [Django]-Celery discover tasks in files with other filenames
- [Django]-Raise a validation error in a model's save method in Django
1👍
My BrokenPipe error disappears when I remove the "/" in the path in "urls.py"
urlpatterns = [
path('', views.index)
]
- [Django]-Adding new custom permissions in Django
- [Django]-How do I restrict foreign keys choices to related objects only in django
- [Django]-When should I use a custom Manager versus a custom QuerySet in Django?
1👍
I turned
DEBUG = True
and added
if os.getcwd() == '/app':
DEBUG = False
now it is working
- [Django]-How to add data into ManyToMany field?
- [Django]-Http POST drops port in URL
- [Django]-Django + Ajax
0👍
I came across this issue as well while using tilelite. It’s actually caused by a known, and now fixed, bug in python. You can resolve this issue by applying the following patch:
http://bugs.python.org/issue14574
Otherwise, you can download one of the more recent builds of python.
- [Django]-Execute code when Django starts ONCE only?
- [Django]-Dynamically adding a form to a Django formset
- [Django]-Convert Django Model object to dict with all of the fields intact
0👍
Now currently it should be (in settings.py
):
# Miscellaneous patch code
def patch_broken_pipe_error():
"""Monkey Patch BaseServer.handle_error to not write
a stacktrace to stderr on broken pipe.
http://stackoverflow.com/a/22618740/362702"""
import sys
from socketserver import BaseServer
from wsgiref import handlers
handle_error = BaseServer.handle_error
log_exception = handlers.BaseHandler.log_exception
def is_broken_pipe_error():
type, err, tb = sys.exc_info()
return repr(err) == "error(32, 'Broken pipe')"
def my_handle_error(self, request, client_address):
if not is_broken_pipe_error():
handle_error(self, request, client_address)
def my_log_exception(self, exc_info):
if not is_broken_pipe_error():
log_exception(self, exc_info)
BaseServer = my_handle_error
handlers.BaseHandler.log_exception = my_log_exception
patch_broken_pipe_error()
Note the lowercase socketserver
import line.
- [Django]-The right place to keep my signals.py file in a Django project
- [Django]-Pass extra arguments to Serializer Class in Django Rest Framework
- [Django]-Add additional options to Django form select widget