Skip to content

test(mcp): stop the discovery walk test failing on a worker stall - #44000

Closed
pylaterreur wants to merge 1 commit into
BerriAI:mainfrom
pylaterreur:fix-mcp-discovery-walk-test-timeout
Closed

pylaterreur wants to merge 1 commit into
BerriAI:mainfrom
pylaterreur:fix-mcp-discovery-walk-test-timeout

Conversation

@pylaterreur

Copy link
Copy Markdown

TLDR

Problem this solves:

  • mcp-integration failed 5 of the last 20 runs on main
  • Every case of one MCP test gets a 0.2 s budget
  • A worker stall longer than that fails cases with no deadline

How it solves it:

  • Deadline cases get 2 s, every other case gets 30 s
  • Same split the TCP cancellation test in that file already uses

The failing test is test_optional_discovery_rejects_incomplete_walks, and the failing cases were internal_error and method_not_found on resources/templates/list. They don't test the deadline, yet the client turns its 0.2 s timeout into the deadline for the whole page walk. In the five failures the case took 0.56 to 0.97 s instead of the usual 0.04 s, and in the four that got as far as counting requests, no page request had reached the mock server, so the walk ran out of time before its first page. The shard runs with reruns: 0, so one stall turns the job red

User Flow

Before: a contributor's pull request goes red on an MCP test their change never touched

  1. They open a pull request against main and Unit Tests runs its mcp-integration shard
  2. About one run in four, test_optional_discovery_rejects_incomplete_walks[False-internal_error-resources/templates/list] fails with assert 0 == 2
  3. The shard has no reruns, so the check stays red until someone re-runs the job

After: the same pull request gets a green mcp-integration check

  1. They open a pull request against main and Unit Tests runs its mcp-integration shard
  2. All 42 test_optional_discovery_rejects_incomplete_walks cases pass
  3. The check stays green

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

No new tests, since this only changes the timeout one existing test passes to its client. tests/unit/experimental_mcp_client/ passes locally, 428 tests, and every assertion in the changed test is untouched

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Tests-only PR, so there is no request to curl. Product code is the same in Before and After. The CI failures this reproduces are on main at 0980f756bd, 6d7d183a80, ef6aa4ad66, 0c515ed7a8 and 6997223068

The stall comes from this local pytest plugin, saved at the repository root and not part of the PR. It blocks the event loop once per test, the first time the test asks for a page of prompts, resources or resource templates, the way the CI worker stalled

stall_first_list_page.py
import os
import time

import pytest
from mcp.client.session import ClientSession

STALL_SECONDS = float(os.environ.get("STALL_SECONDS", "0.5"))


@pytest.fixture(autouse=True)
def stall_first_list_page(monkeypatch):
    stalled = []

    def wrap(name):
        original = getattr(ClientSession, name)

        async def stalling(self, *, params=None):
            if params is None and not stalled:
                stalled.append(name)
                time.sleep(STALL_SECONDS)
            return await original(self, params=params)

        return stalling

    for name in ("list_prompts", "list_resources", "list_resource_templates"):
        monkeypatch.setattr(ClientSession, name, wrap(name))

Before (0980f75)

0.5 s stall

  1. STALL_SECONDS=0.5 PYTHONPATH=. uv run pytest -p stall_first_list_page tests/unit/experimental_mcp_client/test_mcp_client.py -k test_optional_discovery_rejects_incomplete_walks
  2. 42 failed. [False-internal_error-resources/templates/list] fails with assert 0 == 2 and logs MCP client list_resource_templates failed - Error Type: TimeoutError, the same as CI
  3. [True-method_not_found-resources/templates/list] fails with TimeoutError raised by anyio.fail_after after anyio.WouldBlock, the same as the ef6aa4a failure

No stall

  1. uv run pytest tests/unit/experimental_mcp_client/test_mcp_client.py -k test_optional_discovery_rejects_incomplete_walks
  2. 42 passed in 4.2 s

After (7966a35)

0.5 s stall

  1. STALL_SECONDS=0.5 PYTHONPATH=. uv run pytest -p stall_first_list_page tests/unit/experimental_mcp_client/test_mcp_client.py -k test_optional_discovery_rejects_incomplete_walks
  2. 42 passed
  3. With STALL_SECONDS=1.0, 42 passed

No stall

  1. uv run pytest tests/unit/experimental_mcp_client/test_mcp_client.py -k test_optional_discovery_rejects_incomplete_walks
  2. 42 passed in 15.4 s, with each of the six deadline cases taking 2.0 s

Type

Test

Caveats (if any)

Low

  • The six deadline cases now take 2 s each instead of 0.2 s
    • The file runs about 11 s longer on its xdist worker
  • A deadline case can still fail if a stall passes about 1.9 s
  • Why the worker stalls is not known
    • It hits about the same point of the run each time, which fits a garbage collection pass
  • Other tests in the file still use a 0.2 s timeout and are left alone

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

test_optional_discovery_rejects_incomplete_walks gives every case a
0.2 s client timeout, which the client uses as the HTTP timeout and as
the deadline for the whole page walk. Since 6997223, mcp-integration
on main has failed 5 of 20 runs on cases that do not test the deadline
at all, internal_error and method_not_found on resources/templates/list.
The failed case took 0.56 to 0.97 s instead of the usual 0.04 s, and
the four failures that reached the request count show that no page
request got to the mock server, so the worker had stalled for longer
than the whole budget. The shard runs without reruns, so one stall
fails the job

Give the deadline case 2 s and every other case 30 s, the same split
test_cancellation_delivers_termination_over_tcp already uses. Blocking
the event loop for 0.5 s at the first page request reproduces the CI
errors for all 42 cases on main, and all 42 pass with this change, with
a 1 s stall too. The six deadline cases now wait 2 s each, so the file
takes about 11 s longer
@pylaterreur
pylaterreur requested a review from a team October 1, 2026 09:44
@greptile-apps

greptile-apps Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Low risk] Adjusts test timeout values to prevent flakiness.

The PR appears safe to merge.

Summary

The PR gives deadline cases a 2-second client timeout and other optional-discovery cases a 30-second timeout to prevent worker stalls from failing the test.

  • Production code and test assertions are unchanged.

Reviews (1) · Last reviewed commit: "test(mcp): stop the discovery walk test ..."

@codecov

codecov Bot commented Oct 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pylaterreur

Copy link
Copy Markdown
Author

Closing as superseded by #44007, which fixed the same flaky test with a manual-clock event loop

@pylaterreur pylaterreur closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant