Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to resolve state sessions race condition #977

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions mesop/server/state_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import threading
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Protocol
Expand All @@ -13,6 +14,8 @@
from mesop.exceptions import MesopException
from mesop.server.config import Config, app_config

mutex = threading.Lock()

States = dict[type[Any], object]


Expand Down Expand Up @@ -72,6 +75,7 @@ class MemoryStateSessionBackend(StateSessionBackend):

def __init__(self):
self.cache = {}
self.last_checked_sessions = datetime(1900, 1, 1)

def restore(self, token: str, states: States):
if token not in self.cache:
Expand All @@ -89,15 +93,37 @@ def save(self, token: str, states: States):
def clear_stale_sessions(self):
stale_keys = set()

# Avoid trying to clear sessions with every request to avoid unnecessary processing.
current_time = _current_datetime()
for key, (timestamp, _) in self.cache.items():
if (
self.last_checked_sessions + timedelta(minutes=self._SESSION_TTL_MINUTES)
> current_time
):
return

self.last_checked_sessions = current_time

# Add a mutex in the case where a cache item may be removed while looping through
richard-to marked this conversation as resolved.
Show resolved Hide resolved
# the cache. Limit scope of the lock by just getting the cache keys.
with mutex:
cache_keys = list(self.cache)

for key in cache_keys:
timestamp, _ = self.cache.get(key, (None, None))
if (
timestamp + timedelta(minutes=self._SESSION_TTL_MINUTES) < current_time
timestamp
and timestamp + timedelta(minutes=self._SESSION_TTL_MINUTES)
< current_time
):
stale_keys.add(key)

for key in stale_keys:
del self.cache[key]
try:
del self.cache[key]
except KeyError:
logging.warning(
f"Tried to delete non-existent memory cache entry {key}."
)


class FileStateSessionBackend(StateSessionBackend):
Expand Down
Loading