Skip to content

test(isolation): module-scope sys.modules stubs leak across xdist workers — one guard + one canonical fixture for the class (#18181, #18529, #18601, #18618, #18511, #17648 are instances) #18893

Open

Description

@mrveiss

Class defect: test modules install sys.modules stubs at import or module scope, or in conftest without a matching teardown. The stub outlives the file and breaks whichever test the same xdist worker collects next. Each instance has been filed and fixed one at a time. This issue is the single home for the class: one fix (a guard plus a canonical fixture), with every instance listed below.

Instances already filed (each one is a witness of this class):

New, seen 2026-10-19 in a targeted -n 4 run on branch issue-18825-graph-owner-scope. Most of 96 failures came from these two:

  • autobot-backend/tests/memory_graph/test_property_graph.py stubs autobot_shared.ssot_config at module scope.
  • autobot-backend/tests/memory_graph/test_invalidate_endpoints.py replaces the autobot_memory_graph package at import time. Production modules doing from autobot_memory_graph import X then fail in later tests.

Fix (once, for the class):

  • One canonical fixture in autobot_shared test helpers, or the root conftest, that installs stubs through monkeypatch.setitem(sys.modules, ...), so pytest restores them after each test.
  • A ratchet guard (AST) that rejects module-scope sys.modules[...] =, sys.modules.update, and patch.dict(sys.modules, ...) started outside a fixture, in test files and conftests. The baseline lists the current offenders and only shrinks.

AC:

  • The canonical fixture exists and is documented.
  • The guard rejects a planted module-scope stub (control) and passes with the baseline.
  • Every instance above is migrated, or carries a baseline entry linking its issue.
  • The linked instance issues close against this fix.

Activity

  1. mrveiss commented on Oct 19, 2026

    @mrveiss
    OwnerAuthor

    Another instance (2026-10-19): running autobot-slm-backend/tests/services/ as one -n 4 invocation from inside autobot-slm-backend produces ModuleNotFoundError: No module named 'services.playbook_output_stream'; 'services' is not a package in 6 tests: test_extra_vars_file_11735.py (3) and collection errors in test_dynamic_inventory_group_vars_11781.py, test_self_update_systemd_detach_11492.py and test_wizard_inventory_parity_14286.py. Some earlier file in that directory leaves a non-package services in sys.modules. The branch under test touched no SLM backend services code.

  2. mrveiss commented on Oct 19, 2026

    @mrveiss
    OwnerAuthor

    Another instance (2026-10-20), order-dependent in the other direction: autobot-slm-backend/tests/services/test_token_denylist.py::TestDecodeTokenAsyncRevocation::test_a_healthy_epoch_store_with_no_marker_accepts_token fails when the file runs alone with RevocationCheckUnavailable: no Redis client → 503, and passes when run in a batch with tests/api/test_auth_*. The test patches password_epoch.get_async_redis_client, but the SLM process uses its own epoch store (#18663 use_epoch_store). That store is set up, or not, by whichever test ran first. The fix belongs to the canonical fixture: install the epoch store explicitly per test.

  3. mrveiss commented on Oct 20, 2026

    @mrveiss
    OwnerAuthor

    Another instance (2026-10-20): in one -n 4 invocation over whole autobot-backend/services + knowledge + chat_workflow + chat_history, these failed. knowledge/connectors/egress_vendor_behaviour_test.py::test_the_default_vendor_host_is_allowed[gdrive|notion|onedrive|slack] gave RuntimeError: Event loop is closed and the mock was awaited 0 times. services/knowledge/test_kb_synthesizer.py::test_synthesize_docs_uses_collection_config_prompt saw chat awaited 2 times. All five pass in a small batch on the same tree (1121 passed). The branch under test (#16664) touches neither file.

  4. added
    needs-structureMissing goal milestone, matching phase label or native parent (#19522)
    on Nov 4, 2026
  5. github-actions commented on Nov 4, 2026

    @github-actions
    Contributor

    needs-structure: this issue is missing:

    • a goal milestone M<n> - ...: none is set
    • a phase:M<n>.<k> label whose <n> matches the goal milestone: found none
    • a native parent issue (add one, or write Parent: #N in the body), unless this is an umbrella with sub-issues

    The label clears on the next issue event once all three are present (#19522).

  6. mrveiss commented on Nov 12, 2026

    @mrveiss
    OwnerAuthor

    Sighting (2026-11-12), same class: autobot-backend/conftest.py (around line 929) registers code_intelligence.pattern_analysis in sys.modules as a bare stub package with no storage submodule. Two new tests in PR #21328 patched …pattern_analysis.storage.<name> by dotted string, which only set a throwaway attribute on the stub; the code under test then failed its function-local from code_intelligence.pattern_analysis.storage import … with No module named …storage in two python-suite shards. Fixed locally in that PR with fakes installed through monkeypatch.setitem; the stub in the shared conftest remains.

    A second effect seen while reviewing PR #21361: that conftest installs about 50 such stubs, and they are visible to every test module that runs in the same worker — so a test outside autobot-backend/ can depend on them without declaring it, and would fail when run without that conftest (for example in a narrower guard-only run).

    • The backend conftest's package stubs are scoped (fixture + monkeypatch) or complete enough that a dotted-path patch cannot silently hit a stub.
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

    needs-structureMissing goal milestone, matching phase label or native parent (#19522)tech-debttesting

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions