Skip to content

fix: deflake //rs/tests/driver:unit_tests - #11880

Merged
basvandijk merged 1 commit into
masterfrom
ai/deflake-unit_tests-2026-10-16
Oct 16, 2026
Merged

basvandijk merged 1 commit into
masterfrom
ai/deflake-unit_tests-2026-10-16

Conversation

@basvandijk

Copy link
Copy Markdown
Collaborator

Root cause

driver::local_backend::tests::qemu_cmdline_names_pidfile_requires_the_pidfile_argument_pair (added in #11848) spawns three
sh -c 'read _' sh <args> children and then checks each child's /proc/<pid>/cmdline. Every failure is the same:

assertion `left == right` failed
  left: [false, false, false]
 right: [true, false, false]

Since the test landed on 2026-10-14 it failed in 3 of ~180 runs. Two were flaky runs on master (self-hosted runners). The
third was an RBE @ Namespace invocation on #11872 where all 3 attempts failed: it ran 599 of 698 tests on a cold cache,
with 200-330 actions running during the attempts. Its re-run, with at most 52 actions running, passed.

Command::spawn can return before the child's /proc/<pid>/cmdline shows its new argv:

  1. Rust's std spawns this Command with glibc's posix_spawnp, which uses clone3(CLONE_VM | CLONE_VFORK). So the parent
    resumes as soon as the child's execve wakes it.
  2. The kernel wakes it (exec_mmap() → exec_mm_release() → complete_vfork_done()) before it switches the child to
    its new mm. If the child is preempted right there, /proc/<pid>/cmdline reads the still-shared address space, i.e.
    the test's own argv.
  3. After the switch, get_mm_cmdline() returns nothing until create_elf_tables() has recorded where the new argv is.

The test reads child 1's cmdline only after spawning the other two, which normally gives child 1 time to finish its exec.
Under CPU contention child 1 can still be in one of these windows, and then it doesn't match.

Reproduction

On an idle devenv (Linux 6.8, glibc 2.39), reading /proc/<pid>/cmdline right after spawn() returned nothing in 1966
of 2000 spawns. Copies of the real test binary, run concurrently and pinned to a few CPUs, fail with exactly the CI
assertion:

contention before after
only this test, 8 copies on 2 CPUs 54 / 12000 0 / 12000
only this test, 16 copies on 4 CPUs 57 / 12000 0 / 12000
all 49 tests, 16 copies on 4 CPUs 7 / 640 0 / 640

A standalone copy of the test that dumps the cmdline on failure showed the parent's argv and comm every time (step 2).

Fix

Each child now runs echo; read _ with stdout piped, and the test waits for that newline before it reads the child's
cmdline. sh can only print once its exec has completed, so its argv is in place by then. echo is a builtin like
read, so the shell still doesn't fork. If sh exits before it prints, read_exact fails at that point instead of
producing the flake's assertion.

The production caller, stop_qemu, is not affected. It checks the cmdline of the QEMU that wrote the pid-file, at least
5 s after sending it SIGTERM.

This PR was created following the steps in .claude/skills/fix-flaky-tests/SKILL.md.

🤖 Generated with Claude Code

qemu_cmdline_names_pidfile_requires_the_pidfile_argument_pair read the
children's /proc/<pid>/cmdline right after spawning them. But
Command::spawn, via glibc's vfork-based posix_spawn, returns once the
child's execve wakes the parent, which the kernel does before it
switches the child to its new mm and records where the new argv is.
Until then the cmdline shows the test's own argv (the address space is
still shared) or nothing, so under CPU contention the child that names
the pid-file did not match.

Let each child echo a newline once sh runs and wait for it before
reading its cmdline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused test-only handshake addresses the startup race without changing production behavior.

0 open findings

What changed in this PR

Stabilizes the driver's command-line matching unit test by waiting for child-shell readiness before inspecting /proc, addressing the startup race introduced with #11848.

Changes:

  • Adds a piped stdout handshake to synchronize child startup.
  • Leaves production behavior and test assertions unchanged.
File Description
rs/​tests/​driver/​src/​driver/​local_backend.rs Waits for each shell's newline before checking its command line.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@basvandijk
basvandijk marked this pull request as ready for review October 16, 2026 16:33
@basvandijk
basvandijk requested a review from a team as a code owner October 16, 2026 16:33
@basvandijk
basvandijk added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit c5199ac Oct 16, 2026
44 checks passed
@basvandijk
basvandijk deleted the ai/deflake-unit_tests-2026-10-16 branch October 16, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants