Skip to content

Full-suite pytest collection: partial-module caching poisons initialization/services.* packages across unrelated test files #12463

Closed

Description

@mrveiss

Summary

Running python3 -m pytest --collect-only -p no:xdist -q across the full autobot-backend/ suite produces collection errors on files that collect cleanly when run standalone or in small targeted groups (confirmed via #12436/#12437 investigation). Root cause is distinct from stub-leak issues previously fixed (#12437, #12452 etc.) — this is Python's own import-caching behavior interacting with pytest's --import-mode=importlib.

Root cause

Several collectors that run early (alphabetically/directory order) in a full-suite run fail mid-import with unrelated pre-existing errors, e.g.:

  • initialization/lifespan_test.py fails via fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <MagicMock ...> is a valid Pydantic field type while executing initialization/__init__.py → .routers → .router_registry → core_routers.py → api.approval_gates.
  • agents/agent_optimizer_test.py, training/completion_trainer_test.py, and others fail similarly for unrelated reasons.

Because Python inserts a module into sys.modules before exec_module() runs, a mid-import exception leaves a half-populated module object cached under that name (e.g. sys.modules["initialization"] exists but is missing names defined after the failing import line, like load_core_routers).

Every subsequent test file in the same collection session that does from initialization import (load_core_routers, ...) (or similar for services.mesh_brain, services.agents, services.slm_client, services.llm_service) gets the broken cached module instead of a fresh import attempt, and fails with a confusing, unrelated-looking error:

ImportError: cannot import name 'load_core_routers' from 'initialization'
ModuleNotFoundError: No module named 'services.mesh_brain'
ModuleNotFoundError: No module named 'services.agents'
ImportError: cannot import name '_SERVICE_JWT_TTL_HOURS' from 'services.slm_client'
ImportError: cannot import name 'LLMService' from 'services.llm_service'

Confirmed files affected by this cascade in a full-suite --collect-only run (as of this filing):
mcp/mcp_security_test.py, system_integration_test.py, security/security_integration_test.py, performance_benchmarks_test.py, services/slm_client_test.py, services/documentation_watcher_staleness_test.py, tests/agents/test_subagent_spawning.py.

Verified NOT caused by #12436/#12437: all 7 files above collect cleanly (0 errors, 147 tests) when run together in an isolated group without the other early-failing collectors present. The cascade only appears in a full-suite run, once an earlier collector (e.g. initialization/lifespan_test.py) partially imports and fails.

Suggested fix directions (needs design decision — filing rather than fixing to keep #12436/#12437's PR scoped)

  1. Fix the primary failures first (the fastapi.exceptions.FastAPIError: Invalid args for response field! ... <MagicMock ...> errors in initialization/lifespan_test.py, judges/judge_integration_test.py, api/collaboration_test.py, api/marketplace_test.py, api/telegram_bot_test.py, api/whatsapp_test.py, tests/api/test_advanced_control_takeover_enum_12208.py, tests/api/test_marketplace.py, tests/test_diagnostics_router_registration.py — likely a stub in conftest.py leaking a MagicMock into a route's response-model type annotation). Fixing these removes the poisoning trigger for the whole cascade.
  2. Independently, consider a pytest hook (pytest_collectstart/pytest_exception_interact, or a pytest_collectreport handler) that evicts any sys.modules entry for a module whose exec_module() raised during collection, so subsequent files get a clean re-import attempt rather than a poisoned cache.

Evidence

Full-repo collect-only baseline (before #12436/#12437 fix): 19266 tests collected, 27 errors.
After #12436/#12437 fix (only those 2 files' errors removed): 19292 tests collected, 25 errors — this issue accounts for the remaining pre-existing errors not touched by that PR.

Related: #12436, #12437 (test collector fixes, PR resolves those; this issue is a separate discovery from that investigation).

Activity

  1. mrveiss commented on Jul 25, 2026

    @mrveiss
    OwnerAuthor

    Fixed in #12475 (merged): real-loaded api.schemas_workflows (killed the shared-MagicMock sys.modules poison), guarded llc/tests services stub, added a narrowed pytest collection-eviction hook (uninitialized/stub modules only). Full-suite collection 25→6 errors (remaining are #12455 dead tests + a scanner NameError + local-only dep gaps).

  2. github-actions commented on Jul 25, 2026

    @github-actions
    Contributor

    PR #12475 (merged to Dev_new_gui) references this issue with a close keyword.

    fix(test-infra): full-suite pytest collection cascade — MagicMock response-model leak + poisoned sys.modules cache (#12463)

    If this issue is fully resolved, close it manually. If work remains, no action is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions