Robert Brewer avatar Robert Brewer committed 6bccd3c

clean_interrupt was calling clean_thread.cancel() but not .join(). Session.clean_thread is also now a PerpetualTimer, so it keeps the same thread rather than spawning new ones on each run.

Comments (0)

Files changed (1)


 from cherrypy.lib import http
+class PerpetualTimer(threading._Timer):
+    def run(self):
+        while True:
+            self.finished.wait(self.interval)
+            if self.finished.isSet():
+                return
+            self.function(*self.args, **self.kwargs)
 class Session(object):
     """A CherryPy dict-like Session object (one per request).
             if self._load() is not None:
        = None
-    def clean_cycle(self):
-        """Clean up expired sessions at regular intervals."""
-        # clean_thread is both a cancelable Timer and a flag.
-        if self.clean_thread:
-            self.clean_up()
-            t = threading.Timer(self.clean_freq, self.clean_cycle)
-            self.__class__.clean_thread = t
-            t.start()
     def clean_interrupt(cls):
-        """Stop the expired-session cleaning cycle."""
+        """Stop the expired-session cleaning timer."""
         if cls.clean_thread:
+            cls.clean_thread.join()
             cls.clean_thread = None
     clean_interrupt = classmethod(clean_interrupt)
             self._data = data[0]
         self.loaded = True
+        # Stick the clean_thread in the class, not the instance.
+        # The instances are created and destroyed per-request.
         cls = self.__class__
         if not cls.clean_thread:
-            # Use the instance to call clean_cycle so tool config
-            # can be accessed inside the method.
-            cls.clean_thread = t = threading.Timer(self.clean_freq,
-                                                   self.clean_cycle)
+            # clean_up is in instancemethod and not a classmethod,
+            # so tool config can be accessed inside the method.
+            t = PerpetualTimer(self.clean_freq, self.clean_up)
+            t.setName("CP Session Cleanup")
+            cls.clean_thread = t
     def delete(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.