Skip to content

fix(gateway): remove expired session entries after flush to prevent restart re-flushing#2527

Closed
teyrebaz33 wants to merge 1 commit into
NousResearch:mainfrom
teyrebaz33:fix/session-expiry-watcher-restart-reflushing
Closed

fix(gateway): remove expired session entries after flush to prevent restart re-flushing#2527
teyrebaz33 wants to merge 1 commit into
NousResearch:mainfrom
teyrebaz33:fix/session-expiry-watcher-restart-reflushing

Conversation

@teyrebaz33

Copy link
Copy Markdown
Contributor

Fixes #2506

Problem

_session_expiry_watcher used an in-memory _pre_flushed_sessions set to track already-flushed sessions. Since this set resets to empty on every gateway restart, every expired session was re-flushed on each restart — causing an O(N) flood of LLM API calls where N = number of stale session entries.

Root Cause

_pre_flushed_sessions was never persisted to disk. On restart it reset to empty, so the watcher re-flushed every expired session in sessions.json.

Fix

After a successful flush, remove the entry from _entries and call _save() — persisting the removal to sessions.json. On restart, the entry no longer exists, so it cannot be re-flushed.

Also removes the now-unnecessary _pre_flushed_sessions set and its discard() call in get_or_create_session() — this was dead code once entries are deleted on flush.

Changes

  • gateway/run.py: remove _pre_flushed_sessions check/add, replace with _entries.pop() + _save()
  • gateway/session.py: remove _pre_flushed_sessions attribute and discard() call
  • tests/gateway/test_async_memory_flush.py: remove stale _pre_flushed_sessions tests, add test_pre_flushed_sessions_attr_removed to assert the set is gone

@teknium1

teknium1 commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Thanks @teyrebaz33! Same fix as #2510 — you independently arrived at the same conclusion on the same day. The fix landed in #4481 with a memory_flushed flag approach that preserves session entries for debugging. Appreciate the contribution!

@teknium1 teknium1 closed this Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_session_expiry_watcher re-flushes expired sessions on every gateway restart

2 participants