Skip to content

fix(gateway): prevent re-flushing expired sessions on restart#2510

Closed
dlkakbs wants to merge 2 commits into
NousResearch:mainfrom
dlkakbs:fix/session-expiry-reflush
Closed

fix(gateway): prevent re-flushing expired sessions on restart#2510
dlkakbs wants to merge 2 commits into
NousResearch:mainfrom
dlkakbs:fix/session-expiry-reflush

Conversation

@dlkakbs

@dlkakbs dlkakbs commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2506

The background _session_expiry_watcher repeatedly flushes memories for the same expired sessions after every gateway restart, causing a flood of LLM API calls and log noise.

Root Cause

_pre_flushed_sessions is an in-memory set() that resets on gateway restart. Flushed sessions remain in sessions.json, so every expired session is re-flushed on restart → O(N) LLM API calls.

Fix

After successful memory flush, remove the session entry from _entries and persist to disk. Expired sessions no longer exist in storage after being processed.

Cleanup

Removed the now-unused _pre_flushed_sessions set and related code from:

  • gateway/run.py - skip check and add() call
  • gateway/session.py - set definition and discard() call
  • tests/gateway/test_async_memory_flush.py - updated tests

Testing

test_async_memory_flush.py - 7/7 PASSED

dlkakbs and others added 2 commits March 22, 2026 18:41
Previously, flushed session IDs were tracked in an in-memory set
(_pre_flushed_sessions) that reset on gateway restart. Expired sessions
remained in sessions.json, causing redundant LLM API calls for memory
flush on every restart.

Fix: After successful memory flush, remove the session entry from
_entries and persist to disk. This eliminates the re-flush storm since
expired sessions no longer exist in storage after being processed.

Cleanup: Remove the now-unused _pre_flushed_sessions set and related
code from run.py, session.py, and update tests accordingly.

Fixes NousResearch#2506
@teknium1

teknium1 commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Thanks @dlkakbs — you were the first to identify this bug and propose the entry-removal approach. The fix landed in #4481, which preserves session entries in sessions.json (for debugging) by adding a memory_flushed flag instead of removing them. Your approach of removing _pre_flushed_sessions entirely was adopted — it's dead code once the flag is persisted. Appreciate you catching this!

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