Django Broken pipe in Debug mode

前端 未结 8 1063
灰色年华
灰色年华 2020-11-28 23:45

I have django 1.3 on the remote server behind Nginx.

If I run django with apache + mod_wsgi, I can watch errors in apache log files. It\'s ok but I\'d like to have

相关标签:
8条回答
  • 2020-11-29 00:07

    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.

    0 讨论(0)
  • 2020-11-29 00:09

    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.

    0 讨论(0)
  • 2020-11-29 00:10

    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'.

    0 讨论(0)
  • 2020-11-29 00:11

    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()
    
    0 讨论(0)
  • 2020-11-29 00:15

    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
    
    0 讨论(0)
  • 2020-11-29 00:18

    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()
    
    0 讨论(0)
提交回复
热议问题