Commits

Alessio Deiana committed e71302d

Fixes a memory leak caused by cycles

The RequestHandler and the write/start_response would form
a cycle that could not be garbage collected

  • Participants
  • Parent commits 73388d4

Comments (0)

Files changed (1)

File src/invenio_devserver/webserver.py

 import sys
 import signal
 from urllib import unquote
+from weakref import proxy
 from SocketServer import ThreadingMixIn, ForkingMixIn
 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
 
             path_info = self.path
             query = ''
 
+        server_proxy = proxy(self.server)
+
         def shutdown_server():
-            self.server.shutdown_signal = True
+            server_proxy.shutdown_signal = True
 
         url_scheme = self.server.ssl_context is None and 'http' or 'https'
         environ = {
         headers_set = []
         headers_sent = []
 
+        self_proxy = proxy(self)
+
         def write(data):
             assert headers_set, 'write() before start_response'
             if not headers_sent:
                 status, response_headers = headers_sent[:] = headers_set
                 code, msg = status.split(None, 1)
-                self.send_response(int(code), msg)
+                self_proxy.send_response(int(code), msg)
                 header_keys = set()
                 for key, value in response_headers:
-                    self.send_header(key, value)
+                    self_proxy.send_header(key, value)
                     key = key.lower()
                     header_keys.add(key)
                 if 'content-length' not in header_keys:
-                    self.close_connection = True
-                    self.send_header('Connection', 'close')
+                    self_proxy.close_connection = True
+                    self_proxy.send_header('Connection', 'close')
                 if 'server' not in header_keys:
-                    self.send_header('Server', self.version_string())
+                    self_proxy.send_header('Server', self.version_string())
                 if 'date' not in header_keys:
-                    self.send_header('Date', self.date_time_string())
-                self.end_headers()
+                    self_proxy.send_header('Date', self.date_time_string())
+                self_proxy.end_headers()
 
             assert type(data) is str, 'applications must write bytes'
-            self.wfile.write(data)
-            self.wfile.flush()
+            self_proxy.wfile.write(data)
+            self_proxy.wfile.flush()
 
         def start_response(status, response_headers, exc_info=None):
             if exc_info: