fix: add async lock to list_namespaces cache#52
Merged
Conversation
The shared _namespace_cache dict is accessed without a lock. When multiple MCP tool calls invoke list_namespaces() concurrently and the cache expires, duplicate K8s API calls are made and the cache can be written inconsistently. Added double-checked locking with asyncio.Lock(): fast path reads without lock, slow path (cache miss) acquires lock and re-checks before fetching. Closes #44
olegbet
approved these changes
Jun 22, 2026
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.
Problem
list_namespaces()accesses the shared_namespace_cachedict without any lock. Concurrent MCP tool calls during cache expiry cause duplicate K8s API calls and potential inconsistent cache state (TOCTOU race).Fix
Added
_namespace_cache_lock = asyncio.Lock()with double-checked locking:Testing
Verified with MCP Inspector:
list_namespacesreturns correct namespace list.Closes #44