Skip to content

test(doctor): write test scripts from a child process so they are never busy when run - #1120

Open
matt2e wants to merge 1 commit into
mainfrom
fix-flaky-doctor-env-snapshot-test
Open

matt2e wants to merge 1 commit into
mainfrom
fix-flaky-doctor-env-snapshot-test

Conversation

@matt2e

@matt2e matt2e commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Symptom

freshness::tests::installed_version_uses_env_snapshot in the doctor crate failed once in Crates CI, run 46581441992 (PR #1112, which changes nothing under crates/doctor), with the crate's other 159 tests passing:

panicked at crates/doctor/src/freshness.rs:822:9:
assertion `left == right` failed
  left: None
 right: Some("1.2.3")

Crates CI passed on the same crate sources before and after that run.

Cause

The tests are wrong; Doctor is not.

  • Each affected test writes a shell script with std::fs::write, marks it executable and has Doctor run it at once.
  • Linux refuses to execute a file that any process has open for writing (ETXTBSY, "Text file busy").
  • The tests run on parallel threads and most of them spawn processes. A child forked by another test while the script is open for writing inherits that descriptor and keeps it until its own exec. A script run in that window does not start.
  • Doctor reports a probe that could not start as a missing version or a failed check, which is the right answer for a tool that cannot be run. The test then fails on an assertion that names no cause.
  • macOS has no such check, so the failure never shows locally.

It is not the probe's 15 s limit: the CI log prints the failure within milliseconds of the test binary starting, and the whole suite finished in 6.1 s.

Fix

The fix removes the descriptor instead of waiting it out. There is no retry and no sleep.

  • New crates/doctor/src/test_support.rs with one write_executable: a child sh writes the file and the mode is set by path. The test binary never opens a script for writing, so there is nothing for a concurrent fork to inherit. The writer has exited before the helper returns.
  • It replaces the four copies of write_executable (freshness, agents, resolve, lib) and the three tests that wrote a script inline (checks, resolve, lib).
  • Two tests beside the helper: the contents are written byte for byte, and a script runs at once while two other threads spawn processes without a pause, which is the race forced.
  • No product code changes.

Before and after

Linux, Ubuntu 24.04 container with 16 CPUs, the test binary built from this tree before and after the change.

Runs Failed before Failed after
The CI test alone, 2000 runs 0 0
Whole suite, 8 loops side by side, 320 runs 13 0
Whole suite, 8 loops plus 16 busy loops, 320 runs 2 0
New forced test, 300 runs (before = the old in-process write put back into the helper) 295 0
  • The test alone never fails: it needs the other tests' forks.
  • The 15 failed suite runs were five tests, all of which run a script they wrote: checks::check_git_receives_env_snapshot (10), freshness::installed_version_uses_env_snapshot (2, the CI failure), two exec_command_with_*_env_snapshot* tests in lib and agents::unknown_auth_status_carries_login_command_without_a_fix (1 each).
  • With the spawn error printed, all 12 failures of the first two, which spawn the script directly, were Os { code: 26, kind: ExecutableFileBusy, message: "Text file busy" }. The other three run it through a shell, where a busy script is an exit status. The text of the two lib failures was not kept.
  • Forced, 2000 write-then-run rounds with the old helper: 0 busy with no other thread spawning, 25 with one, 102 with four.

cargo fmt --check, cargo clippy --workspace -- -D warnings and cargo test -p doctor (162 passed) pass on macOS. cargo test --workspace was not run locally.

Not in this change

Tests in crates/acp-client and crates/blox-cli write scripts the same way. They were not seen to fail and are left alone here.

🤖 Generated with Claude Code

…er busy when run

`freshness::tests::installed_version_uses_env_snapshot` failed once in
Crates CI (run 46581441992, on a PR that changes nothing under
crates/doctor) with `left: None, right: Some("1.2.3")`, the crate's other
159 tests passing. The request was to find the cause, fix it without a
retry or a longer wait, and fix the sibling tests that share it.

The cause is in the tests, not in Doctor. Each of them writes a shell
script with `std::fs::write`, marks it executable and has Doctor run it at
once. Linux refuses to execute a file that any process has open for
writing (ETXTBSY, "Text file busy"). The tests run on parallel threads and
most of them spawn processes: a child forked by another test while the
script is open for writing inherits that descriptor and keeps it until its
own exec. A script run in that window does not start. Doctor reports a
probe that could not start as a missing version or a failed check, which
is the right answer for a tool that cannot be run, so the test fails on an
assertion that names no cause. macOS has no such check, so it never shows
locally. It is not the probe's 15 s limit: the CI log prints the failure
within milliseconds of the test binary starting.

The fix removes the descriptor instead of waiting it out. A new
`test_support::write_executable` has a child `sh` write the file and sets
the mode by path, so the test binary never opens a script for writing and
there is nothing for a concurrent fork to inherit. The writer has exited
before the helper returns. It replaces the four copies of
`write_executable` (freshness, agents, resolve, lib) and the three tests
that wrote a script inline (checks, resolve, lib). No product code
changes.

Two tests are added beside the helper: the contents are written byte for
byte, and a script runs at once while two other threads spawn processes
without a pause, which is the race forced.

Measured on Linux (Ubuntu 24.04 container, 16 CPUs, the test binary built
from this tree before and after):

- The CI test alone, 2000 runs: 0 failed before, 0 after. It needs the
  other tests' forks.
- The whole suite, 8 loops side by side, 320 runs: 13 failed before, 0
  after. With 16 busy loops added for CPU contention, 320 runs: 2 failed
  before, 0 after.
- The 15 failed runs were five tests, all of which run a script they
  wrote: `checks::check_git_receives_env_snapshot` (10),
  `freshness::installed_version_uses_env_snapshot` (2, the CI failure),
  two `exec_command_with_*_env_snapshot*` tests in lib and
  `agents::unknown_auth_status_carries_login_command_without_a_fix` (1
  each). With the spawn error printed, all 12 failures of the first two,
  which spawn the script directly, were
  `Os { code: 26, kind: ExecutableFileBusy }`. The other three run it
  through a shell, where a busy script is an exit status; the agents
  failure read the shell's 126 as signed out. The text of the two lib
  failures was not kept.
- Forced, 2000 write-then-run rounds with the old helper: 0 busy with no
  other thread spawning, 25 with one, 102 with four.
- The new forced test with the old in-process write put back into the
  helper: 295 of 300 runs fail. With the fix: 0 of 300.

`cargo fmt --check`, `cargo clippy --workspace -- -D warnings` and
`cargo test -p doctor` (162 passed) pass on macOS.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T06:56:46.454757Z b274c1e PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

1 participant