Repository navigation
fix(tests): isolate concurrent pytest runs with per-run tmpfs basetemp - #1785
Conversation
Problem: #1026 routed pytest temp dirs onto /dev/shm with a basetemp keyed on sha1(rootpath)[:8] — unique per checkout, but IDENTICAL for two runs from the same checkout. pytest ensure_reset_dir()s an explicit basetemp at startup, so a second same-checkout run wipes the first's xdist worker dirs mid-flight, surfacing as spurious FileNotFoundError. This forced a "one pytest at a time" constraint and produced false failures whenever two suites ran concurrently (agent background runs, a manual run alongside a hook run). What changed: - basetemp is now pytest-polylogue-{checkout}-{run_id}. The controller mints run_id once (pid + uuid) and exports POLYLOGUE_PYTEST_RUN_ID + POLYLOGUE_PYTEST_CHECKOUT so this run's xdist workers inherit the identical path while a concurrent run gets its own. Per-checkout grouping (worktree isolation from #1026) is preserved as the first component. - The shared seeded_db root is re-keyed on the checkout hash instead of the now per-run basetemp, so cross-run reuse survives (the corpus is read-only after build and the build is flock-serialized). Concurrent runs share one seeded corpus safely. - Per-run basetemps are freed on sessionfinish (controller only); a startup sweep reclaims orphans from SIGKILLed runs (>6h, excluding seeded roots), keeping /dev/shm bounded. Verification: - nix develop -c pytest tests/unit/pipeline -n 4 (x2 concurrent, same checkout): both 403 passed, distinct basetemps, zero FileNotFoundError. - seeded_db consumer run twice: build 27.06s -> reuse 17.73s (.build.done), root pytest-polylogue-seeded-<checkout> kept; per-run basetemps cleaned. - nix develop -c devtools verify --quick: ruff/mypy/render-all green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR refactors how pytest manages RAM-backed temporary directories in ChangesTmpfs Basetemp Lifecycle
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes flaky failures from concurrent pytest runs sharing the same tmpfs basetemp by making the tmpfs basetemp per-run (while retaining per-checkout isolation) and preserving cross-run reuse of the seeded corpus by re-keying it on the checkout hash instead of the basetemp.
Changes:
- Make
/dev/shmbasetempunique per run via{checkout_hash}-{run_id}and export env vars so xdist workers share the same run basetemp. - Add best-effort startup sweeping of stale per-run tmpfs basetemps and controller-only cleanup on
pytest_sessionfinish. - Re-key
seeded_dbshared tmpfs root by checkout hash to preserve cross-run seeded corpus reuse.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| run_id = os.environ.get("POLYLOGUE_PYTEST_RUN_ID") | ||
| if run_id is None: | ||
| run_id = f"{os.getpid()}-{uuid.uuid4().hex[:8]}" | ||
| os.environ["POLYLOGUE_PYTEST_RUN_ID"] = run_id | ||
| _sweep_stale_tmpfs_basetemps(shm) | ||
| config.option.basetemp = str(shm / f"pytest-polylogue-{checkout}-{run_id}") |
| if os.environ.get("PYTEST_XDIST_WORKER"): | ||
| return | ||
| if not os.environ.get("POLYLOGUE_PYTEST_RUN_ID"): | ||
| return | ||
| basetemp = session.config.option.basetemp |
| cutoff = time.time() - max_age_s | ||
| for entry in shm.glob("pytest-polylogue-*"): | ||
| if "-seeded-" in entry.name: | ||
| continue | ||
| try: | ||
| if entry.is_dir() and entry.stat().st_mtime < cutoff: | ||
| shutil.rmtree(entry, ignore_errors=True) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d8bf6bd036
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…w pytest (#1786) ## Summary Adds `devtools test <pytest args>` — a focused inner-loop runner that routes a selection through the managed harness, so agents and humans never hand-roll raw `pytest`. Complements `devtools verify` (the full gate); it is not a substitute. ## Problem `devtools` owned the full verification gate (`verify`) but had no comfortable entrypoint for "run this file / `-k` expr / directory." The result: agents reach for raw `pytest`, hand-rolling worker counts, env, and tmpfs handling — exactly the ad-hoc path that, in a recent session, led to launching overlapping suites that raced on shared test state and produced false failures. ## Solution `devtools/run_tests.py` (`devtools test`): - Forwards any selection (paths, `-k`/`-m`, `-x`, …) to pytest via the control-plane dispatch's existing passthrough (`allow_extra_args` / `ignore_unknown_options`). - Managed environment parity with `verify` (`POLYLOGUE_ROOT`, `POLYLOGUE_REPO_ROOT`, repo-local `PYTHONPYCACHEPREFIX`). - Single-process default (`-n 0`) for fast focused runs; override with an explicit `-n` or `POLYLOGUE_PYTEST_WORKERS`. - Live, streamed output (unlike `verify`, which captures for aggregation). - A checkout-scoped `flock` serializes overlapping `devtools test` runs so two suites from one checkout don't burn CPU racing. Correctness under concurrency is already guaranteed by the per-run tmpfs basetemp (#1785); this lock is the throughput guard. `POLYLOGUE_TEST_NO_LOCK=1` bypasses it. - Empty selection prints guidance and exits 2 (does not silently run the whole suite — that's `devtools verify`). Registered as a featured catalog command; `docs/devtools.md` + `AGENTS.md` regenerated; `TESTING.md` steers the inner loop to `devtools test`. ## Verification ``` devtools test tests/unit/devtools/test_run_tests.py -> 7 passed (dogfooded, -n 0) # two concurrent runs, same selection: -> exactly one printed "waiting for in-flight run"; both passed (lock works) devtools verify --quick -> EXIT 0 (ruff/mypy/render-all/verify-doc-commands + all gates) ``` New unit tests in `tests/unit/devtools/test_run_tests.py` cover worker-default and override, `--json` stripping, the no-selection guard, exit-code passthrough, and managed-env contents. Co-authored-by: Claude Opus 4.8 <[email protected]>
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
pytest's tmpfs basetemp (#1026) was keyed only on the checkout, so two runs
from the same checkout shared one path and clobbered each other. This makes
the basetemp per-run while preserving per-checkout worktree isolation and
cross-run seeded-corpus reuse.
Problem
#1026 routes pytest temp dirs onto
/dev/shmwithbasetemp = pytest-polylogue-{sha1(rootpath)[:8]}— unique per checkout, butidentical for two concurrent runs from the same checkout. pytest
ensure_reset_dir()s an explicit basetemp at startup, so a second same-checkoutrun rmtrees the first's xdist worker dirs mid-flight, surfacing as spurious
FileNotFoundErrorunder setup.This forced an undocumented "one pytest at a time per checkout" constraint and
manufactured false failures whenever two suites overlapped — e.g. an agent
launching background suites, or a manual run alongside a git-hook run. (This bit
hard in a recent session: every "let me re-run to be sure" launch made results
less trustworthy because the runs overwrote each other's tmpfs.)
Mechanism confirmed empirically: two concurrent invocations both resolve to
/dev/shm/pytest-polylogue-9b40a785.Solution
tests/conftest.py:pytest-polylogue-{checkout}-{run_id}. The controllermints
run_idonce (pid + uuid4) and exportsPOLYLOGUE_PYTEST_RUN_ID(+
POLYLOGUE_PYTEST_CHECKOUT) so this run's xdist workers inherit theidentical path while a concurrent run gets its own. The per-checkout hash
(worktree isolation from verification runtime: outlier tests slow full baseline to ~6 min #1026) stays as the first component.
seeded_dbroot is re-keyed on thecheckout hash instead of the now per-run basetemp. The seeded corpus is
read-only after build and the build is flock-serialized, so cross-run and
concurrent-run sharing is safe; without this re-key the corpus would rebuild
every run.
sessionfinish(controlleronly — workers share it); a startup sweep reclaims orphans from SIGKILLed
runs (>6h, excluding the shared seeded roots).
No production code touched;
conftest.pyis excluded from the clock-hygienelint, so the sweep's
time.time()is in-policy.Verification
Follow-ups
The behavioral guard (don't launch overlapping suites) is documented separately
in global agent instructions; this PR removes the underlying hazard so an
accidental overlap no longer corrupts results.
Summary by CodeRabbit