-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(server): settle a cancelled serveStdio probe so a pipelined initialize fallback cannot wedge the connection #2317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+89
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing issue (not introduced by this PR):
Protocol._oncancelinpackages/core/src/shared/protocol.ts:512-515guards withif (!notification.params.requestId) return, so anotifications/cancellednaming request id0— the very first id an SDK client uses — is silently ignored and the in-flight handler is never aborted. The new channel-level settle here correctly checkscancelledId !== undefined; a follow-up should change the Protocol guard torequestId === undefinedso the two layers agree on whether id 0 is cancellable.Extended reasoning...
What the bug is.
Protocol._oncancel(packages/core/src/shared/protocol.ts:512-519) starts withif (!notification.params.requestId) { return; }.RequestIdisstring | number, and0(as well as'') is falsy, so a cancellation that names request id 0 is treated as if the field were absent: the method returns early, the matchingAbortControlleris never looked up, and the in-flight request handler keeps running to completion.Why id 0 is realistic — in fact the most likely id to be cancelled.
Protocolinitializes its request counter at 0 (private _requestMessageId = 0, protocol.ts:418) and assigns ids with post-increment (const messageId = this._requestMessageId++, protocol.ts:1132). So an SDK-built client's very first request on a connection — e.g. the openingserver/discoverprobe that this PR's headline scenario is about — carries id 0, and the client's own cancellation path (protocol.ts:1165) sendsnotifications/cancelledwith that same numeric id.Concrete walkthrough. 1) An SDK 2026 client opens a stdio connection and sends
server/discoverwith id 0 (its first request). 2) It decides to abandon the probe and pipelinesnotifications/cancelledwithrequestId: 0. 3) On the server, the entry delivers both messages to the probe instance; the new channel code inserveStdio.ts:187-198correctly settles pending id 0 because it checkscancelledId !== undefined. 4) ButProtocol._oncancelevaluates!0 === trueand returns — the discover handler's abort signal never fires, the handler runs to completion, and its response is still written. The cancellation is silently ignored for that one id.Why this PR doesn't prevent or recreate it. The PR only touches the channel layer in
serveStdio.ts;protocol.tsis unchanged. Importantly, this does NOT recreate the wedge the PR fixes: because the handler for id 0 is never aborted, the discover answer still reaches the wire,send()settles the pending id, and the discard wait resolves — the connection's pump is never blocked. The visible impact is limited to wasted handler work and a response the cancelling client must ignore per spec. However, after this PR the channel layer (!== undefined) and the Protocol layer (falsy check) disagree on whether id 0 is a valid cancellation target, which is exactly the off-by-falsy class the new code in this diff was careful to avoid.How to fix. In a follow-up to core (out of scope for this PR), change the guard to
if (notification.params.requestId === undefined) { return; }— or drop it entirely, since the schema requires the field. String ids of''would be handled correctly by the same change.All four verifiers independently confirmed the falsy guard, the id-0 starting counter, and that the impact is non-blocking; there were no refutations.