Skip to content

Commit be1ddf5

Browse files
jbafindleyr
authored andcommitted
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.
1 parent cccc086 commit be1ddf5

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

internal/jsonrpc2/jsonrpc2_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path"
1212
"reflect"
1313
"testing"
14+
"time"
1415

1516
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
1617
)
@@ -143,6 +144,8 @@ func testConnection(t *testing.T, framer jsonrpc2.Framer) {
143144
t.Run(test.Name(), func(t *testing.T) {
144145
client, err := jsonrpc2.Dial(ctx,
145146
listener.Dialer(), binder{framer, func(h *handler) {
147+
// Sleep a little to a void a race with setting conn.writer in jsonrpc2.bindConnection.
148+
time.Sleep(50 * time.Millisecond)
146149
defer h.conn.Close()
147150
test.Invoke(t, ctx, h)
148151
if call, ok := test.(*call); ok {

0 commit comments

Comments
 (0)