[#5326] avoid "Broken pipe" error we were seeing, by providing ErrorMiddleware with an explicit wsgi.errors stream
diff --git a/Allura/allura/command/taskd.py b/Allura/allura/command/taskd.py
index 554efdb..f9bd6ef 100644
--- a/Allura/allura/command/taskd.py
+++ b/Allura/allura/command/taskd.py
@@ -59,6 +59,11 @@
         if only:
             only = only.split(',')
 
+        # errors get logged via regular logging and also recorded into the mongo task record
+        # so this is generally not needed, and only present to avoid errors within
+        # weberror's ErrorMiddleware if the default error stream (stderr?) doesn't work
+        wsgi_error_log = open(pylons.config.get('taskd.wsgi_log', '/dev/null'), 'a')
+
         def start_response(status, headers, exc_info=None):
             pass
 
@@ -93,7 +98,10 @@
                             only=only)
                     if self.task:
                         # Build the (fake) request
-                        r = Request.blank('/--%s--/' % self.task.task_name, dict(task=self.task))
+                        r = Request.blank('/--%s--/' % self.task.task_name,
+                                          {'task': self.task,
+                                           'wsgi.errors': wsgi_error_log,  # ErrorMiddleware records error details here
+                                           })
                         list(wsgi_app(r.environ, start_response))
                         self.task = None
             except Exception as e:
@@ -102,6 +110,8 @@
                     time.sleep(10)
                 else:
                     base.log.exception('taskd error %s' % e)
+            finally:
+                wsgi_error_log.flush()
         base.log.info('taskd pid %s stopping gracefully.' % os.getpid())
 
         if self.restart_when_done: