Skip to content

Use mutex-protected session_exists? in handle_regular_request#258

Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:use_mutex_protected_session_exists_predicate
Mar 18, 2026
Merged

Use mutex-protected session_exists? in handle_regular_request#258
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:use_mutex_protected_session_exists_predicate

Conversation

@koic
Copy link
Member

@koic koic commented Mar 16, 2026

Motivation and Context

handle_regular_request was checking @sessions.key?(session_id) directly without holding @mutex, while concurrent threads could modify @sessions via cleanup_session or handle_delete. This created a TOCTOU race where the check could pass but the session could be deleted before subsequent use.

The class already provides a mutex-protected session_exists? helper, and handle_get already uses it. This change makes handle_regular_request consistent with handle_get.

How Has This Been Tested?

Added a test that verifies handle_regular_request delegates to the mutex-protected session_exists? helper instead of accessing @sessions directly.

All existing tests pass.

Breaking Change

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

## Motivation and Context

`handle_regular_request` was checking `@sessions.key?(session_id)` directly
without holding `@mutex`, while concurrent threads could modify `@sessions`
via `cleanup_session` or `handle_delete`. This created a TOCTOU race where
the check could pass but the session could be deleted before subsequent use.

The class already provides a mutex-protected `session_exists?` helper, and
`handle_get` already uses it. This change makes `handle_regular_request`
consistent with `handle_get`.

## How Has This Been Tested?

Added a test that verifies `handle_regular_request` delegates to the
mutex-protected `session_exists?` helper instead of accessing
`@sessions` directly.

All existing tests pass.

## Breaking Change

None.
@koic koic force-pushed the use_mutex_protected_session_exists_predicate branch from 526d643 to be1bbc2 Compare March 17, 2026 19:31
@koic koic merged commit f4aab5c into modelcontextprotocol:main Mar 18, 2026
11 checks passed
@koic koic deleted the use_mutex_protected_session_exists_predicate branch March 18, 2026 02:53
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.

2 participants