Skip to content

Commit b4e65ba

Browse files
committed
fix: resolve race condition in TestTokenInfo with atomic testAuth
Replace unsafe global testAuth boolean with atomic.Bool to prevent data race between test setup/teardown and background HTTP operations in StreamableClientTransport.
1 parent 016015c commit b4e65ba

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

mcp/streamable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ func (c *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
12301230

12311231
// testAuth controls whether a fake Authorization header is added to outgoing requests.
12321232
// TODO: replace with a better mechanism when client-side auth is in place.
1233-
var testAuth = false
1233+
var testAuth atomic.Bool
12341234

12351235
func (c *streamableClientConn) setMCPHeaders(req *http.Request) {
12361236
c.mu.Lock()
@@ -1242,7 +1242,7 @@ func (c *streamableClientConn) setMCPHeaders(req *http.Request) {
12421242
if c.sessionID != "" {
12431243
req.Header.Set(sessionIDHeader, c.sessionID)
12441244
}
1245-
if testAuth {
1245+
if testAuth.Load() {
12461246
req.Header.Set("Authorization", "Bearer foo")
12471247
}
12481248
}

mcp/streamable_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,8 +1310,9 @@ func textContent(t *testing.T, res *CallToolResult) string {
13101310
}
13111311

13121312
func TestTokenInfo(t *testing.T) {
1313-
defer func(b bool) { testAuth = b }(testAuth)
1314-
testAuth = true
1313+
oldAuth := testAuth.Load()
1314+
defer testAuth.Store(oldAuth)
1315+
testAuth.Store(true)
13151316
ctx := context.Background()
13161317

13171318
// Create a server with a tool that returns TokenInfo.

0 commit comments

Comments
 (0)