Clean up session ownership keys in Redis#47
Open
ennsharma wants to merge 1 commit into
Open
Conversation
session:${sessionId}:owner keys were written without a TTL and never
deleted, so they accumulated forever (modelcontextprotocol#21).
- Delete the ownership key in ServerRedisTransport.close(), which runs
on client DELETE, SHUTDOWN control messages, and inactivity timeout
- Set a 30-minute safety-net TTL on the key, refreshed on each
authorized request, so keys orphaned by a crashed process still
expire
Fixes modelcontextprotocol#21
Co-Authored-By: Claude Fable 5 <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.
Fixes #21.
session:${sessionId}:ownerkeys were written without a TTL and never deleted when the transport was cleaned up, so they accumulated in Redis indefinitely.Two complementary changes:
ServerRedisTransport.close()now deletes the session's ownership key.close()runs on every session-termination path — clientDELETE(onsessionclosed→shutdownSession),SHUTDOWNcontrol messages, and the 5-minute inactivity timeout — so a finished session always removes its key.setSessionOwnernow sets a 30-minuteEXon the key, andvalidateSessionOwnershiprefreshes it on each successful (authorized) check. Live sessions therefore never expire mid-flight — every request slides the TTL — while keys orphaned by a crashed process (whereclose()never ran) still get reaped by Redis.The TTL refresh happens only on successful validation, so an attacker probing someone else's session id can't keep a foreign key alive.
Added unit tests covering: TTL set on write, TTL refresh on authorized validation only, key removal on transport close, and key removal via SHUTDOWN control message.
npm run build,npx eslinton the touched files, and the full jest suite (83 tests, 6 suites) all pass.🤖 Generated with Claude Code