From 213b0264cc0f918f11c2f9b0b38598949af83e24 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 16 Aug 2019 13:18:02 -0400 Subject: [PATCH 1/2] improved ramsession patch Ref: https://github.com/cherrypy/cherrypy/pull/1804 --- sessions.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/sessions.py b/sessions.py index 10f95d9..240098e 100644 --- a/sessions.py +++ b/sessions.py @@ -8,24 +8,29 @@ class RamSession(cpRamSession): now = self.now() try: - for _id, (data, expiration_time) in list(self.cache.items()): - if expiration_time <= now: - try: - del self.cache[_id] - except KeyError: - pass - try: - if self.locks[_id].acquire(blocking=False): - lock = self.locks.pop(_id) - lock.release() - except KeyError: - pass - - except RuntimeError: + cache_items_copy = self.cache.copy().items() + except RuntimeError as re: """Under heavy load, list(self.cache.items()) will occasionally raise this error for large session caches with message "dictionary changed size during iteration" Better to pause the cleanup than to let the cleanup thread die. """ + cherrypy.log(f'Runtime Error happened while copying the cache entries: {re}') + cherrypy.log('Ref: https://github.com/cherrypy/cherrypy/pull/1804') + return + + for _id, (data, expiration_time) in cache_items_copy: + if expiration_time <= now: + try: + del self.cache[_id] + except KeyError: + pass + try: + if self.locks[_id].acquire(blocking=False): + lock = self.locks.pop(_id) + lock.release() + except KeyError: + pass + return From fdb64ca0e01be6f08617263734ce553e964fbb29 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 16 Aug 2019 13:36:50 -0400 Subject: [PATCH 2/2] add import for log --- sessions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sessions.py b/sessions.py index 240098e..ff8a1b8 100644 --- a/sessions.py +++ b/sessions.py @@ -1,5 +1,5 @@ # will submit a PR to CherryPy project - if it gets merged we can remove it. ESH 8/15/2019 - +import cherrypy from cherrypy.lib.sessions import RamSession as cpRamSession class RamSession(cpRamSession):