mcp: closing a session right after a cancelled call drops notifications/cancelled, so the server handler keeps running
维护者通常 1 天内回复
@vshalpnjabi 已经在做这个了。
开始于 2026年11月6日。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- go
调研方向
Start by tracing call() and ClientSession.Close through the JSON-RPC connection shutdown path, then inspect how outgoing notifications are tracked and drained. The issue’s repro can confirm the failure and provide a starting test. Done means closing immediately after a cancelled call still delivers notifications/cancelled, cancels the server handler, and avoids waiting for the 5-second timeout.
由索引模型根据 Issue 内容生成。
描述
Summary
Since v1.8.0 (#1151), call() retires a cancelled call and sends notifications/cancelled from a goroutine. If the client closes the session right after the call returns (a graceful shutdown that cancels in-flight tool calls and then closes, for example), the notification is dropped. On a stateful streamable HTTP server the tool handler is then never cancelled, and ClientSession.Close blocks for closeDeleteTimeout (5s).
MCPGODEBUG=blockingcancelnotify=1 restores the v1.7.0 ordering, but it is scheduled for removal in v1.9.0.
Repro (v1.8.0, also matches main)
package repro
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestCloseAfterCanceledCallCancelsTheServerHandler(t *testing.T) {
entered := make(chan struct{}, 1)
canceled := make(chan struct{}, 1)
release := make(chan struct{})
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "1"}, nil)
server.AddTool(&mcp.Tool{Name: "wait", InputSchema: map[string]any{"type": "object"}},
func(ctx context.Context, _ *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
entered <- struct{}{}
select {
case <-ctx.Done():
canceled <- struct{}{}
return nil, ctx.Err()
case <-release:
return &mcp.CallToolResult{}, nil
}
})
httpServer := httptest.NewServer(mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server }, nil))
t.Cleanup(httpServer.Close)
t.Cleanup(func() { close(release) })
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "1"}, nil)
session, err := client.Connect(t.Context(), &mcp.StreamableClientTransport{Endpoint: httpServer.URL}, nil)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(t.Context())
go func() {
<-entered
cancel()
}()
if _, err := session.CallTool(ctx, &mcp.CallToolParams{Name: "wait"}); !errors.Is(err, context.Canceled) {
t.Fatalf("CallTool: got %v, want context.Canceled", err)
}
start := time.Now()
_ = session.Close()
closed := time.Since(start)
select {
case <-canceled:
t.Logf("handler canceled; Close took %v", closed.Round(time.Millisecond))
case <-time.After(2 * time.Second):
t.Fatalf("server handler never saw the cancellation; Close took %v", closed.Round(time.Millisecond))
}
}
Go 1.26.10, linux/amd64:
- Default: fails 20/20,
server handler never saw the cancellation; Close took 5s. MCPGODEBUG=blockingcancelnotify=1: passes 20/20,handler canceled; Close took 1ms.
Why
call()runsconn.Retire(call, ...), starts the notify goroutine and returns.ClientSession.Close→jsonrpc2.Connection.Closemarks the connection closing.- The goroutine's
Notifyfinds no outgoing calls and no incoming requests, soshuttingDown(ErrClientClosing)rejects it. The notification is never written. - The streamable client sends DELETE.
serveStatefulDELETE→ServerSession.Closecancels onlysubscriptions/listenhandlers, thenconn.Closewaits for in-flight requests, including the tool handler that is still blocked on its context. - The DELETE hangs until the client gives up after
closeDeleteTimeoutand cancels its connection. The handler keeps running.
So closing the connection does not signal the cancellation to a stateful streamable server, which is the assumption in #1241.
Suggestion
Keep the caller's return immediate (the point of #1150), but register the pending notification synchronously before call() returns, e.g. bump outgoingNotifications (or an equivalent "pending cancel" counter) before spawning the goroutine. Then Close allows the write and its drain waits for it, bounded by notifyCancellationTimeout.
We hit this in a graceful-shutdown path that cancels tool calls, waits for them to return, then closes the session; we're running with blockingcancelnotify=1 until there is a fix.
- 主要语言
- Go
- 星标
- 5.3k
- 派生
- 607
- 平均合并
- 1 天 15 分钟
- 30 天内合并 PR
- 76
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/go-sdk 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/go-sdk#1512 · 1 条评论 ·
维护者通常 1 天内回复
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensions可能重新可做 关联的 PR 已关闭且未合并。 未关闭needs investigation
难度 2/5 1-3 小时 新手友好度 72/100
modelcontextprotocol/go-sdk#745 · 13 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 62/100
modelcontextprotocol/go-sdk#1506 · 2 条评论 ·
维护者通常 1 天内回复
-
mcp: list_changed can only be broadcast; a server cannot notify one session (2026-07-28 subscriptions/listen)可能已有人在做 @nahapetyan-serob 于 2 天前认领。 未关闭
难度 4/5 1-2 天 新手友好度 32/100
modelcontextprotocol/go-sdk#1480 · 1 条评论 · 已指派 2 人 ·
维护者通常 1 天内回复
-
mcp: Client.Connect falls back to initialize after a 401 on server/discover, running the authorization flow twice可能已有人在做 @kerthans 于 18 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
modelcontextprotocol/go-sdk#1423 · 1 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/go-sdk 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
nebari-dev/nebi#652 · 1 条评论 ·
维护者通常 1 天内回复
-
govulncheck reports 7 reachable vulnerabilities in dependencies可能已有人在做 @dwisiswant0 今天认领。 未关闭Type: Bug
难度 2/5 1-3 小时 新手友好度 76/100
projectdiscovery/rawhttp#558 ·
-
govulncheck reports vulnerable golang.org/x/net and goldmark in called code可能已有人在做 @dwisiswant0 今天认领。 未关闭Type: Bug
难度 1/5 1 小时以内 新手友好度 72/100
projectdiscovery/cdncheck#549 ·
维护者通常 6 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
Type: Bug
难度 2/5 1-3 小时 新手友好度 76/100
projectdiscovery/pdtm#467 ·