fix(sessions): prevent RuntimeError in cleanup_expired_sessions under concurrency#60
Open
ry-ops wants to merge 1 commit intoPegaProx:mainfrom
Open
fix(sessions): prevent RuntimeError in cleanup_expired_sessions under concurrency#60ry-ops wants to merge 1 commit intoPegaProx:mainfrom
ry-ops wants to merge 1 commit intoPegaProx:mainfrom
Conversation
… concurrency
Under concurrent load a request handler thread may add or remove an
entry from `active_sessions` while `cleanup_expired_sessions` is
iterating over it, causing:
RuntimeError: dictionary changed size during iteration
Two additional hardening changes:
- Snapshot `active_sessions.items()` into a list before filtering so
the iteration is over an immutable copy
- Replace `del active_sessions[sid]` with `active_sessions.pop(sid, None)`
so a session already removed by another thread is silently skipped
instead of raising KeyError
- Use `.get('last_activity', 0)` as a safe fallback in the filter
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Under concurrent load a request handler thread can add or remove an entry from
active_sessionswhilecleanup_expired_sessionsis iterating over it, causing:Additionally, if a session was already deleted by another thread between the list comprehension and the
delcall, aKeyErrorwould be raised.Changes:
active_sessions.items()into alist()before filtering — iteration is over an immutable copydel active_sessions[sid]withactive_sessions.pop(sid, None)— silently skips sessions already removed by another thread.get('last_activity', 0)as a safe fallback in the expiry filterTest plan
RuntimeError: dictionary changed size during iterationappears in logs🤖 Generated with Claude Code