Spaces:
Running
Running
Joffrey Thomas
commited on
Commit
·
986eaf4
1
Parent(s):
903f1a8
change app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,7 @@ SAMPLE_RATE = 16_000
|
|
| 23 |
WARMUP_DURATION = 2.0 # seconds of silence for warmup
|
| 24 |
WPM_WINDOW = 10 # seconds for running mean calculation
|
| 25 |
CALIBRATION_PERIOD = 5 # seconds before showing WPM
|
| 26 |
-
SESSION_TIMEOUT = int(os.environ.get("SESSION_TIMEOUT", "
|
| 27 |
INACTIVITY_TIMEOUT = int(os.environ.get("INACTIVITY_TIMEOUT", "10")) # Close after 10s silence
|
| 28 |
MAX_CONCURRENT_SESSIONS = int(os.environ.get("MAX_SESSIONS", "50"))
|
| 29 |
|
|
@@ -44,7 +44,7 @@ _sessions_lock = threading.Lock()
|
|
| 44 |
_session_registry = {}
|
| 45 |
_registry_lock = threading.Lock()
|
| 46 |
_last_cleanup = time.time()
|
| 47 |
-
SESSION_REGISTRY_CLEANUP_INTERVAL =
|
| 48 |
SESSION_MAX_AGE = 30 # 30 seconds - remove sessions older than this
|
| 49 |
|
| 50 |
|
|
@@ -80,20 +80,44 @@ def get_or_create_session(session_id: str = None) -> "UserSession":
|
|
| 80 |
def _cleanup_stale_sessions():
|
| 81 |
"""Remove sessions that haven't been accessed recently."""
|
| 82 |
now = time.time()
|
| 83 |
-
|
|
|
|
| 84 |
|
|
|
|
| 85 |
with _registry_lock:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
def cleanup_session(session_id: str):
|
|
|
|
| 23 |
WARMUP_DURATION = 2.0 # seconds of silence for warmup
|
| 24 |
WPM_WINDOW = 10 # seconds for running mean calculation
|
| 25 |
CALIBRATION_PERIOD = 5 # seconds before showing WPM
|
| 26 |
+
SESSION_TIMEOUT = int(os.environ.get("SESSION_TIMEOUT", "50")) # Max 30s per session
|
| 27 |
INACTIVITY_TIMEOUT = int(os.environ.get("INACTIVITY_TIMEOUT", "10")) # Close after 10s silence
|
| 28 |
MAX_CONCURRENT_SESSIONS = int(os.environ.get("MAX_SESSIONS", "50"))
|
| 29 |
|
|
|
|
| 44 |
_session_registry = {}
|
| 45 |
_registry_lock = threading.Lock()
|
| 46 |
_last_cleanup = time.time()
|
| 47 |
+
SESSION_REGISTRY_CLEANUP_INTERVAL = 30 # seconds
|
| 48 |
SESSION_MAX_AGE = 30 # 30 seconds - remove sessions older than this
|
| 49 |
|
| 50 |
|
|
|
|
| 80 |
def _cleanup_stale_sessions():
|
| 81 |
"""Remove sessions that haven't been accessed recently."""
|
| 82 |
now = time.time()
|
| 83 |
+
to_remove_from_registry = []
|
| 84 |
+
to_remove_from_active = []
|
| 85 |
|
| 86 |
+
# Need both locks to safely check both dictionaries
|
| 87 |
with _registry_lock:
|
| 88 |
+
with _sessions_lock:
|
| 89 |
+
# Find stale sessions in registry
|
| 90 |
+
for session_id, session in _session_registry.items():
|
| 91 |
+
# NEVER remove if still in active_sessions (websocket still running)
|
| 92 |
+
if session_id in _active_sessions:
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
last_accessed = getattr(session, '_last_accessed', 0)
|
| 96 |
+
# Remove if: not running AND not active AND old
|
| 97 |
+
if not session.is_running and (now - last_accessed > SESSION_MAX_AGE):
|
| 98 |
+
to_remove_from_registry.append(session_id)
|
| 99 |
+
|
| 100 |
+
# Find orphaned sessions in active_sessions (not in registry anymore)
|
| 101 |
+
for session_id, session in list(_active_sessions.items()):
|
| 102 |
+
if session_id not in _session_registry:
|
| 103 |
+
# Orphaned - mark for removal
|
| 104 |
+
if not session.is_running:
|
| 105 |
+
to_remove_from_active.append(session_id)
|
| 106 |
+
|
| 107 |
+
# Clean up registry
|
| 108 |
+
for session_id in to_remove_from_registry:
|
| 109 |
+
_session_registry.pop(session_id, None)
|
| 110 |
+
|
| 111 |
+
# Clean up orphaned active sessions
|
| 112 |
+
for session_id in to_remove_from_active:
|
| 113 |
+
_active_sessions.pop(session_id, None)
|
| 114 |
+
|
| 115 |
+
active_count = len(_active_sessions)
|
| 116 |
+
registry_count = len(_session_registry)
|
| 117 |
|
| 118 |
+
total_cleaned = len(to_remove_from_registry) + len(to_remove_from_active)
|
| 119 |
+
if total_cleaned > 0:
|
| 120 |
+
print(f"Cleaned up {len(to_remove_from_registry)} stale + {len(to_remove_from_active)} orphaned sessions. Registry: {registry_count}, Active: {active_count}")
|
| 121 |
|
| 122 |
|
| 123 |
def cleanup_session(session_id: str):
|