Skip to content

Make the doctor stop test catch a stop path that does not stop first - #2284

Merged
fazxes merged 4 commits into
mainfrom
fazxes/doctor-stop-test-listing-hook
Nov 25, 2026
Merged

fazxes merged 4 commits into
mainfrom
fazxes/doctor-stop-test-listing-hook

Conversation

@fazxes

@fazxes fazxes commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The test "stopping a check that keeps starting processes in groups of their own leaves none of them" is meant to prove that doctor's stop path stops a check's process group before it lists what the check started. #2012's mutant of the stop path, with no group SIGSTOP and no second listing (the 100 ms wait and stopAll kept), passed it most of the time. This change makes the test fail on that mutant in every run, in ReleaseSafe and Debug, quiet and loaded, and the test passes on macOS. It changes no product behavior.

Why the old test missed the mutant

Only a process started after the first listing and moved out of the check's group before the check itself stops can escape. That window is the listing: about 28 processes, 0.7 to 2.1 ms in ReleaseSafe and 1.3 to 2.4 ms in Debug. Each child was a new perl process, which took about 2 ms (median, quiet) to start and leave its group, and the forker started one about every 3 ms. Under load a perl start takes several times longer.

On 2 pinned CPUs, with 6 busy loops for "loaded", on unchanged main:

Build Quiet Loaded
ReleaseSafe 22 of 200 0 of 100
Debug 40 of 100 0 of 100

A forker that only started processes faster did not fix it. It caught every run when quiet, but only 69 of 100 in ReleaseSafe and 94 of 100 in Debug when loaded. A trace of 200 loaded rounds found no process started inside the window in 154 of them in ReleaseSafe and in 123 in Debug: the window is a few milliseconds, and a forker that sleeps between starts is often not on a CPU in it. Nothing outside the stop path can see that the listing is done.

What changes

  • killTree calls test_after_listing once, after the first listing. The variable is typed like test_defer in delivery_test_controls.zig: if (builtin.is_test) ?*const fn (root: std.posix.pid_t) void else void. The call is under if (builtin.is_test), so other builds compile both out. Its doc line names the test. It is not pub, because only this file uses it. It adds no env var and no file control.
  • The test sets the hook to holdUntilForkerMovesMore. The hook returns at once when ProcessProbe.stop_landed is set, which stopTrees sets with processIsStopped on Linux and macOS, so the real stop path never waits in it. When the stop path left the forker running, the hook empties the named pipe, then waits with poll(), at most 5 s, for two more bytes. The forker writes the second byte only after it started a process after the first byte and moved it out of its group, so that process is one the listing missed. R7.3 holds: it waits on a descriptor, not on a sleep.
  • The forker is one perl process. Per child it forks, writes the pid file, moves the child with setpgrp($pid, $pid), and writes a byte to a named pipe, then pauses 1 ms. The pid file is written first, so a process outside the group is always one the test knows.
  • The named pipe is a FIFO the test makes in its own temp folder before the forker starts, with the read end open and non-blocking. The forker opens it by path with O_NONBLOCK (sysopen) and clears the flag again right after, so its one-byte writes block as before. With the flag set, an open with no reader fails with ENXIO and the forker ends, where a blocking open would wait for a reader forever (see "If the test dies mid-round"). Each round closes the read end and removes the FIFO's entry on every path that leaves it, including a failed ProcessProbe.create.
  • Kept as they were: the 200-process cap, the five rounds, the watchdog, and the cleanup that never signals a reaped pid.

Why the hook alone is not enough with the old forker

I built the hook with the old forker too (a shell loop that starts a perl per child, each moving itself and writing a byte; an inherited pipe, on Linux). It catches the mutant 100 of 100 in every config, but the real stop path waits in the hook. With a count printed when the hook starts to wait, over 100 loaded runs of 5 rounds:

Build, CPUs under load Rounds that waited in the hook
ReleaseSafe, 2 82 of 500
ReleaseSafe, 1 10 of 500
Debug, 2 81 of 500
Debug, 1 16 of 500

Each wait ended at the 5 s bound (runs took up to 20.6 s). A forker whose children move themselves asynchronously cannot say when a process started after the listing is out of the group. The single forker has a small group of its own, so its stop lands in time.

The macOS failure and its fix

The first version failed macOS arm64 unit tests in both macOS runs (45487664407 and 45487667022) with ... leaves none of them' failed without output. I reproduced it on an arm64 Mac (macOS 27.2.1, Apple perl 5.34.1) with a diagnostic copy of the test, built in a sandbox and run only against the real stop code.

  • The check that failed is expect(started_one) in round 0, not probe.done or started > 0. Perl's open(W, ">&=", 6) failed with "Bad file descriptor", so the forker exited at once, no pid file appeared in 5.8 s, and the test returned before it reached the other checks.
  • The pipe's write end never reached the forker. A plain /bin/sh and a plain /usr/bin/perl, started the same way as ProcessProbe.spawn starts a check, see only descriptors 0, 1 and 2, with the pipe open. The write end was not close-on-exec (F_GETFD returned 0, as on Linux), and clearing the flag changed nothing. On Linux an extra descriptor is inherited, which is why only macOS failed. I read the 0.17.1 source: POSIX_SPAWN_CLOEXEC_DEFAULT is not set. I did not find which layer drops the descriptor, so the cause stated here is what I measured, not a reading of the source.
  • The fix is the named pipe: the forker opens it by path, which needs no inherited descriptor.
  • macOS poll reports nothing when a FIFO's writer closes, where Linux reports POLLHUP. The hook then waits for its 5 s bound, not for the end of the pipe, which matters only for a forker that dies before it writes two bytes.

If the test dies mid-round

If the test process is killed after it starts the forker and before the forker opens the FIFO, nothing holds the read end any more. With a blocking open, that forker waits in wait_for_partner for a reader that never comes. I killed the test (SIGKILL) as soon as its forker appeared, in a sandbox, and counted the forkers blocked in the FIFO open:

Killed test 1 s after the last kill 41 s after
Previous commit, 100 kills at varying moments (0 to 5 ms after the forker appears) 61 61
This commit, the same 100 kills 0 0
Previous commit, forker sleeps 0.3 s before the open, 20 kills 20 20
This commit, the same 20 kills 0 0

One second after the kills, this commit still showed 41 sleep 30 children of forkers that had already started. They are not blocked, and none was left at 41 s. A killed run still leaves its FIFO and pid files in its temp folder, like the pid files a killed run leaves on main. This change does not remove them.

Proof

All Linux runs are in sandboxes with Zig 0.17.1, on origin/main (4ffe877160) plus this branch's diff, which is what CI merges. A catch is a failing run.

Run ReleaseSafe quiet ReleaseSafe loaded Debug quiet Debug loaded
Real code passes 300 of 300 300 of 300 300 of 300 300 of 300
Mutant m2012 caught 100 of 100 100 of 100 100 of 100 100 of 100
Plant: no outside kill caught 100 of 100 100 of 100 100 of 100 100 of 100
Plant: group-only kill caught 100 of 100 100 of 100 100 of 100 100 of 100
Plant: group kill planted out caught 30 of 30 30 of 30 30 of 30 30 of 30
  • Nothing was left after any run: after each run the driver counted what the test started and found 0, and the sandbox had no perl, sleep or tmux left at the end.
  • The hook never waited in the real code: 0 waits in all 1200 runs above. In the mutant it waited in 500 of 500 rounds in each config, and gave up in none.
  • The group-kill plant ends each run through the watchdog message, with a peak of 198 to 200 processes, so the cap holds.
  • macOS, on the Mac, real stop code only, 20 runs in a row with ulimit -u, timeout -k 5 60 and FX_SOUND=0: 20 of 20 pass, each under 1 s, and 0 processes of the test left after any run. The same on each of the last three commits.
  • The Build & Test job's steps, as origin/main's ci.yml lists them, run in a sandbox on that tree: formatting, PGSO, the public-surface and four boundary scripts, the five model checks and the four drivers that walk them, CLI shape, the CI and zig-skill unittests, zig build, and the direct-write audit (classified=113 unclassified=0) all exit 0. All six CI build surfaces exit 0. check_deprecated.py src build.zig benchmarks tests exits 0 (0 removed, 0 deprecated).
  • The full unfiltered zig build test -Doptimize=ReleaseSafe --summary all on that tree: 13056 of 13100 passed, 12 failed, 8 distinct names. They are the same names as in the clean origin/main run on the earlier tip f473287ddb (13039 of 13083, 12 failed): file-permission tests that fail as root, and one capture-store timing test. Two earlier full runs on an older base showed up to two more failing names and one crash (the terminal client, auto_upgrade, and a second capture-store timing test). They did not show again in reruns, so I take them for flakes, but I did not isolate their cause.
  • Byte identity: on the previous commit's file, the ReleaseSafe fx, the same with -Dcpu=baseline, both Wasm builds and the four N-API targets were byte-identical to the ones built from origin/main's doctor_probes.zig, in the same sandbox and clone (8 of 8). This commit changes only the perl script text and a comment inside the test, so no product line changes. I did not rerun that check on it.

Not verified

  • CI on the new head. The macOS arm64 unit tests job takes about 30 minutes.
  • The killed-run check ran on Linux only. A write-only open of a FIFO with no reader fails with ENXIO on every POSIX system, but I did not run it on the Mac.
  • The mutant and the plants on macOS: the Mac runs used the real stop code only.
  • TSan: no thread of the test changed. The hook runs on the thread that calls stopTrees, as stop_landed is documented to.

The test "stopping a check that keeps starting processes in groups of
their own leaves none of them" is meant to prove that the stop path stops
a check's process group before it lists what the check started. It caught
the stop path with no group SIGSTOP and no second listing in 22 of 200
ReleaseSafe runs, and in none under load. Only a process started between
the first listing and the check's own stop escapes, a window of about a
millisecond or two, and each child was a perl that took longer than that
to leave its group.

A hook in killTree, called once after the first listing, now holds the
stop path until the forker has moved two more processes out of its group.
It exists only in test builds: the variable is void and the call is
compiled out otherwise. The forker is one perl process that records each
child, moves it with setpgrp itself, and writes a byte to a pipe, so a
byte says the process is out of the group. The hook waits on that pipe
with poll(), at most five seconds, and returns at once when the stop has
landed, as it has in the real stop path.

The 200-process cap, the watchdog and the cleanup that never signals a
reaped pid are unchanged.
@fazxes fazxes added type: maintenance Changes internal tooling, dependencies, CI, or implementation structure ci:macos Run macOS arm64 checks on this PR labels Nov 24, 2026
On macOS the forker test failed without output. Perl's open of the
pipe's write end by descriptor number failed with a bad descriptor, so
the forker exited at once, no pid file appeared, and the test returned at
its check that a process started, after the five second wait.

std.process.spawn leaves a child on macOS only stdin, stdout and stderr,
even for a descriptor that is not close-on-exec, so the forker cannot be
handed an open one. The test now makes a named pipe in its temp folder
before the forker starts and holds the read end open without blocking.
The forker opens the pipe by its path. The hook, the product lines, the
200-process cap, the watchdog and the cleanup are as they were.
std.fmt.bufPrintSentinel is deprecated, and the check for deprecated Zig
declarations in CI rejects it. The test now builds the pipe's path with
std.mem.printSentinel.

Each round also removes the named pipe's entry itself, on every path
that leaves the round, beside the close of its read end, instead of
leaving the entry to the cleanup of the test's folder.
If the test process dies after it starts the forker and before the
forker opens the pipe, nothing holds the read end any more, and the
forker's blocking open waits for a reader that never comes.

The forker now opens the pipe with O_NONBLOCK, so an open with no reader
fails with ENXIO and the forker ends. It clears the flag again right
after, so its one-byte writes block as they did.
@fazxes
fazxes marked this pull request as ready for review November 25, 2026 05:59
@fazxes
fazxes merged commit 8096378 into main Nov 25, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:macos Run macOS arm64 checks on this PR type: maintenance Changes internal tooling, dependencies, CI, or implementation structure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant