Skip to content

test: reset console routing for every suite; fail tests that leak process state (part of #4115) - #4226

Merged
Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
p2-test-state-leaks
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 2 commits into
mainfrom
p2-test-state-leaks

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes the Windows-only unit failure on main
tests/unit/install/phases/test_targets_phase.py::test_config_default_target_provenance_names_config_source
(Windows units (1/3), runs 44705583099, 44705963213, 44706384587) and closes
the leak class behind it.

Root cause: tests/unit/core/test_output_mode.py::test_failing_machine_output_keeps_update_notice_off_stdout
runs cli audit --format=json in-process. configure_output_mode then calls
set_console_stderr(True), which routes console output to stderr for the rest
of the process. Only tests/integration/conftest.py reset that state, so a later
unit test on the same xdist worker read '' from capsys. The #4032 shard
reshuffle put the victim after the polluter on gw3. The failure is
order-dependent but not Windows-specific: it reproduces on Linux with
-n 0 in that order.

The other gw3 failure (git executable cache) is owned by #4123 (merged); this
PR does not touch the git cache.

Changes

  • tests/conftest.py: _reset_console_state moves to the root conftest and
    now applies to every suite. _reset_config_cache is new: get_config()
    caches ~/.apm/config.json for the life of the process, so config read
    under a patched HOME would reach later tests.
  • tests/conftest.py: _apm_state_leak_detector (autouse, outermost)
    snapshots the environment (including PATH), cwd, console routing, the
    config cache and the git/gh executable memo. If a test leaves any of them
    changed, the detector fails that test and restores the state.
    The logic lives in tests/utils/state_leaks.py.
  • Shrink-only allowlist: tests/fixtures/test_state_leak_baseline.json, with
    BASELINE_CEILING = 0. A stale entry fails, and the baseline cannot grow
    past the ceiling. It is empty: every leaker found was fixed.
  • One more leaker fixed: tests/unit/test_ssl_cert_hook.py. The hook wrote
    APM_SSL_CERT_FILE_IS_BUNDLED_DEFAULT into os.environ and nothing
    restored it.
  • tests/integration/conftest.py keeps its post-test cwd repair. The
    tests/integration/test_wave6_outdated_view_coverage.py import now points
    at the root conftest; that import is what broke collection in the earlier
    reverted attempt.

Scope note: the detector skips tests/integration/ and tests/benchmarks/.
Those suites run real subprocesses, and their session fixtures shape the
environment on purpose. The console and config resets still cover them.

Issue and approved scope

Issue: Part of #4115

Human scope-approval comment: #4115 (comment)
(scope: "Fix any Windows unit failure the new lane exposes at its owner")

Does this PR complete the issue, or what remains? No. The PR-lane topology
change for #4115 is on p1-windows-units-on-pr, which integrator PR #4150
merges. This PR covers only the unit-pollution failure.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Evidence (Linux, Python 3.12, same SHA family):

  • Red before, on main 9d50de15: uv run pytest -p no:randomly -n 0 <polluter> <victim>
    gives 1 failed, 1 passed with AssertionError: assert 'source: apm config target' in ''.
  • Green after: tests/unit/test_state_leak_detector.py::test_machine_output_polluter_then_stdout_victim_pass_on_one_worker
    runs the same pair in one process and both pass.
    uv run pytest tests/unit/test_state_leak_detector.py gives 12 passed.
  • Full unit suite with the detector, uv run pytest tests/unit tests/test_console.py -n 4 --dist worksteal:
    0 detector failures. The other failures are the same 294 this sandbox shows on
    main (it runs as root, so the installer tests refuse); comm shows 0 new
    failures. Before test: stop the agentrc probe from poisoning the git executable cache #4123 landed, the detector flagged 24 git-cache leaks; with
    test: stop the agentrc probe from poisoning the git executable cache #4123 merged there are 0.
  • Lint: ruff check, ruff format --check, pylint R0801, lint-auth-signals,
    lint-architecture-boundaries and check_complexity_ratchet all pass.

Detector overhead: about 40 us per test (two snapshots plus the diff, with
PYTEST_CURRENT_TEST changing each phase). That is ~1.2 s CPU over 28,992 unit
tests, or ~0.3 s wall on 4 xdist workers. The env snapshot copies
os.environ._data rather than decoding every entry; the first version, with
dict(os.environ), cost ~350 us per test. Full-suite wall time on this
machine was within noise: 340.7 s on main against 271.6 s after, under
different background load. PR CI (run 44716214987), PR unit step
"Run tests with coverage": shard 1 371 s, shard 2 264 s. Three other PRs on
the same base without the detector ran 295/366 s, 242/359 s and 263/304 s,
so the difference is within runner noise.

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md:
    user-visible effect first, 25 words or fewer, ending with (#PR);
    breaking changes only under upgrade notes; no Highlights; see
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

…cess state

Part of #4115. Windows units (1/3) on main went red after the shard
reshuffle (runs 44705583099, 44705963213, 44706384587):
test_output_mode.py runs `cli audit --format=json` in-process, and
configure_output_mode -> set_console_stderr(True) kept console output on
stderr for the rest of the xdist worker. Only tests/integration/conftest.py
reset it, so test_targets_phase.py's provenance test captured ''.

- Promote _reset_console_state to the root conftest (every suite); the
  integration conftest keeps its post-test cwd repair.
- Reset the process-wide config cache around each test.
- Add _apm_state_leak_detector (outermost autouse): snapshots env incl.
  PATH, cwd, console routing, config cache and the git/gh executable
  memo; fails the leaking test and restores state. Shrink-only baseline
  tests/fixtures/test_state_leak_baseline.json (empty, ceiling 0).
- Fix the one other leaker it found (test_ssl_cert_hook env).
- Regression: polluter then victim in one process must both pass.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.37.0 milestone Nov 20, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 10:05
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The environment leak detector snapshots a stale import-time mapping, missing replacements of os.environ and allowing leaked state to persist into later tests.

1 open issue
What changed in this PR

This PR addresses the test-state pollution exposed by #4115’s Windows unit coverage. It strengthens shared test isolation without changing product behavior or CI topology.

Changes:

  • Moves console resets to the root fixtures and adds config-cache isolation across suites.
  • Adds process-state leak detection for tests outside integration and benchmarks, with an empty baseline and regression coverage.
  • Restores environment changes made by SSL-hook tests and updates the relocated fixture import.
File Description
tests/​utils/​state_leaks.py Adds state snapshots, leak comparison, restoration and baseline handling.
tests/​unit/​test_state_leak_detector.py Tests detector helpers, fixture ordering and the original pollution regression.
tests/​unit/​test_ssl_cert_hook.py Restores environment variables after SSL-hook tests.
tests/​integration/​test_wave6_outdated_view_coverage.py Updates the console-reset fixture import.
tests/​integration/​conftest.py Removes the relocated console reset while retaining cwd repair.
tests/​fixtures/​test_state_leak_baseline.json Adds an empty known-leak baseline.
tests/​conftest.py Adds shared console/config resets and the leak-detection fixture.
CHANGELOG.md Records the test-isolation changes.

Review effort: Balanced

Comment thread tests/utils/state_leaks.py Resolved
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 5aa5423 into main Nov 20, 2026
30 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the p2-test-state-leaks branch November 20, 2026 10:17
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 20, 2026
…er; restore PYTHONIOENCODING (#4115)

- test_windows_unit_diagnostics.py pinned build-release's inline Windows
  unit command; the step now calls `python -m scripts.ci_selections
  windows-unit`, so the contract checks that call and that the owner's
  selection keeps the Linux roots, worksteal xdist (-n 4) and the
  diagnostic flags (plus mutation cases).
- test_version_entrypoint.py: main() sets PYTHONIOENCODING for child
  processes on Windows (cli.py); the in-process test now restores
  os.environ, which the #4226 leak detector flagged on Windows units.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Takeshi Masukawa (takeaship) pushed a commit to takeaship/apm that referenced this pull request Nov 20, 2026
main() calls _configure_encoding(), which on Windows sets
os.environ["PYTHONIOENCODING"]. test_version_entrypoint never restored it,
so the microsoft#4226 state-leak detector failed
test_argument_placement_preserves_click_behavior on every Windows main push.
A module autouse fixture registers the variable with monkeypatch, so it
returns to its exact prior state, absent included.

Co-authored-by: Copilot App <[email protected]>
Takeshi Masukawa (takeaship) pushed a commit to takeaship/apm that referenced this pull request Nov 20, 2026
…ion candidate

The root conftest's leak detector (microsoft#4226) reads
tests/fixtures/test_state_leak_baseline.json; the mutation harness copied
tests/conftest.py into its candidate tree but not the baseline, so every
case errored with FileNotFoundError.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants