fix(gateway): remove expired sessions from disk after proactive memory flush#4362
Closed
insecurejezza wants to merge 1 commit into
Closed
fix(gateway): remove expired sessions from disk after proactive memory flush#4362insecurejezza wants to merge 1 commit into
insecurejezza wants to merge 1 commit into
Conversation
…y flush The session expiry watcher (_session_expiry_watcher) flushes memories for expired sessions to Honcho, then marks them in the in-memory _pre_flushed_sessions set to prevent double-flushing within a single process lifetime. However, _pre_flushed_sessions is never persisted to disk. On gateway restart, the set resets to empty while the expired session entries remain in sessions.json. The watcher rediscovers and re-flushes the same sessions, blocking the event loop and making all platforms (Discord, etc.) unresponsive for 10-15+ minutes per restart cycle. Fix: after a successful flush, pop the entry from the session store and persist the removal to disk. This ensures flushed expired sessions are never re-discovered on restart. Adds two regression tests: - Verifies flushed entries are removed from disk - Demonstrates the original bug (in-memory guard lost on restart)
Contributor
|
Thanks @insecurejezza! This bug was real — the in-memory Merged via #4481 with a slightly different approach: instead of removing entries from |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a bug where the gateway becomes unresponsive after a restart due to the proactive memory flush loop re-flushing already-flushed expired sessions.
The
_session_expiry_watcher(introduced in d80c30c) flushes memories for expired sessions to Honcho, then tracks them in the in-memory_pre_flushed_sessionsset to prevent double-flushing within a single process lifetime. However, this set is never persisted to disk. On gateway restart, the set resets to empty while the expired session entries remain insessions.json. The watcher rediscovers and re-flushes the same sessions every restart cycle, blocking the event loop and making all platforms (Discord, Telegram, etc.) unresponsive for 10-15+ minutes per cycle.Related Issue
No existing issue — discovered in production when a gateway restart triggered a cascade of ~30 expired session flushes that repeated on every subsequent restart.
Type of Change
Changes Made
gateway/run.py: After a successful proactive memory flush, remove the expired session entry from the session store on disk (_entries.pop()+_save()). This ensures flushed sessions are never re-discovered on restart.tests/gateway/test_async_memory_flush.py: Two regression tests — one verifying entries are removed from disk after flush, one demonstrating the original bug (in-memory guard lost on restart).How to Test
sessions.jsonsource .venv/bin/activate python -m pytest -q tests/gateway/test_async_memory_flush.pyChecklist
Code
pytest tests/ -qand all tests pass