Commits

Alessio Deiana committed c14d2e5

Nicer message when a client disconnects

Comments (0)

Files changed (1)

src/invenio_devserver/webserver.py

                 self_proxy.end_headers()
 
             assert type(data) is str, 'applications must write bytes'
-            self_proxy.wfile.write(data)
-            self_proxy.wfile.flush()
+            try:
+                self_proxy.wfile.write(data)
+                self_proxy.wfile.flush()
+            except socket.error, e:
+                if e.errno == 32:
+                    _log('info', ' * Client disconnected')
+                else:
+                    raise
 
         def start_response(status, response_headers, exc_info=None):
             if exc_info:
                                          self.log_date_time_string(),
                                          message % args))
 
+    def finish(self):
+        if not self.wfile.closed:
+            try:
+                self.wfile.flush()
+            except socket.error:
+                # An final socket error may have occurred here, such as
+                # the local error ECONNABORTED.
+                pass
+        try:
+            self.wfile.close()
+        except socket.error, e:
+            if e.errno == 32:
+                _log('info', ' * Client disconnected')
+            else:
+                raise
+        self.rfile.close()
+
 
 class BaseWSGIServer(HTTPServer, object):
     """Simple single-threaded, single-process WSGI server."""
     def log(self, type, message, *args):
         _log(type, message, *args)
 
-    def serve_forever(self):
+    def serve_forever(self, poll_interval=0.5):
         self.shutdown_signal = False
         try:
             _log('info', ' * Ready')
-            HTTPServer.serve_forever(self)
+            HTTPServer.serve_forever(self, poll_interval)
         except KeyboardInterrupt:
             pass