Skip to content

fix(tests): a nested in-process pytest run evicts numpy from sys.modules, so a later test in the same worker fails to import it #1807

Closed

Description

@dan-petty

tests/test_ai_subcommands_rag.py::test_try_retrieve_rag_context_success fails in the pre-push gate whenever its xdist worker has earlier run tests/test_ai_test_gen.py::test_test_gen_emits_failing_scaffolds_without_presence_assertions. It blocked the push of #1805 three times (gate runs of 4m41s and 2m49s failed; other runs of the same tree passed, because which worker gets which test varies). The release branch's gate is red whenever the two land on one worker.

Cause

The test_ai_test_gen.py test passes -o addopts= to pytester.runpytest_inprocess, which drops the repository's -p no:randomly. The nested session therefore loads pytest-randomly, which imports numpy. When the test ends, pytester restores sys.modules to the snapshot it took at the start, so numpy leaves sys.modules while its C extension stays loaded in the process. numpy cannot initialise twice: the next import of it in that worker raises ImportError: cannot load module more than once per process.

The RAG test is where it shows. _try_retrieve_rag_context imports qdrant_client lazily through count_points, which imports numpy, and its except Exception: pass turns the ImportError into None, so the assertion reports None == '<rag_context>mock content</rag_context>' and nothing names the cause. Any later test that first-imports numpy in that worker fails the same way.

Evidence

  • Serial, one process: pytest -n 0 tests/test_ai_test_gen.py tests/test_ai_subcommands_rag.py::test_try_retrieve_rag_context_success fails, deterministically. The RAG test alone, and after test_ci_repeat.py, tests/invariants/test_hermetic_floor.py or test_isolation_tripwire.py, passes.
  • A temporary log of the swallowed exception in a gate run showed ImportError('cannot load module more than once per process') raised from numpy/_core/multiarray.py while qdrant.py's lazy __getattr__ imported qdrant_client.http.models.
  • test_ci_repeat.py also runs a nested session with pytest-randomly, but its fixture imports numpy before the nested run, so the snapshot already holds it.

Proposed remedy

Give the nested run in test_ai_test_gen.py the same -p no:randomly the repository's addopts gives every session, and make the test assert that the nested run leaves numpy's presence in sys.modules as it found it, so the next nested run that loads a numpy-importing plugin fails in its own test, not in an unrelated one.

Activity

  1. added this to the v0.2.39 milestone on Oct 21, 2026
  2. added
    type/bug
    priority/p0-critical
    scope/ci
    source/agent
    on Oct 21, 2026
  3. added a commit that references this issue on Oct 21, 2026
    b200a02
  4. dan-petty commented on Oct 21, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1808, merged into release/v0.2.39 as b200a02.

    A test that imports numpy for the first time no longer fails after test_ai_test_gen.py has run in its pytest worker.

    3 file(s) changed.

    How it was verified

    Check runs at 18ea699:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • Fail-to-Pass Check: pass
    • Fail-to-Pass Self-Check: pass
    • devops-cli: pass
    • zizmor: pass
    • bandit: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • Changed-Test Independence: pass
    • Rule Pack Tests: pass
    • Tests & Coverage: pass
    • Dependency Freshness: skipping
    • Service Image: skipping
    • DevContainer Image: skipping

    Acceptance Criteria of docs/agent/tasks/task-1807-nested-pytest-numpy.md:

    No Acceptance Criteria section.

  5. added a commit that references this issue on Oct 23, 2026
    f209f6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p0-criticalscope/cisource/agenttype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions