Skip to content

fix(tests): sys.modules stub pollution causes 34+ spurious unit test failures in full-suite runs #935

Closed

Description

@vybe

Summary

Running the full test suite causes 34+ unit tests to fail due to sys.modules stub pollution. All affected tests pass when run in isolation — they only fail when run after specific other test files in the same pytest session.

Root Cause

Several test files install module stubs into sys.modules without using a context manager or monkeypatch scope, so the stubs leak into subsequent test files. tests/unit/conftest.py has a _POP_PREFIXES list that cleans up known stubs between files, but it is missing entries for the modules that are actually being polluted.

Affected Tests (34+ failures, 28+ errors in full suite)

Module contaminated Tests broken Error
services.task_execution_service unit/test_auto_retry_reader_race.py (13), unit/test_file_upload.py (18), unit/test_session_persistence_flag.py (1) _is_reader_race_signature / execute_task resolve to MagicMock instead of real functions
dependencies unit/test_fleet_status_resilience.py (7) from dependencies import require_admin → ImportError
services.cleanup_service unit/test_startup_recovery_race.py (1) _within_startup_grace resolves to a stub that returns truthy for all inputs
services.docker_service test_github_pat_propagation_unit.py (11 errors) list_all_agents_fast missing from stub; only reachable from top-level conftest, not unit/conftest.py

Fix

tests/unit/conftest.py — add to _POP_PREFIXES:

"services.task_execution_service",
"dependencies",
"services.cleanup_service",

tests/conftest.py — add to _SYS_MODULES_INVARIANT_KEYS:

"services.docker_service",

These additions ensure the real modules are re-imported for each test file, eliminating cross-file contamination.

Acceptance Criteria

  • All 4 module names added to the appropriate conftest cleanup lists
  • pytest tests/unit/test_auto_retry_reader_race.py tests/unit/test_file_upload.py tests/unit/test_session_persistence_flag.py tests/unit/test_fleet_status_resilience.py tests/unit/test_startup_recovery_race.py tests/test_github_pat_propagation_unit.py passes in a single run (not just in isolation)
  • Full suite failure count drops by ≥ 34

Activity

  1. added
    priority-p1
    type-bug
    on May 25, 2026
  2. self-assigned this
    on May 25, 2026
  3. added a commit that references this issue on May 25, 2026
  4. added
    status-in-dev
    on May 25, 2026
  5. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

    Shipped in v0.6.0 (released via #998).

  6. added
    theme-infrastructure
    and removed
    status-in-dev
    on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p1theme-infrastructuretype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions