-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(dispatcher): advance mint counter past caller-supplied IDs (#3126) #3237
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
Open
elang2
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
elang2:fix/dispatcher-id-collision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -474,11 +474,45 @@ async def parked() -> None: | |||||||||||
|
|
||||||||||||
| tg.start_soon(parked) | ||||||||||||
| await entered.wait() | ||||||||||||
| # The counter mints 1 and 2, then skips the occupied 3 to 4. | ||||||||||||
| # The counter is advanced to 3 when "3" is supplied, so | ||||||||||||
| # subsequent mints produce 4, 5, 6 — never revisiting 3. | ||||||||||||
| for _ in range(3): | ||||||||||||
| await client.send_raw_request("plain", None) | ||||||||||||
| release.set() | ||||||||||||
| assert [request_id for request_id in seen_ids if request_id != "3"] == [1, 2, 4] | ||||||||||||
| assert [request_id for request_id in seen_ids if request_id != "3"] == [4, 5, 6] | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.anyio | ||||||||||||
| async def test_minted_id_skips_injected_consecutive_in_flight_ids(): | ||||||||||||
| """Defensive guard: if _in_flight_ids contains the next sequential | ||||||||||||
| candidates, the while-loop advances past all of them.""" | ||||||||||||
|
|
||||||||||||
| async def noop( | ||||||||||||
| ctx: DispatchContext[TransportContext], method: str, params: Mapping[str, Any] | None | ||||||||||||
| ) -> dict[str, Any]: | ||||||||||||
| return {} | ||||||||||||
|
|
||||||||||||
| client, server, close = direct_pair() | ||||||||||||
| assert isinstance(server, DirectDispatcher) | ||||||||||||
| try: | ||||||||||||
| async with anyio.create_task_group() as tg: | ||||||||||||
| await tg.start(client.run, noop, noop) | ||||||||||||
| await tg.start(server.run, noop, noop) | ||||||||||||
| # send_raw_request on client dispatches on the server's peer | ||||||||||||
| # (_dispatch_request runs on server). Inject synthetic in-flight | ||||||||||||
| # keys into the SERVER so the mint loop must skip them. | ||||||||||||
| # _next_id is 0, so mint increments to 1, finds it occupied, | ||||||||||||
| # increments to 2, finds it occupied, increments to 3, finds it | ||||||||||||
| # occupied, and finally lands on 4. | ||||||||||||
| server._in_flight_ids.update({1, 2, 3}) | ||||||||||||
| result = await client.send_raw_request("ping", None) | ||||||||||||
| assert result == {} | ||||||||||||
|
Comment on lines
+508
to
+509
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: A broken request/response path can leave this awaited request pending indefinitely, causing the test worker to hang instead of producing a failure. Bound the request with the standard five-second Prompt for AI agents
Suggested change
|
||||||||||||
| # After completion the id is discarded from in_flight, but the | ||||||||||||
| # counter must have advanced to 4 (skipping 1, 2, 3). | ||||||||||||
| assert server._next_id == 4 | ||||||||||||
| tg.cancel_scope.cancel() | ||||||||||||
| finally: | ||||||||||||
| close() | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.anyio | ||||||||||||
|
|
@@ -512,6 +546,64 @@ async def first() -> None: | |||||||||||
| assert await client.send_raw_request("again", None, {"request_id": "7"}) == {} | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.anyio | ||||||||||||
| async def test_minted_ids_never_reuse_a_completed_caller_supplied_id(pair_factory: PairFactory): | ||||||||||||
| """Regression: after a caller-supplied integer id completes, the mint counter | ||||||||||||
| must have advanced past it so no future minted id collides. This is the bug | ||||||||||||
| from GH-3126: the counter could land on a previously-used supplied id because | ||||||||||||
| the guard only checked `_pending`/`_in_flight_ids` (cleared on completion).""" | ||||||||||||
| seen_ids: list[RequestId | None] = [] | ||||||||||||
|
|
||||||||||||
| async def track( | ||||||||||||
| ctx: DispatchContext[TransportContext], method: str, params: Mapping[str, Any] | None | ||||||||||||
| ) -> dict[str, Any]: | ||||||||||||
| seen_ids.append(ctx.request_id) | ||||||||||||
| return {} | ||||||||||||
|
|
||||||||||||
| async with running_pair(pair_factory, server_on_request=track) as (client, *_): | ||||||||||||
| with anyio.fail_after(5): | ||||||||||||
| # Send a request with a caller-supplied integer id. | ||||||||||||
| await client.send_raw_request("supplied", None, {"request_id": 5}) | ||||||||||||
| # Now send several auto-minted requests. None should reuse id 5. | ||||||||||||
| for _ in range(6): | ||||||||||||
| await client.send_raw_request("minted", None) | ||||||||||||
|
|
||||||||||||
| # The first id is the supplied 5; the rest are minted sequentially starting | ||||||||||||
| # above 5 (i.e. 6, 7, 8, 9, 10, 11). | ||||||||||||
| assert seen_ids[0] == 5 | ||||||||||||
| minted_ids = seen_ids[1:] | ||||||||||||
| assert 5 not in minted_ids | ||||||||||||
| # Verify they are unique and monotonically increasing integers > 5. | ||||||||||||
| assert all(isinstance(i, int) and i > 5 for i in minted_ids) | ||||||||||||
| assert len(minted_ids) == len(set(minted_ids)) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.anyio | ||||||||||||
| async def test_minted_ids_never_reuse_a_completed_numeric_string_id(pair_factory: PairFactory): | ||||||||||||
| """Same as above but with a numeric-string supplied id ("3"), which coerces to | ||||||||||||
| int 3 in the collision domain. Minted ids must skip past 3.""" | ||||||||||||
| seen_ids: list[RequestId | None] = [] | ||||||||||||
|
|
||||||||||||
| async def track( | ||||||||||||
| ctx: DispatchContext[TransportContext], method: str, params: Mapping[str, Any] | None | ||||||||||||
| ) -> dict[str, Any]: | ||||||||||||
| seen_ids.append(ctx.request_id) | ||||||||||||
| return {} | ||||||||||||
|
|
||||||||||||
| async with running_pair(pair_factory, server_on_request=track) as (client, *_): | ||||||||||||
| with anyio.fail_after(5): | ||||||||||||
| await client.send_raw_request("supplied", None, {"request_id": "3"}) | ||||||||||||
| for _ in range(4): | ||||||||||||
| await client.send_raw_request("minted", None) | ||||||||||||
|
|
||||||||||||
| assert seen_ids[0] == "3" | ||||||||||||
| minted_ids = seen_ids[1:] | ||||||||||||
| # 3 should never appear (even though "3" completed and left _pending/_in_flight). | ||||||||||||
| assert 3 not in minted_ids | ||||||||||||
| assert all(isinstance(i, int) and i > 3 for i in minted_ids) | ||||||||||||
| assert len(minted_ids) == len(set(minted_ids)) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.anyio | ||||||||||||
| async def test_notify_intercept_sees_every_notification_and_consumes_on_true(pair_factory: PairFactory): | ||||||||||||
| """The intercept sees every inbound notification; a frame it consumes never reaches `on_notify`, the rest do.""" | ||||||||||||
|
|
||||||||||||
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.
Uh oh!
There was an error while loading. Please reload this page.