Fix concurrent map panic in MCP server caches#16042
Merged
pelikhan merged 3 commits intogithub:mainfrom Feb 16, 2026
Merged
Conversation
The permissionCache map and repoCache pointer are accessed concurrently
from HTTP handler goroutines without synchronization. In Go, concurrent
read+write on a map causes a fatal runtime panic ("concurrent map read
and map write").
Add a sync.RWMutex to protect both caches. Read operations use RLock
for concurrency, write operations use Lock for exclusive access.
pelikhan
requested changes
Feb 16, 2026
Contributor
pelikhan
left a comment
There was a problem hiding this comment.
Create a class to hide the details and apply locks consistentely.
Replace bare global mutex + maps with an mcpCacheStore struct that encapsulates all locking internally. Callers now use GetPermission, SetPermission, GetRepo, and SetRepo methods instead of manual lock/unlock sequences, making it impossible to access caches without proper synchronization.
pelikhan
requested changes
Feb 16, 2026
pkg/cli/mcp_server.go
Outdated
| repoCache *repositoryCache | ||
| repoCacheTTL = 1 * time.Hour | ||
| ) | ||
| func newMCPCacheStore() *mcpCacheStore { |
pelikhan
approved these changes
Feb 16, 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.
Summary
The MCP server's
permissionCache(map) andrepoCache(pointer) are package-level variables accessed concurrently from HTTP handler goroutines without any synchronization. In Go, concurrent read+write on a plain map causes a fatal runtime panic (concurrent map read and map write), crashing the server.This is triggered when the MCP server runs in HTTP transport mode (
--port) and receives multiple simultaneous requests that hitqueryActorRoleorgetRepository.Fix: Add a
sync.RWMutex(cacheMu) to protect both caches. Read operations useRLockfor concurrency, write operations useLockfor exclusive access.Affected code paths:
getRepository()— reads/writesrepoCachepointer (lines 65-101)queryActorRole()— reads/writes/deletes frompermissionCachemap (lines 118-150)Test plan
TestQueryActorRole_ConcurrentCacheAccess— 20 goroutines × 100 iterations exercising concurrent cache read/write/deleteTestGetRepository_ConcurrentCacheAccess— same pattern for the repo cachego test -race(no data races detected)go test -race