Server: a notifications/cancelled that arrives before the request's handler task is registered is dropped, so the request runs to completion
メンテナーはふだん 7 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start with Sources/MCP/Server/Server.swift at the receive-loop dispatch, handleRequest, and removePendingRequest(id:) locations described in the issue. Review PR #270 and its “Cancellation received before handler registration prevents handler start” barrier test; done means the reproduction no longer starts or responds to a request cancelled immediately after dispatch.
索引モデルが issue の本文から書いたものです。
説明
Summary
Server registers a request's handler task for cancellation after it has already returned to the receive loop, so a notifications/cancelled that follows the request closely enough is processed before the registration exists. The cancellation is then logged as "unknown or completed" and ignored, and the request runs to completion and sends its response — for a request the server has, in fact, received and is about to start.
Mechanics (0.12.1, Sources/MCP/Server/Server.swift)
- The receive loop decodes a request and hands it off without waiting (
:235):
The loop immediately continues to the next stream element.} else if let request = try? decoder.decode(AnyRequest.self, from: data) { // Handle request in a separate task to avoid blocking the receive loop Task { _ = try? await self.handleRequest(request, sendResponse: true) } } - Inside that task,
handleRequestfirst awaits the transport's HTTP context lookup (:784) and only then registers the task (:816):pendingRequestTasks[request.id] = handlerTask - Meanwhile the loop has already decoded the next element. If it is
notifications/cancelledfor that id, the built-in handler runsremovePendingRequest(id:)(:1001), finds nothing, and takes the "unknown or completed" branch. Nothing cancels the handler task once it is registered a moment later.
The window is the gap between the detached Task at :235 and the assignment at :816, which includes an await. It is a real ordering hazard, not a theoretical one: any client that sends a request and cancels it immediately (a user abort, a timeout that fires during send) can hit it, and it is deterministic to reproduce with a barrier that holds the context lookup until the cancellation has been consumed.
Expected behavior
The cancellation spec says the receiver SHOULD stop processing the cancelled request and not send a response, and MAY ignore a cancellation for an unknown request. This request is not unknown — the server decoded it and dispatched it — so ignoring the cancellation is a missed SHOULD caused by an implementation race, not a legitimate "unknown request" case. A cancellation that arrives after dispatch and before registration should still cancel the request, or prevent the handler from starting.
Reproduction
On any transport (stdio, or two back-to-back stateless HTTP POSTs), send:
{"jsonrpc":"2.0","id":"r1","method":"tools/call","params":{"name":"slow"}}- immediately:
{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":"r1"}}
Observe: the slow handler runs and a response for r1 is sent. With a transport whose httpRequestContext(for:) is held until after the cancellation is consumed, the outcome is deterministic.
Existing fix
PR #270 (Fix early request cancellation race) addresses exactly this: it tracks the dispatch task before handler registration so an immediate cancellation cannot fall through the gap, processes the built-in cancellation handler before other notification handlers can suspend, and adds a deterministic barrier test (Cancellation received before handler registration prevents handler start). That PR currently references no issue; this one is filed so the defect has a tracking entry independent of any particular fix. #270 also carries two adjacent hardening changes — suppressing a late response from a handler that swallows CancellationError, and rejecting duplicate outstanding request ids in Server — which are worth noting when reviewing it, since the second is a behavior change beyond this bug.
Related
- #255 — a cancelled request's HTTP POST hangs in
StatelessHTTPServerTransport(the transport side of cancellation; fix in #260) - #254 / #265 — colliding JSON-RPC ids across concurrent stateless clients (fix in #264); with exchange-scoped ids in place,
Server's duplicate-id rejection from #270 only ever triggers on single-client transports
- 主要言語
- Swift
- スター
- 1.5k
- フォーク
- 252
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/swift-sdk のほかの issue
-
NetworkTransport leaks the underlying socket (CLOSE_WAIT) when reconnection is disabled and the receive loop terminates対応中かも @skirrellyjones が 39 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
modelcontextprotocol/swift-sdk#282 ·
メンテナーはふだん 7 日以内に返信
-
Value decoding changes data-URL-looking JSON strings into Value.data対応中かも @bitbemol が 50 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
modelcontextprotocol/swift-sdk#277 ·
メンテナーはふだん 7 日以内に返信
-
enhancement
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
modelcontextprotocol/swift-sdk#274 ·
メンテナーはふだん 7 日以内に返信
-
`HTTPClientTransport` fails to build on Windows: `#if !os(Linux)` guards no longer match the `EventSource` platform condition再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
modelcontextprotocol/swift-sdk#261 ·
メンテナーはふだん 7 日以内に返信
-
Value: add numberValue (Double?) that coerces .int and .double対応中かも @gsdali が 161 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
modelcontextprotocol/swift-sdk#225 ·
メンテナーはふだん 7 日以内に返信
modelcontextprotocol/swift-sdk の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
Emanuele-web04/remodex#203 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
test(BrainBar): inject synthetic observabilityCadence in BackupsHealth/one-page fixtures (DCI-1)オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
EtanHey/brainlayer#1367 ·
メンテナーはふだん 1 日以内に返信
-
cmux-next: new workspaces show no name instead of workspace-N対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンarea: workspaces
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
manaflow-ai/cmux#21357 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 4 日以内に返信