fix(mcp): preserve cache invalidation during refresh - #4807
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36e85b1dff
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if refresh_generation == self._tools_cache_generation: | ||
| self._tools_list = tools | ||
| self._cache_dirty = False |
There was a problem hiding this comment.
Keep tool-call validation aligned with refresh results
When invalidate_tools_cache() is called while a refresh is in flight and the server changed the required parameters of an existing tool, this branch leaves the old _tools_list in place while list_tools() returns the newly fetched schema. A subsequent call_tool() validates against that old list in _validate_required_parameters() and can reject a valid invocation for missing a parameter that the returned schema no longer requires, before the request reaches the server. Avoid validating against the dirty prior cache while preserving the pending invalidation.
AGENTS.md reference: AGENTS.md:L149-L149
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in cbbcd49b. Dirty tool caches are no longer used for local required-parameter validation, so a stale retained schema cannot reject calls while a newer invalidation is pending. The regression now covers the stale refresh result, dirty-cache call-through, the subsequent clean refresh, and restored validation from the new authoritative cache.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
sylvesterkaczmarek
left a comment
There was a problem hiding this comment.
The generation check handles invalidation during a refresh, but it does not prevent an older concurrent refresh from overwriting a newer one when both started at the same generation.
For Stdio/SSE servers _serialize_session_requests is false, so two callers can enter list_tools() while the cache is dirty. Both capture the same _tools_cache_generation. If refresh A starts first, refresh B starts second and returns a newer schema first, B publishes it and clears _cache_dirty; when the slower A later completes, its generation still matches and it publishes its older result over B.
This leaves the cache authoritative but stale, including the local required-parameter validation this PR is trying to protect.
Could refresh publication also carry a monotonically increasing refresh sequence (publish only the latest-started refresh), or coalesce/serialize cache refreshes? A deterministic test can start A and B after one invalidation, let B return the new schema first, then release A with the old schema and assert the cache remains on B's result.
This pull request fixes an MCP tools-cache race where an in-flight
list_tools()refresh could clear a newer invalidation or leave a retainedstale schema authoritative for
call_tool()validation.Each refresh captures the current cache generation and only publishes its
result when that generation remains current. While the cache is dirty, local
required-parameter validation is skipped until a later refresh establishes a
clean authoritative snapshot.
A deterministic concurrency regression covers the stale refresh result,
retained cache snapshot, dirty-cache call-through, subsequent refresh, and
restored clean-cache validation.
Validation:
tests/mcp/test_caching.pymake formatmake lintmake typecheckmake testsgit diff --check