added exception handling to clean_up method

will submit a pull request to CherryPy project so we can remove this code when it gets merged
fix_ramsession
eric 2019-08-15 13:03:51 -04:00
parent 82e7230eca
commit 01bfd42fed
2 changed files with 39 additions and 0 deletions

View File

@ -69,6 +69,7 @@ tools.I18nTool.mo_dir: CherryPyApp.install_dir + '/i18n'
tools.I18nTool.domain: 'messages'
tools.sessions.on: True
tools.sessions.storage_class = sessions.RamSession
#tools.sessions.storage_type = "postgres"
tools.sessions.table_name = "cherrypy.sessions"
tools.sessions.timeout: 30

38
sessions.py Normal file
View File

@ -0,0 +1,38 @@
from cherrypy.lib.sessions import RamSession as cpRamSession
class RamSession(cpRamSession):
def clean_up(self):
"""Clean up expired sessions."""
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:
"""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.
"""
return
# added to remove obsolete lock objects
for _id in list(self.locks):
locked = (
_id not in self.cache
and self.locks[_id].acquire(blocking=False)
)
if locked:
lock = self.locks.pop(_id)
lock.release()