Commit 6838aa8b by Matjaz Gregoric

Patch wsgi handler to ignore broken pipe errors.

We would often see 'Broken pipe' errors pulluting the logs when running tests.
Broken pipe errors occur when browser aborts connection. It can be because
the browser was navigated to a different url before the current page finished
loading, but it could also be because it started downloading an image, but decided
that the image didn't change after loading part of it, and aborts the connection
to use the version from the cache.

The broken pipe errors are benign and are actually suppressed by default in Django 1.8.
parent df6addce
...@@ -9,7 +9,6 @@ because the workbench SDK's settings file is not inside any python module. ...@@ -9,7 +9,6 @@ because the workbench SDK's settings file is not inside any python module.
import os import os
import sys import sys
import logging import logging
logging_level_overrides = { logging_level_overrides = {
...@@ -18,7 +17,35 @@ logging_level_overrides = { ...@@ -18,7 +17,35 @@ logging_level_overrides = {
'workbench.runtime': logging.ERROR, 'workbench.runtime': logging.ERROR,
} }
def patch_broken_pipe_error():
"""Monkey Patch BaseServer.handle_error to not write a stacktrace to stderr on broken pipe.
This message is automatically suppressed in Django 1.8, so this monkey patch can be
removed once the workbench upgrades to Django >= 1.8.
http://stackoverflow.com/a/22618740/51397"""
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
if __name__ == "__main__": if __name__ == "__main__":
patch_broken_pipe_error()
# Use the workbench settings file: # Use the workbench settings file:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "workbench.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "workbench.settings")
# Configure a range of ports in case the default port of 8081 is in use # Configure a range of ports in case the default port of 8081 is in use
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment