Fix falsy-zero requestId bugs in debounce guard and cancellation handling#2504
Open
dumbCodesOnly wants to merge 2 commits into
Open
Fix falsy-zero requestId bugs in debounce guard and cancellation handling#2504dumbCodesOnly wants to merge 2 commits into
dumbCodesOnly wants to merge 2 commits into
Conversation
- _notificationViaCodec: canDebounce used `!options?.relatedRequestId`, which treats relatedRequestId === 0 the same as undefined. A notification tied to request 0 could be incorrectly coalesced with unrelated notifications of the same method. - _oncancel: `if (!notification.params.requestId)` dropped notifications/cancelled for requestId 0, so the abort controller for request 0 never fired. Request IDs start at 0 (_requestMessageId = 0), so both are reachable in practice, not just theoretical edge cases. Fixes modelcontextprotocol#2117, modelcontextprotocol#2283
- relatedRequestId: 0 must never be debounce-coalesced (issue modelcontextprotocol#2117) - notifications/cancelled with requestId: 0 must abort the correct request's controller (issue modelcontextprotocol#2283)
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Two separate call sites in
packages/core-internal/src/shared/protocol.tstreat a validrequestId/relatedRequestIdof0as absent, because JS treats0as falsy. Request IDs start at0(_requestMessageId = 0), so this is a reachable bug in normal operation, not a theoretical edge case.Site 1 — #2117: debounce guard wrongly skips zero-id
In
_notificationViaCodec:!options?.relatedRequestIdistrueboth whenrelatedRequestIdisundefinedand when it's0. A notification tied to request0could be incorrectly treated as debounce-eligible and silently coalesced with unrelated notifications of the same method — when it should never be debounced.Fixed to
options?.relatedRequestId === undefined.Site 2 — #2283: cancellation notification dropped for request id 0
In
_oncancel:A
notifications/cancelledmessage for request id0was silently ignored, so the abort controller for that request never fired.Fixed to explicitly check for
undefined/null.Testing
Added regression tests in
packages/core-internal/test/shared/protocol.test.ts:relatedRequestId: 0must never be debounce-coalescednotifications/cancelledwithrequestId: 0must abort the correct request's controllerBoth new tests fail against the pre-fix code and pass with the fix applied.
Fixes #2117
Fixes #2283