fix(mcp): resolve TOCTOU race condition and resource leak#999
fix(mcp): resolve TOCTOU race condition and resource leak#999yinwm merged 1 commit intosipeed:mainfrom
Conversation
- Use atomic.Bool for closed flag to prevent TOCTOU race between CallTool and Close operations - Add double-check pattern in CallTool for thread-safe closed state - Use atomic Swap in Close to ensure no new calls can start after closed flag is set - Move MCP manager cleanup defer before initialization to handle partial initialization failures - Update tests to use atomic.Bool operations Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
+github.com/gdamore/tcell/v2 v2.13.8
+github.com/rivo/tview v0.42.0
These two dependencies have changed from indirect to direct dependencies, but there is no code referencing them in the PR. This could be a side effect of go mod tidy, or there may be other uncommitted local code that introduced these dependencies.
There was a problem hiding this comment.
it's go mod tidy effect
nikolasdehor
left a comment
There was a problem hiding this comment.
LGTM. This is a solid fix for a real TOCTOU race condition in the MCP manager.
What was broken: The original code checked m.closed (a plain bool) under a read-lock in CallTool, then released the lock, then called wg.Add(1). Meanwhile Close() could set closed=true and call wg.Wait() which would return immediately because Add(1) had not happened yet. This means Close() would start tearing down connections while a CallTool was about to execute.
What this fixes:
- atomic.Bool with Swap(true) in Close() -- atomic set + get-previous-value in one operation, no lock needed
- Double-check pattern in CallTool -- fast-path check before lock, TOCTOU-safe check after lock, with wg.Add(1) inside the critical section
- defer mcpManager.Close() moved before LoadFromMCPConfig() -- handles partial initialization failures
The resource leak fix (defer before init) is an important secondary fix. If LoadFromMCPConfig connects 3 servers successfully then fails on the 4th, those 3 connections would previously leak.
Clean, correct, well-documented.
…esource-leak fix(mcp): resolve TOCTOU race condition and resource leak
…esource-leak fix(mcp): resolve TOCTOU race condition and resource leak
Summary
atomic.Boolfor closed flag to prevent TOCTOU race betweenCallToolandCloseoperationsCallToolfor thread-safe closed state detectionSwapinCloseto ensure no new calls can start after closed flag is setdeferbefore initialization to handle partial initialization failuresatomic.BooloperationsProblem
TOCTOU Race Condition
Original code had a race window between checking
closedflag and adding toWaitGroup:Resource Leak
If
LoadFromMCPConfigpartially succeeded then failed, MCP connections were not cleaned up becausedefer mcpManager.Close()was only called on success path.Solution
Close()usesSwap(true)which atomically sets closed and returns previous valueTest plan
make checkpassesatomic.Bool🤖 Generated with Claude Code