Fix for Exception when the server is restarted. Fixes #2713

This issue was caused because we recently added session_write_delay in session.
So session won't be written/updated to disk from memory until specified seconds are elapsed.

However we must forcefully write/update session to disk if user loges in or out irrespective of session_write_delay to keep sessions from memory and disk in sync as user logged in status is kept in session.
This commit is contained in:
Harshal Dhumal 2017-09-18 14:39:43 +01:00 committed by Dave Page
parent bdc08d94c4
commit f0ad26ba7a
2 changed files with 8 additions and 3 deletions

View File

@ -17,7 +17,7 @@ from importlib import import_module
from flask import Flask, abort, request, current_app, session, url_for
from flask_babel import Babel, gettext
from flask_htmlmin import HTMLMIN
from flask_login import user_logged_in
from flask_login import user_logged_in, user_logged_out
from flask_security import Security, SQLAlchemyUserDatastore
from flask_mail import Mail
from flask_security.utils import login_user
@ -368,7 +368,6 @@ def create_app(app_name=None):
##########################################################################
@user_logged_in.connect_via(app)
def on_user_logged_in(sender, user):
# Keep hold of the user ID
user_id = user.id
@ -497,6 +496,10 @@ def create_app(app_name=None):
except:
pass
@user_logged_in.connect_via(app)
@user_logged_out.connect_via(app)
def force_session_write(app, user):
session.force_write = True
##########################################################################
# Load plugin modules

View File

@ -58,6 +58,7 @@ class ManagedSession(CallbackDict, SessionMixin):
self.modified = False
self.randval = randval
self.last_write = None
self.force_write = False
self.hmac_digest = hmac_digest
def sign(self, secret):
@ -219,12 +220,13 @@ class FileBackedSessionManager(SessionManager):
current_time = time.time()
if not session.hmac_digest:
session.sign(self.secret)
else:
elif not session.force_write:
if session.last_write is not None \
and (current_time - float(session.last_write)) < self.disk_write_delay:
return
session.last_write = current_time
session.force_write = False
fname = os.path.join(self.path, session.sid)
with open(fname, 'wb') as f:
dump(