Skip to content

test: fix three flakes from tests racing each other in one process - #181

Merged
backnotprop merged 6 commits into
plannotator:mainfrom
sebastianst:seb/rex-agent-flake
Nov 22, 2026
Merged

backnotprop merged 6 commits into
plannotator:mainfrom
sebastianst:seb/rex-agent-flake

Conversation

@sebastianst

@sebastianst sebastianst commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three test flakes, all from tests racing each other inside one process:

  • Text file busy (rex::agent mostly, also rex::agents, hc::*, app::pi_target, review::tests::*): fixtures wrote a fake command with std::fs::write and then ran it. When another test thread forked in that moment, the child inherited the open write handle until it exec'd, and running the script failed with Text file busy (os error 26) meanwhile. test_support::write_script now has /bin/sh write the file, so the test process never holds it open; the review crate and the integration tests (tests/support) get the same helper. rex::agent alone went from 13 failures in 1000 runs to 0.
  • hangup::tests::piped_stdin_is_passed_over_for_the_terminal: a child forked by another test holds a copy of the pipe's writer until it execs, so the pipe hadn't hung up yet when checked. A socket pair with shutdown(Both) hangs up regardless of copies. In the same module, pty() opens /dev/ptmx close-on-exec rather than setting it after openpt, which left a gap for a child to keep the controller.
  • pi::tests::a_closed_session_is_gone_and_its_entry_removed: the "closed" port was bound and released, so a parallel test's bind(0) could be given it. It's now port 1, below every ephemeral range, so bind(0) never hands it out.

AGENTS.md gets a section on writing tests that share one process, so new tests avoid these races.

The full plannotator-tui suite failed on 4–7% of runs on main; with these, 0 in 200.

The hangup changes are Unix-only and untested on macOS, which only post-merge CI covers: they rely on a shut-down Unix socket reporting POLLHUP and on /dev/ptmx working with grantpt/unlockpt, as on Linux.

🤖 Co-created with Claude Opus 5.5

sebastianst and others added 4 commits November 20, 2026 09:23
…hits text file busy

Tests that fake `rex`, `herdr`, `hc`, `gh` and `herdr-app` wrote the script with
std::fs::write and then ran it. A fork on another test thread while the file was
open inherited the write handle until that child exec'd, and exec of the script
failed with ETXTBSY in the meantime: rex::agent's tests failed this way about 1 in
80 runs of the module alone. `test_support::write_script` has /bin/sh write the
file, so this process never holds it open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lds its writer

The piped-stdin test closed a pipe writer and asserted the reader hung up.
A child that another test is spawning holds a copy of every descriptor
between fork and exec, so the writer could outlive the drop and the pipe
not hang up yet. A socket pair stands in: shutting the writer down hangs
the reader up however many copies stay open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pty() opened the controller with openpt and set close-on-exec afterwards.
A child that another test spawned in between (e.g. the delivery test's
`sleep 5 &`) kept the controller past its exec, so the tty did not hang
up within the neighbouring test's 5 s deadline. Opening /dev/ptmx with
O_CLOEXEC closes that window on Linux and macOS alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
closed_port() bound port 0 and released it, so a test running alongside could be given the same port and find the "closed" session answering. Port 1 lies below every ephemeral range.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sebastianst sebastianst changed the title test: write fake commands from a child process, so running one never hits text file busy test: fix three flakes from tests racing each other in one process Nov 20, 2026
sebastianst and others added 2 commits November 20, 2026 10:05
… written from a child too

The same text file busy race as in the app's unit tests: the review crate's fake gh, and the fake gh, rex, hc and herdr-app of tests/pr_review.rs and tests/rex_hc_review.rs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@backnotprop
backnotprop added this pull request to the merge queue Nov 22, 2026
Merged via the queue into plannotator:main with commit a059de2 Nov 22, 2026
3 checks passed
@sebastianst
sebastianst deleted the seb/rex-agent-flake branch November 23, 2026 14:52
@backnotprop backnotprop mentioned this pull request Nov 23, 2026
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.

2 participants