Skip to content

Commit d4c4107

Browse files
committed
fix: hold mutex for instructions read in MCP Toolset.Instructions()
Assisted-By: docker-agent
1 parent b4894c8 commit d4c4107

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

pkg/tools/mcp/mcp.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,8 @@ func (ts *Toolset) tryRestart(ctx context.Context) bool {
339339

340340
func (ts *Toolset) Instructions() string {
341341
ts.mu.Lock()
342-
started := ts.started
343-
ts.mu.Unlock()
344-
if !started {
342+
defer ts.mu.Unlock()
343+
if !ts.started {
345344
// TODO: this should never happen...
346345
return ""
347346
}

pkg/tools/mcp/mcp_race_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package mcp
2+
3+
import (
4+
"sync"
5+
"testing"
6+
)
7+
8+
func TestInstructions_Concurrent(t *testing.T) {
9+
t.Parallel()
10+
11+
ts := &Toolset{
12+
started: true,
13+
instructions: "initial",
14+
}
15+
16+
var wg sync.WaitGroup
17+
for range 100 {
18+
wg.Add(2)
19+
go func() {
20+
defer wg.Done()
21+
// Simulate what doStart does (always called under ts.mu)
22+
ts.mu.Lock()
23+
ts.instructions = "updated"
24+
ts.mu.Unlock()
25+
}()
26+
go func() {
27+
defer wg.Done()
28+
_ = ts.Instructions()
29+
}()
30+
}
31+
wg.Wait()
32+
}

0 commit comments

Comments
 (0)