Skip to content

fix(mcp): honor caller deadlines during gateway initialization - #939

Open
rudycelekli wants to merge 1 commit into
Tencent:mainfrom
rudycelekli:fix/mcp-cancel-initialization
Open

rudycelekli wants to merge 1 commit into
Tencent:mainfrom
rudycelekli:fix/mcp-cancel-initialization

Conversation

@rudycelekli

Copy link
Copy Markdown

What changed

CallToolContext and ListToolsContext applied cancellation only to the actual tool request. The initialize handshake could exceed the API caller deadline, including while a concurrent caller held the handshake lock. Bind both handshake requests to the caller context, allow cancellation while queued, and leave canceled initialization retryable.

Scope

  • Platform core / API / runner

Validation

A synthetic HTTP endpoint stalls the first initialization. Main exceeds the caller deadline, while the fixed client returns the deadline and a fresh caller successfully initializes. The mcpclient package tests and vet pass.

Full platform build and public browser checks were not run for this branch. All fixtures are synthetic.

@Yanami-2K Yanami-2K left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

核对了 internal/mcpclient/client.go:初始化不再用全程持有的 c.mu 串行,改成容量为 1 的 initGate 通道,排队时可以响应 ctx.Done();initialize 请求改用 NewRequestWithContext,调用方取消时返回 ctx.Err(),并且不把 c.init 置为 true,后续调用方可以重新初始化。测试断言了 DeadlineExceeded、重试成功,以及一共发出两次 initialize。非阻塞建议:“排队等 initGate 时超时”这条分支没有单独的测试;ensureInit 的 diff 后半段被截断,我没能确认通知那段里的 c.nextID++ 是否在 c.mu 保护下。它和 #935 改的是同一个结构体和 ensureInit,先合入哪个,另一个就需要 rebase。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants