Skip to content

Commit dff933e

Browse files
committed
update
1 parent 6c97a1b commit dff933e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

client/http_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ func TestHTTPClient(t *testing.T) {
1313
hooks := &server.Hooks{}
1414
hooks.AddAfterCallTool(func(ctx context.Context, id any, message *mcp.CallToolRequest, result *mcp.CallToolResult) {
1515
clientSession := server.ClientSessionFromContext(ctx)
16-
// return until all the notifications are handled
16+
// wait until all the notifications are handled
1717
for len(clientSession.NotificationChannel()) > 0 {
1818
}
19-
time.Sleep(time.Millisecond * 200)
19+
time.Sleep(time.Millisecond * 50)
2020
})
2121

2222
// Create MCP server with capabilities
@@ -58,11 +58,10 @@ func TestHTTPClient(t *testing.T) {
5858
},
5959
)
6060

61-
// Initialize
6261
testServer := server.NewTestStreamableHTTPServer(mcpServer)
6362
defer testServer.Close()
6463

65-
t.Run("Can create client", func(t *testing.T) {
64+
t.Run("Can receive notification from server", func(t *testing.T) {
6665
client, err := NewStreamableHttpClient(testServer.URL)
6766
if err != nil {
6867
t.Fatalf("create client failed %v", err)

server/streamable_http.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ func (s *StreamableHTTPServer) handlePost(w http.ResponseWriter, r *http.Request
246246
mu := sync.Mutex{}
247247
upgradedHeader := false
248248
done := make(chan struct{})
249-
defer close(done)
250249

251250
go func() {
252251
for {
@@ -255,6 +254,12 @@ func (s *StreamableHTTPServer) handlePost(w http.ResponseWriter, r *http.Request
255254
func() {
256255
mu.Lock()
257256
defer mu.Unlock()
257+
// if the done chan is closed, as the request is terminated, just return
258+
select {
259+
case <-done:
260+
return
261+
default:
262+
}
258263
defer func() {
259264
flusher, ok := w.(http.Flusher)
260265
if ok {
@@ -295,6 +300,8 @@ func (s *StreamableHTTPServer) handlePost(w http.ResponseWriter, r *http.Request
295300
// Write response
296301
mu.Lock()
297302
defer mu.Unlock()
303+
// close the done chan before unlock
304+
defer close(done)
298305
if ctx.Err() != nil {
299306
return
300307
}

0 commit comments

Comments
 (0)