Skip to content

Commit c20ea86

Browse files
author
sachin9082
committed
Rollback: Disable auto-logout after inactivity
- Temporarily disable 1-hour inactivity timeout - Single-session enforcement still active - Session activity tracking preserved for debugging - Auto-logout will be fixed and re-enabled after proper debugging
1 parent 541505b commit c20ea86

1 file changed

Lines changed: 31 additions & 92 deletions

File tree

‎server/auth/user_manager_postgres.py‎

Lines changed: 31 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -174,53 +174,25 @@ def validate_session(self, token):
174174

175175
session = sessions[0]
176176

177-
# AUTO-LOGOUT: Check for 1-hour inactivity timeout
178-
last_activity = session.get("last_activity")
179-
180-
# DEBUG LOGGING - Remove after debugging
181-
current_time = datetime.now()
182-
logger.info(f"[SESSION-DEBUG] User: {session['username']}")
183-
logger.info(f"[SESSION-DEBUG] Current time: {current_time} (type: {type(current_time)}, tz: {current_time.tzinfo})")
184-
logger.info(f"[SESSION-DEBUG] Last activity raw: {last_activity} (type: {type(last_activity)})")
185-
186-
if last_activity:
187-
original_last_activity = last_activity # Store original for logging
188-
189-
# Handle both string and datetime types, ensuring timezone-naive comparison
190-
if isinstance(last_activity, str):
191-
# Parse ISO format string and strip timezone info to match datetime.now()
192-
last_activity = datetime.fromisoformat(last_activity.replace("Z", "+00:00"))
193-
logger.info(f"[SESSION-DEBUG] Parsed from string: {last_activity} (tz: {last_activity.tzinfo})")
194-
# Convert to naive datetime by removing timezone info
195-
if last_activity.tzinfo is not None:
196-
last_activity = last_activity.replace(tzinfo=None)
197-
logger.info(f"[SESSION-DEBUG] After stripping timezone: {last_activity}")
198-
elif isinstance(last_activity, datetime):
199-
logger.info(f"[SESSION-DEBUG] Already datetime: {last_activity} (tz: {last_activity.tzinfo})")
200-
# If datetime object has timezone, remove it for consistent comparison
201-
if last_activity.tzinfo is not None:
202-
last_activity = last_activity.replace(tzinfo=None)
203-
logger.info(f"[SESSION-DEBUG] After stripping timezone: {last_activity}")
204-
205-
# Both datetimes are now timezone-naive, safe to subtract
206-
idle_duration = current_time - last_activity
207-
INACTIVITY_TIMEOUT = timedelta(hours=1) # 1 hour timeout
208-
209-
# DEBUG LOGGING
210-
logger.info(f"[SESSION-DEBUG] Final last_activity: {last_activity} (tz: {last_activity.tzinfo})")
211-
logger.info(f"[SESSION-DEBUG] Idle duration: {idle_duration} (seconds: {idle_duration.total_seconds()})")
212-
logger.info(f"[SESSION-DEBUG] Timeout threshold: {INACTIVITY_TIMEOUT} (seconds: {INACTIVITY_TIMEOUT.total_seconds()})")
213-
logger.info(f"[SESSION-DEBUG] Will logout? {idle_duration > INACTIVITY_TIMEOUT}")
214-
215-
if idle_duration > INACTIVITY_TIMEOUT:
216-
# Session expired due to inactivity
217-
logger.warning(f"[SESSION-TIMEOUT] User {session['username']} logged out due to inactivity")
218-
logger.warning(f"[SESSION-TIMEOUT] Idle duration: {idle_duration} > threshold: {INACTIVITY_TIMEOUT}")
219-
logger.warning(f"[SESSION-TIMEOUT] Last activity was: {original_last_activity}")
220-
self.logout(token)
221-
return None
222-
else:
223-
logger.warning(f"[SESSION-DEBUG] No last_activity found for user {session['username']}")
177+
# AUTO-LOGOUT: TEMPORARILY DISABLED
178+
#
179+
# The auto-logout after 1 hour of inactivity has been temporarily disabled
180+
# because users were getting logged out prematurely (5-6 minutes instead of 1 hour).
181+
#
182+
# Single-session enforcement (lines 89-102) is still ACTIVE and working correctly.
183+
# Activity tracking (update_session_activity) is still ACTIVE for future debugging.
184+
#
185+
# Session still expires after 24 hours (checked at line 152 via expires_at column).
186+
#
187+
# TO RE-ENABLE AFTER FIXING:
188+
# 1. Switch back to the feat/user-session branch (has the full implementation)
189+
# 2. Review DEBUG_SESSION_TIMEOUT.md for debugging steps
190+
# 3. Collect production logs to identify root cause
191+
# 4. Fix the timezone/timing issue
192+
# 5. Test thoroughly with debug logging
193+
# 6. Merge the fix back
194+
#
195+
# Original code preserved in git history and feat/user-session branch
224196

225197
# With RealDictCursor, this will always be a dict
226198
return {
@@ -644,54 +616,21 @@ def get_all_users_for_admin(self, admin_username):
644616
def cleanup_idle_sessions(self):
645617
"""
646618
Cleanup sessions that have been idle for more than 1 hour.
647-
This is called periodically by background job.
648-
"""
649-
try:
650-
INACTIVITY_TIMEOUT = timedelta(hours=1) # 1 hour timeout
651-
cutoff_time = datetime.now() - INACTIVITY_TIMEOUT
652-
653-
query = (
654-
"""
655-
UPDATE sessions SET is_active = false
656-
WHERE last_activity < %s AND is_active = true
657-
"""
658-
if self.db.is_postgres
659-
else """
660-
UPDATE sessions SET is_active = 0
661-
WHERE last_activity < ? AND is_active = 1
662-
"""
663-
)
664-
665-
result = self.db.execute_query(query, (cutoff_time,))
619+
TEMPORARILY DISABLED - Returns empty list.
666620
667-
# Get list of affected users to terminate their WebSocket connections
668-
affected_query = (
669-
"""
670-
SELECT DISTINCT u.username
671-
FROM sessions s
672-
JOIN users u ON s.user_id = u.id
673-
WHERE s.last_activity < %s AND s.is_active = false
674-
"""
675-
if self.db.is_postgres
676-
else """
677-
SELECT DISTINCT u.username
678-
FROM sessions s
679-
JOIN users u ON s.user_id = u.id
680-
WHERE s.last_activity < ? AND s.is_active = 0
681-
"""
682-
)
683-
684-
affected_users = self.db.execute_query(affected_query, (cutoff_time,))
685-
686-
if affected_users:
687-
logger.info(f"Cleaned up {len(affected_users)} idle sessions")
688-
return [user["username"] for user in affected_users]
621+
This is called periodically by IdleSessionCleanupJob (every 5 minutes)
622+
but does nothing until the auto-logout timeout issue is debugged and fixed.
689623
690-
return []
624+
The background job will continue running but won't terminate any sessions.
691625
692-
except Exception as e:
693-
logger.error(f"Error cleaning up idle sessions: {e}")
694-
return []
626+
TO RE-ENABLE AFTER FIXING:
627+
1. Switch back to feat/user-session branch (has full implementation)
628+
2. Debug and fix the timeout calculation issue
629+
3. Test thoroughly with debug logging
630+
4. Restore this method from git history
631+
"""
632+
logger.debug("Idle session cleanup temporarily disabled (rollback)")
633+
return [] # No users to cleanup
695634

696635

697636
# Background job for idle session cleanup

0 commit comments

Comments
 (0)