modelcontextprotocol / modelcontextprotocol/go-sdk

Potential race condition in (*mcp.ClientSession).Close()

Open
#855 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

needs investigation P2
Dominant language
Go
Stars
5.1k
Forks
543
Avg merge
1d 17h
Merged PRs (30d)
37

Description

We've experienced an increasing number of test flakes in the tests for our MCP server. There is an error when closing the connection at the end of the tests: "server is closing: EOF". We think there might be a bug in mcp causing a race between the active part of Close (which breaks the connection and terminates the child process) and the waiting part of close (which seems to observe an error from the terminated child without realizing that close itself was responsible). Alan began tracing the issue here: https://github.com/golang/go/issues/77336#issuecomment-4090756198

To Reproduce
The test is here: https://github.com/golang/tools/blob/36360cdf844bb2553617bdc496c8d972c7229e7d/gopls/internal/cmd/mcp_test.go#L109
Server code is here: https://github.com/golang/tools/blob/master/gopls/internal/cmd/mcp.go
Try to reproduce the failure locally by using -count=100:

go test -run ^TestMCPCommandLogging$ -count=100 golang.org/x/tools/gopls/internal/cmd
=== RUN   TestMCPCommandLogging/trace.log
    gopls: server is closing: EOF
    mcp_test.go:156: closing MCP connection: exit status 2

Expected behavior
Client connection closes without error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with gopls/internal/cmd/mcp_test.go around TestMCPCommandLogging and gopls/internal/cmd/mcp.go, then run go test -run ^TestMCPCommandLogging$ -count=100 golang.org/x/tools/gopls/internal/cmd to reproduce the flake. Trace the MCP client close and child-process termination interaction; done means the client connection closes without error across repeated runs.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.