Repository navigation
fix(tests): let asyncio start under the unit-suite network guard - #4115
Conversation
Running the unit suite on Windows reported 556 failures and 526 errors on a commit that is green in CI, so a local run was worthless as a gate. Windows has no native socketpair(); CPython emulates it over a loopback TCP connect, and asyncio builds every event loop's self pipe that way — so the guard's global patch of socket.socket.connect killed every async test at setup, before its own body ran. Unblocking socketpair() alone, for the duration of that one call, takes the suite to 108 failures and 21 errors. Exempting loopback wholesale would have worked too, but it would let a test silently reach a local Lemonade server on 127.0.0.1:8000. The guard stays strict for every real outbound connect, loopback included.
Verdict: Approve with suggestionsThis makes Two things worth handling before or shortly after merge:
One smaller point: the fix disarms and re-arms the guard by swapping a process-wide attribute, so it briefly opens the guard for the whole process and will stomp on a test that patched connect itself. A thread-local flag does the same job without either problem — details below. Real-world evidenceN/A — tests-only change, and no 🔍 Technical detailsIssues🟡 The At least ten sites opted out of
🟢 Swapping the class attribute is not thread-safe and clobbers test-local patches (
A thread-local re-entrancy flag avoids all three, keeps the monkeypatch teardown authoritative, and is no harder to read. Replacing (Applied as written this replaces lines 77-98, i.e. the existing 🟢
Strengths
|
…#4136) ## Why this matters A Windows contributor running the four shell test suites sees `112 skipped`, which is visually indistinguishable from green. CI on ubuntu and macOS sees 112 real results. So the loop is: verify locally, see no red, push, watch CI fail — which is exactly how the confirm-gate defect in amd#3808 reached CI undetected. The skip reason is genuine for the tests that actually launch a process: Windows runs each pipeline through `cmd.exe`, and `os.killpg` / process groups are POSIX-only. It is *not* genuine for the refusal, parsing, grant and tool-description tests — those decide before anything reaches a shell, and a module-level skip was hiding them on the platform most GAIA contributors develop on. After this change Windows runs **70 passed / 42 skipped** instead of `112 skipped`, and every one of those 70 is a real assertion about the security-adjacent refusal path. POSIX is untouched: the per-test mark carries the same `sys.platform == "win32"` condition the module-level one did, so off Windows it is a no-op and all 112 still run. <details> <summary>🔍 Technical details</summary> **Membership was measured, not guessed.** A throwaway pytest plugin stripped the `cmd.exe` skipifs, wrapped `shell_tools.subprocess.Popen` / `.run`, and recorded which node IDs actually spawned a child. 42 did; 70 did not. The 70 were then run on Windows 11 and all passed; every one of the 10 failures fell inside the 42. So the split is empirical, not a reading of the test names. That measurement mattered, because *running the tests* would have given the wrong answer. This dev box has MSYS coreutils on `PATH`, so `ls`, `cat`, `sort` and `date` resolve and most execution tests pass here — they would not on a clean Windows machine or a `windows-latest` runner. Classifying on "does it pass on my box" is the hidden-state trap CLAUDE.md warns about; classifying on "does it spawn a process" is not. - `pytestmark = pytest.mark.skipif(...)` → `posix_only = pytest.mark.skipif(...)`, same reason string per module, applied as `@posix_only` to the 35 spawning tests across three files. - **No assertion was weakened, relaxed, or deleted.** The diff is 53 insertions / 3 deletions; the 3 deletions are the `pytestmark` name on each of the three lines. - Node IDs are unchanged (no reorganisation into classes), so other in-flight PRs that name these tests in their test plans still resolve. - `test_shell_env_assignment.py`'s two existing `shutil.which("pytest")` skipifs stack above `@posix_only`; pytest ORs them, so behaviour off Windows is identical. - `tests/unit/test_shell_pipelines.py` keeps its module-level mark — all seven of its tests genuinely run a pipeline, so there is nothing to promote. `test_shell_output_encoding.py` already used per-test marks and is untouched. - One pleasing side effect: `test_windows_never_hands_a_compound_line_to_cmd_exe_as_one_string` — a fully-mocked test *about* the Windows path — was itself skipped on Windows. It now runs there. **Merge-conflict note for reviewers:** amd#3721 and amd#3402 both touch `test_shell_compound_commands.py`, neither near the `pytestmark` line. Whichever lands second may need a trivial rebase. </details> ## Scope This does **not** close amd#4127. That issue asks for `windows-latest` in the `test_unit.yml` matrix, gated on amd#4115 and amd#4119 landing first so the lane starts green. This is a prerequisite for that lane being worth anything — without it, 112 of the tests the new lane would run are skipped by construction. ## Test plan - [x] `python -m pytest tests/unit/test_shell_compound_commands.py tests/unit/test_shell_env_assignment.py -q` on Windows 11 — was `77 skipped`, now `51 passed, 26 skipped` - [x] Same for all four modules — was `112 skipped`, now `70 passed, 42 skipped` - [x] `python -m pytest tests/unit/test_shell*.py tests/unit/test_powershell_command_injection.py -q` — `266 passed, 42 skipped` - [x] `python util/lint.py --all` — output byte-identical to a clean `origin/main` worktree (both exit 1 on the same pre-existing mypy findings in `src/gaia/`, none in files this PR touches) - [ ] CI on ubuntu/macOS — expect the same counts as before this PR; the mark is a no-op off Windows Towards amd#4127. Co-authored-by: Kalin Ovtcharov <[email protected]>
Running
pytest tests/unit/on Windows reported 556 failures and 526 errors on a commit that is green in CI, so a local test run was useless as a gate — and actively misleading to anyone, or any agent, treating it as one. The suite's own network guard caused most of it: Windows has no nativesocketpair(), CPython emulates it over a loopback TCP connect, and asyncio builds every event loop's self pipe that way, so the guard killed every async test at setup before its body ran. Lettingsocketpair()through — and only that, only for the duration of the call — takes the same checkout from 556 failures / 526 errors to 108 / 21. The remaining 108 are real, unrelated breakage that was previously buried.The guard stays strict for every real outbound connect. Exempting loopback wholesale would also have fixed the symptom, but it would let a unit test silently reach a local Lemonade server on
127.0.0.1:8000.No Windows CI lane yet. It is the only thing that stops this bug class hiding again, but at 108 residual failures (chiefly #4102 console encoding and #3912 Python 3.13) the suite is not green enough to gate on, and a permanently red lane teaches people to ignore it. Follow-up once those land.
Closes #4112
Test plan
pytest tests/unit/test_network_guard.py -v— 4 passed; reverting theconftest.pychange turns 2 of them red, so the regression test is a real gatepytest tests/unit/test_telegram_adapter.py -qon Windows — 17 passed (was 3 passed, 14 errors)pytest tests/unit/ -q -p no:randomlyon Windows —108 failed, 12551 passed, 21 errors(was556 failed, 11600 passed, 526 errors)socketpair()is a native syscall there and never touchedconnect()