Skip to content

Comments

fix(sessions): prevent RuntimeError in cleanup_expired_sessions under concurrency#60

Open
ry-ops wants to merge 1 commit intoPegaProx:mainfrom
ry-ops:fix/session-cleanup-race-condition
Open

fix(sessions): prevent RuntimeError in cleanup_expired_sessions under concurrency#60
ry-ops wants to merge 1 commit intoPegaProx:mainfrom
ry-ops:fix/session-cleanup-race-condition

Conversation

@ry-ops
Copy link
Contributor

@ry-ops ry-ops commented Feb 21, 2026

Summary

Under concurrent load a request handler thread can add or remove an entry from active_sessions while cleanup_expired_sessions is iterating over it, causing:

RuntimeError: dictionary changed size during iteration

Additionally, if a session was already deleted by another thread between the list comprehension and the del call, a KeyError would be raised.

Changes:

  • Snapshot active_sessions.items() into a list() before filtering — iteration is over an immutable copy
  • Replace del active_sessions[sid] with active_sessions.pop(sid, None) — silently skips sessions already removed by another thread
  • Use .get('last_activity', 0) as a safe fallback in the expiry filter

Test plan

  • Verify session cleanup still removes expired sessions correctly
  • Under high concurrent load, no RuntimeError: dictionary changed size during iteration appears in logs

🤖 Generated with Claude Code

… 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>
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.

1 participant