Skip to content

[Server] Answer 404 for a session that a handler cleared or that holds corrupt data - #682

Merged
chr-hertel merged 2 commits into
modelcontextprotocol:mainfrom
chr-hertel:session-empty-vs-unreadable
Nov 8, 2026
Merged

chr-hertel merged 2 commits into
modelcontextprotocol:mainfrom
chr-hertel:session-empty-vs-unreadable

Conversation

@chr-hertel

Copy link
Copy Markdown
Member

Follow-up to #627

After #627, a session that exists but holds no attributes is treated like a failed read and answered with 503. So when a handler calls $session->clear(), or the stored data is corrupt, the client keeps retrying until the TTL is over instead of opening a new session. Now only a session the store fails to return gets the 503, those two get a 404.

Protocol can't tell these apart through all(), hence the new SessionInterface::isUnreadable().

[BC Break] A custom SessionInterface needs to implement it - documented in the CHANGELOG.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Readable empty session files and sessions cleared or hydrated before their first read are incorrectly classified as unreadable, causing erroneous 503 responses.

2 open issues
What changed in this PR

This follow-up to #627 distinguishes empty or corrupt session data from failed store reads, allowing clients to restart invalid sessions rather than keep retrying.

Changes:

  • Add SessionInterface::isUnreadable() and use it to distinguish HTTP 404 from 503 responses.
  • Add regression coverage for cleared sessions, corrupt payloads, and failed reads.
  • Document the interface compatibility break.
File Description
tests/​Unit/​Server/​Transport/​SessionBindingTest.php Verify cleared and corrupt sessions return 404.
tests/​Unit/​Server/​Session/​SessionTest.php Cover readable, unreadable, and new session states.
src/​Server/​Session/​SessionInterface.php Declare the unreadable-session check.
src/​Server/​Session/​Session.php Implement unreadable-session detection.
src/​Server/​Protocol.php Use the new check when selecting failure responses.
CHANGELOG.md Document response behavior and the interface break.

Review effort: Balanced

Comment thread src/Server/Protocol.php Resolved
Comment thread src/Server/Session/Session.php Resolved
…ther principal

Treat a zero-byte session file as gone, keep isUnreadable() right after clear() or hydrate(), and log why a session is refused.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the changes separating session read failures from missing or cleared sessions.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@chr-hertel
chr-hertel merged commit a4bad64 into modelcontextprotocol:main Nov 8, 2026
31 checks passed
@chr-hertel
chr-hertel deleted the session-empty-vs-unreadable branch November 8, 2026 13:46
@chr-hertel chr-hertel added Server Issues & PRs related to the Server component breaking change Breaking the Backwards Compatibility Promise labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Breaking the Backwards Compatibility Promise Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants