Skip to content

Commit b1fe14f

Browse files
committed
Fix session isssue due to Flask upgrade.
1 parent 1f66714 commit b1fe14f

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

web/pgadmin/utils/session.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from werkzeug.security import safe_join
3939
from werkzeug.exceptions import InternalServerError
4040

41+
from flask import has_request_context
42+
4143
from pgadmin.utils.ajax import make_json_response
4244

4345

@@ -100,7 +102,6 @@ def put(self, session):
100102
'Store a managed session'
101103
raise NotImplementedError
102104

103-
104105
class CachingSessionManager(SessionManager):
105106
def __init__(self, parent, num_to_store, skip_paths=None):
106107
self.parent = parent
@@ -115,6 +116,17 @@ def _normalize(self):
115116
while len(self._cache) > (self.num_to_store * 0.8):
116117
self._cache.popitem(False)
117118

119+
def is_session_ready(self, _session):
120+
if not has_request_context():
121+
return False
122+
123+
# ._get_current_object() returns the actual dict-like object
124+
# or None if it hasn't been set yet.
125+
try:
126+
return _session._get_current_object() is not None
127+
except (AssertionError, RuntimeError):
128+
return False
129+
118130
def new_session(self):
119131
session = self.parent.new_session()
120132

@@ -146,13 +158,13 @@ def get(self, sid, digest):
146158
with sess_lock:
147159
if sid in self._cache:
148160
session = self._cache[sid]
149-
if session and session.hmac_digest != digest:
161+
if self.is_session_ready(session) and session.hmac_digest != digest:
150162
session = None
151163

152164
# reset order in Dict
153165
del self._cache[sid]
154166

155-
if not session:
167+
if not self.is_session_ready(session):
156168
session = self.parent.get(sid, digest)
157169

158170
# Do not store the session if skip paths

0 commit comments

Comments
 (0)