Skip to content

Use mutex-protected session_exists? in handle_regular_request#258

Open
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
koic:use_mutex_protected_session_exists_predicate
Open

Use mutex-protected session_exists? in handle_regular_request#258
koic wants to merge 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.
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