Skip to content

test: backend tracing tests flake with httpx.ReadTimeout on loaded CI runners #1318

Closed

Description

@planetf1

Symptom

Standard CI (code-checks / quality) fails intermittently with:

test/telemetry/test_tracing_backend.py::<test_name> - httpx.ReadTimeout

The failure appears on one Python matrix leg (usually 3.11 or 3.13), cancelling the others. It is not deterministic — the same test passes on other runs without any code change.

Why it happens

All five tests in test/telemetry/test_tracing_backend.py call a real Ollama instance:

Test What it checks
test_span_duration_captures_async_operation span duration ≥ 0.1 s
test_context_propagation_parent_child parent/child span IDs match
test_token_usage_recorded_after_completion Gen-AI OTel attributes present
test_span_not_closed_prematurely span not closed before avalue() returns
test_multiple_generations_separate_spans two generations → two distinct span IDs

CI runners are CPU-only (no GPU). Ollama evicts models after 5 minutes of inactivity (OLLAMA_KEEP_ALIVE=5m). These tests run near the end of the ~22-minute suite, by which point the model has been evicted and must reload. On a loaded CPU runner, that reload exceeds the 300 s httpx read timeout on OllamaModelBackend, so the request raises httpx.ReadTimeout and the test fails.

Background

PR #1302 fixed an earlier, worse version of this problem: test_streaming_span_duration was hanging for the full 900 s pytest-timeout budget. The fix:

  1. Added a 300 s default timeout to OllamaModelBackend
  2. Split the streaming test — fast mocked version runs on every push; real-Ollama version marked @pytest.mark.slow

The commit message for #1302 explicitly noted that test_span_not_closed_prematurely and test_multiple_generations_separate_spans were already hitting ReadTimeout during development, and 300 s was chosen hoping it would be enough. It wasn't — test_multiple_generations_separate_spans hit httpx.ReadTimeout in standard CI on PR #1292 (run 28017020057).

Proposed fix

Apply the same split-and-mock treatment to the remaining five tests:

  • New integration-marked tests (no ollama marker) that patch ollama.AsyncClient.chat to return a synthetic response and assert the same structural/OTel invariants. These run on every push, take < 1 s each.
  • Existing real-Ollama tests marked @pytest.mark.slow so they are excluded from the default addopts = "-m not slow" and run only on nightly/GPU-backed runners.

test_span_duration_captures_async_operation is the one test that checks actual wall-clock timing. A mock with an artificial asyncio.sleep delay — as used in test_streaming_tracing_integration.py — is sufficient to verify the timing invariant without a live model.

Reference

Activity

  1. added
    bug
    p2
    on Jun 23, 2026
  2. changed the title [-]test: complete PR #1302 fix — replace remaining e2e+ollama backend tracing tests with mocked equivalents[/-] [+]test: backend tracing tests flake with httpx.ReadTimeout on loaded CI runners[/+] on Jun 23, 2026
  3. planetf1 commented on Jun 25, 2026

    @planetf1
    ContributorAuthor

    Seen again on PR #1323 (2026-06-25): test_ollama_generate_from_raw_metrics_integration hit httpx.ReadTimeout on all three Python versions (3.11/3.12/3.13) in the quality CI workflow. Caused the quality checks to go red; re-run cleared it. No code changes on that test path in this PR.

  4. dk3yyyy commented on Jul 29, 2026

    @dk3yyyy
    Contributor

    Hi, I'd like to work on this. I verified that current main still runs the five affected telemetry tests against real Ollama under the standard e2e/ollama markers. My plan is to add mocked integration coverage for the same OpenTelemetry invariants, including a delayed async response for span duration, and mark the real-Ollama variants as slow. Could you confirm that approach and assign the issue to me?

  5. planetf1 commented on Jul 30, 2026

    @planetf1
    ContributorAuthor

    Thanks for offering to take this on, and your read of main is correct — those five tests are still e2e + ollama, no slow marker.

    Two things have moved since I opened this, worth folding in before you start:

    The flake itself is already handled. #1369 (merged 6 July, closing #1367 — the same symptom in test_metrics_backend.py) added a retry to every ollama-marked test in test/conftest.py:

    pytest.mark.flaky(reruns=2, reruns_delay=5, only_rerun="ReadTimeout")

    I checked two of yesterday's PR runs directly (job logs, not just the config) — Ollama genuinely serves and all five tests pass against it on every PR, every Python version. No recurrence since #1369 went in. So this is no longer CI-blocking; it's a coverage/speed improvement now — I'd suggest dropping the bug/p2 labels, happy to do that if useful.

    Please don't mark the real-Ollama tests slow. That part of my original proposal was wrong — addopts excludes slow unconditionally, including in the nightly script, so slow currently means "runs nowhere", not "runs on nightly only". test_stream_with_chunking_e2e from #1302 is already stuck there. Moving these five into that category would delete coverage, not relocate it.

    Given that, mocked versions wouldn't close a CI gap either — the real tests already run there. Where they'd help: local pytest runs without Ollama up (currently these five just get skipped), and a fast, deterministic failure instead of the retry-then-300s-timeout path on a stalled runner.

    One correction to my own root-cause writeup: it's not just the 5-minute keep-alive. conftest.py evicts all loaded models at every module boundary, so this file pays a cold load on its first ollama test regardless of timing — worth knowing since it affects how much wall-clock the mocks actually buy back.

    So: yes to adding the mocked tests (the asyncio.sleep pattern from test_streaming_tracing_integration.py works for the duration assertion), as additions alongside the real ones, not replacements. Assigning to you on that basis — let me know if that doesn't match what you had in mind.

  6. removed
    bug
    p2
    on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions