Repository navigation
test: reset console routing for every suite; fail tests that leak process state (part of #4115) - #4226
Merged
Conversation
…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]>
Daniel Meppiel (danielmeppiel)
requested a review
from Sergio Sisternes (sergio-sisternes-epam)
as a code owner
November 20, 2026 10:05
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Contributor
There was a problem hiding this comment.
🟡 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
This was referenced Nov 20, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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_stdoutruns
cli audit --format=jsonin-process.configure_output_modethen callsset_console_stderr(True), which routes console output to stderr for the restof the process. Only
tests/integration/conftest.pyreset that state, so a laterunit test on the same xdist worker read
''fromcapsys. The #4032 shardreshuffle put the victim after the polluter on
gw3. The failure isorder-dependent but not Windows-specific: it reproduces on Linux with
-n 0in 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_statemoves to the root conftest andnow applies to every suite.
_reset_config_cacheis new:get_config()caches
~/.apm/config.jsonfor the life of the process, so config readunder 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.tests/fixtures/test_state_leak_baseline.json, withBASELINE_CEILING = 0. A stale entry fails, and the baseline cannot growpast the ceiling. It is empty: every leaker found was fixed.
tests/unit/test_ssl_cert_hook.py. The hook wroteAPM_SSL_CERT_FILE_IS_BUNDLED_DEFAULTintoos.environand nothingrestored it.
tests/integration/conftest.pykeeps its post-test cwd repair. Thetests/integration/test_wave6_outdated_view_coverage.pyimport now pointsat the root conftest; that import is what broke collection in the earlier
reverted attempt.
Scope note: the detector skips
tests/integration/andtests/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 #4150merges. This PR covers only the unit-pollution failure.
Type of change
Testing
Evidence (Linux, Python 3.12, same SHA family):
9d50de15:uv run pytest -p no:randomly -n 0 <polluter> <victim>gives
1 failed, 1 passedwithAssertionError: assert 'source: apm config target' in ''.tests/unit/test_state_leak_detector.py::test_machine_output_polluter_then_stdout_victim_pass_on_one_workerruns the same pair in one process and both pass.
uv run pytest tests/unit/test_state_leak_detector.pygives 12 passed.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);
commshows 0 newfailures. 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-architecture-boundaries and check_complexity_ratchet all pass.
Detector overhead: about 40 us per test (two snapshots plus the diff, with
PYTEST_CURRENT_TESTchanging each phase). That is ~1.2 s CPU over 28,992 unittests, or ~0.3 s wall on 4 xdist workers. The env snapshot copies
os.environ._datarather than decoding every entry; the first version, withdict(os.environ), cost ~350 us per test. Full-suite wall time on thismachine 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
## [Unreleased]inCHANGELOG.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.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regenerated