fix(gateway): cap memory flush retries at 3 to prevent infinite loop#5224
Closed
nibzard wants to merge 1 commit into
Closed
fix(gateway): cap memory flush retries at 3 to prevent infinite loop#5224nibzard wants to merge 1 commit into
nibzard wants to merge 1 commit into
Conversation
The _session_expiry_watcher retried failed memory flushes forever because exceptions were caught at debug level without setting memory_flushed=True. Expired sessions with transient failures (rate limits, network errors) would retry every 5 minutes indefinitely, burning API quota and blocking gateway message processing via 429 rate limit cascades. Observed case: a March 19 session retried 28+ times over ~17 days, causing repeated 429 errors that made Telegram unresponsive. Add a per-session failure counter (_flush_failures) that gives up after 3 consecutive attempts and marks the session as flushed to break the loop.
Contributor
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.
Problem
The
_session_expiry_watcheringateway/run.pyretries failed memory flushes forever. When_async_flush_memoriesthrows (rate limit, network error), the exception is caught atdebuglevel without settingmemory_flushed = True, so the same expired session retries every 5 minutes indefinitely.This burns API quota and triggers 429 rate limit cascades that block all gateway message processing (Telegram, Discord, etc.), making the bot appear unresponsive.
Observed case: A March 19 session retried 28+ times over ~17 days, causing repeated 429 errors that made Telegram completely unresponsive.
Fix
Add a per-session failure counter (
_flush_failures) in the watcher loop. After 3 consecutive failures for the same session:memory_flushed = Trueand persist to diskThis breaks the infinite retry loop while still allowing transient failures to recover (up to 3 attempts at 5-min intervals = ~15 min grace period).
Testing
test_flush_memory_stale_guard.py)test_session_hygiene.py)