From be1ddf5f801cc6eb3dda401e5ed3ba6956017f89 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sat, 9 Aug 2025 12:12:38 -0400 Subject: [PATCH] jsonrpc: paper over race in jsonrpc2 test In jsonprc2_test.go, binder.Bind starts a goroutine. That goroutine begins to run during jsonrpc2.bindConnection, and can race with the setting of conn.write in bindConnection. This PR adds a sleep, which is a poor way to deal with the race, but the least invasive change. Better ones include running the test function after Dial returns, or adding a Connection.Ready method to detect when initialization is complete. --- internal/jsonrpc2/jsonrpc2_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/jsonrpc2/jsonrpc2_test.go b/internal/jsonrpc2/jsonrpc2_test.go index 35f4e7f..16a5039 100644 --- a/internal/jsonrpc2/jsonrpc2_test.go +++ b/internal/jsonrpc2/jsonrpc2_test.go @@ -11,6 +11,7 @@ import ( "path" "reflect" "testing" + "time" "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" ) @@ -143,6 +144,8 @@ func testConnection(t *testing.T, framer jsonrpc2.Framer) { t.Run(test.Name(), func(t *testing.T) { client, err := jsonrpc2.Dial(ctx, listener.Dialer(), binder{framer, func(h *handler) { + // Sleep a little to a void a race with setting conn.writer in jsonrpc2.bindConnection. + time.Sleep(50 * time.Millisecond) defer h.conn.Close() test.Invoke(t, ctx, h) if call, ok := test.(*call); ok {