Skip to content

Stop macOS timing flakes in integration tests - #18

Open
tony wants to merge 17 commits into
masterfrom
flaky-investigations-000
Open

tony wants to merge 17 commits into
masterfrom
flaky-investigations-000

Conversation

@tony

@tony tony commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Eleven macos-15 compatibility jobs in the September–October investigation failed on unrelated commits while waiting for process startup or fixture handoff. Linux passed at the same commits. This PR gives event-driven waits a 10-second hang guard, replaces one 0.1-second race with an advancing clock and adds a repeat-run workflow.

Root causes

Failure Cause Fix
MCPMutationTest#test_one_deadline_covers_acquisition_and_mutation_and_preserves_unknown_effects, "command deadline exceeded" The request had 0.1 s of real time to snapshot, run tmux preflights and reach a held hook. A slow spawn expired it before the hook fired. A long request budget, and the mutation clock is advanced so 1 s of real time remains for the held command. Assertions are unchanged.
MCPEnrollmentTest authored-run tests, same error registry.run(..., timeout: 0.5) covered helper startup, a Ruby process start. Bound raised to HANG_GUARD_SECONDS.
"tmux fixture exceeded event deadline" TmuxFixture bounded daemon readiness and each client at 0.5 s. Fixture deadline raised to HANG_GUARD_SECONDS.
"attached client did not draw its terminal" IO.select on the pty master, 0.5 s. Bound raised to HANG_GUARD_SECONDS.
"installed MCP did not return a frame" The original example used a one-second read. The released example now waits for complete frames. Give the framed response and shutdown waits ten seconds.

HANG_GUARD_SECONDS (10 s) is in test/test_helper.rb. Tests that prove a deadline elapsed keep their short real or offset-clock deadlines (0.02 s transport limits, OperationBudgetTest, the never-signalled wait-for). The two transport tests that expire a request by clock offset now offset by the guard.

Evidence

The December 5 rebase uses the v0.1.3 trunk commit. On Linux arm64, Ruby 4.0.7 and tmux 3.8 passed 369 unit, 344 integration and 8 installed-gem tests with zero skips. The outer check also validated types, checked generated examples and rendered 184 documentation pages. It took 128 seconds. Tested head

One stress repetition exposed a separate reporting defect. A Ruby warning interrupted a verbose Minitest line, so the original parser recorded 363 of 364 tests. The runner now parses test output separately from diagnostics and checks its count against Minitest’s total. A fresh Linux tmux 3.7c run recorded all 364 rows, including the process deadline. That case deliberately waits two seconds for a dispatched child to time out and checks that it was reaped. It now runs in the outer suite. Deadline test Original parser · Reporting fix

Original branch measurements

  • Linux, a tmux shim on PATH sleeping before each exec: the old mutation test fails at 10 ms per spawn; the new one passes at 100 ms.
  • The original branch passed the then-current local mid and outer checks.
  • Stress run, 50 repetitions of the 188 integration tests (tmux 3.7c, Ruby 4.0.7) (run): macos-15 0 failures in 50 repetitions, ubuntu-24.04 0 failures in 50. A macOS repetition took 27-46 s, a Linux one about 11 s.
  • Delay shim (a tmux wrapper that sleeps before each exec): at 0.6 s per spawn the old 0.5 s fixture bound fails all 9 tests in server_test.rb with "tmux fixture exceeded event deadline" and the 10 s guard passes all 9. The installed examples fail at the old bounds from 0.1 s per spawn and pass with the new ones; the new 5 s example waits fail from 0.2 s per spawn. The 1.0 s frame wait was not isolated from the 0.5 s waits around it.
Before (steady, 2026-09-20 to 10-04) After (stress, 50 x 188 tests)
macOS jobs failing 11 of 966 steady jobs (1.1%) 0 of 50 repetitions (9,400 test runs)

At the historical 1.1% per-job failure rate, 50 clean repetitions would occur about 58% of the time (0.989^50). The recorded stress run bounds what that sample can establish. The delay-shim experiments above exercised the timing change directly.

macOS guide

.github/MACOS_CI.md documents event waits, private socket paths and terminal behavior. The fixture reports an overlong path before starting its private server. The released compatibility jobs run on Ubuntu. A required macOS compatibility lane is tracked in #47.

Stress workflow

The proposed weekly or manually dispatched workflow runs macOS-15 and Ubuntu 24.04 in sequence with tmux 3.7c. bundle exec scripts/stress --repeat 10 repeats the integration suite and process tests and writes per-test runs, failure counts and slowest times.

@tony
tony force-pushed the flaky-investigations-000 branch 3 times, most recently from 6e9988e to f69e0b2 Compare October 4, 2026 13:48
@tony
tony force-pushed the flaky-investigations-000 branch from f69e0b2 to ccda1cb Compare October 4, 2026 16:01
@tony
tony force-pushed the flaky-investigations-000 branch from 86aff61 to 5cc1805 Compare October 4, 2026 21:25
tony added 3 commits December 5, 2026 11:55
why: Eleven macOS-15 CI runs from 09-26 to 10-04 failed on unrelated
commits with a wait that gave up at 0.5 or 1.0 seconds: "tmux fixture
exceeded event deadline", "attached client did not draw its terminal",
"installed MCP did not return a frame" and a DeadlineExceeded inside
MCPEnrollmentTest. Each wait already returns on an event (pipe
readable, wait-for signal, thread join); only its bound was sized for
a fast machine, and a tmux spawn or Ruby start on the hosted macOS
runner can exceed it. The same tests pass on Linux at the same shas.

what:
- Add HANG_GUARD_SECONDS to test/test_helper.rb and use it for event
  waits in integration tests and TmuxFixture, which now reads it for its
  command and readiness deadline and for reap waits
- Advance the transport clock by the guard in the two tests that expire
  a request deadline by clock offset, so they still expire it
- Raise the same bounds in the shipped examples and their copies in
  docs/recipes.md and the libtmux-async README
- Leave tests that prove a deadline elapsed (0.02 s transport limits,
  OperationBudgetTest's offset clock, a never-signalled wait-for)
why: test_one_deadline_covers_acquisition_and_mutation_and_preserves_
unknown_effects gave the request 0.1 s of real time to take a snapshot,
run tmux's preflight commands and reach a held after-new-window hook.
When that took longer on macOS-15 the deadline expired first, the hook
never fired and the test raised "command deadline exceeded" from its own
wait-for (CI jobs 108738309513, 108738309552, 111211994832). With a tmux
shim on PATH that sleeps 10 ms per spawn, the old test fails; it
cannot tell slow startup from an enforced deadline.

what:
- Give the request a long budget and advance Mutation's clock after
  target acquisition (or before it, for the not-sent case), so exactly
  1 s of real time remains for the held command
- Keep the assertions: the mutation budget is below the acquisition
  budget, both share one cancel token, delivery is possibly_sent with
  unknown effects after dispatch and not_sent with none before it
- Wait on the wait-for channels and the pending request with the hang
  guard
- With the same shim at 100 ms per spawn the new test passes
why: A macOS flake shows up once in about a hundred runs, so a single
green pass proves nothing. A scheduled repeat gives each test a failure
rate and an Ubuntu control to compare against.

what:
- Add scripts/stress: runs the real-tmux integration suite N times and
  writes each test's run and failure counts to failures.csv and a
  Markdown table
- Add stress.yml: weekly and on dispatch (input repeat, default 10),
  one job per runner, macOS-15 and Ubuntu 24.04, tmux 3.7c, the table in
  the step summary and the CSV and logs as an artifact
@tony tony mentioned this pull request Dec 5, 2026
@tony
tony force-pushed the flaky-investigations-000 branch from 5cc1805 to 780166c Compare December 5, 2026 18:08
tony added 14 commits December 5, 2026 12:17
why: Darwin's sockaddr_un.sun_path holds 104 bytes, and the macOS
temporary directory alone is long. An over-long path made tmux fail
with an error that did not name the cause.

what:
- Make TmuxFixture raise a named error for a socket path of 104 bytes
  or more and remove its directory
- Test a too-long TMPDIR against a short control
why: Tests that pass on Linux failed on the macOS runner through
timing, path and process differences. Contributors need the rules and
a way to reproduce each on Linux.

what:
- Add .github/MACOS_CI.md and link it from CONTRIBUTING.md
- List the helpers this repository uses to follow it
why: `with_scope` wrapped every async test in `with_timeout(0.8)`.
That one bound covers ruby child spawns, 32-request admission,
control opens and `Async.open`. A slow process start on a loaded
runner expired it, so tests failed with `Async::TimeoutError`
although nothing hung.

what:
- Replace the 0.8 s bound with `HANG_GUARD_SECONDS` (10 s).
- Tests still return as soon as their events happen; the bound
  fires only when a test hangs.

Evidence: pinned to one CPU beside 40 busy loops, async_test had
1 failure and 12 errors in 22 tests before this series and none
after it. The slowest macOS async test in the 50-repetition stress
run (run 37201292911) took 0.29 s, so 10 s is more than 30x it.
why: `budget` capped every benchmark `timeout:` at 0.5 s, including
`Server.start`, session creation, `wait_for` readiness and cleanup
waits. A slow tmux or ruby start aborted the run with a deadline
error. The benchmark times its own phases, so the cap only guards
against hangs.

what:
- Raise `DEADLINE` from 0.5 s to 10 s.
- Raise the 0.2 s `IO.select` that waits for the pipelined second
  request to be written to `DEADLINE`.
why: Several tests put a 0.08 to 1 s deadline around a step that
starts a ruby or tmux client. On a loaded or slow-spawning runner
the deadline fired before the process started, so the test failed
on a hang-guard error, not on the behavior it checks. None of
these tests asserts how fast a process starts.

what:
- Cleanup budgets that retire a TERM-ignoring child and assert
  empty `cleanup_errors` or a reaped pid use `HANG_GUARD_SECONDS`:
  two async_test tests (0.08 s) and one process_test test (0.1 s).
  The map-failure test also asserts ECHILD after cleanup, so it is
  not a deadline test and leaves the 0.08 s group.
- `link.select` that must reach a tmux hook before the test
  cancels it (operation_budget_test, 0.5 s) and the terminal
  attach that covers client start (terminal_test, 1 s) use
  `HANG_GUARD_SECONDS`.
- The installed MCP helper protocol deadline, executor timeout,
  connect wait and owner join (package_test, 0.5 s) share one
  `hang_guard` value passed in from `HANG_GUARD_SECONDS`.
- The whole-command deadline on a ruby child that must write READY
  first (process_test, 0.1 s) becomes 2 s, and the join after it
  uses `HANG_GUARD_SECONDS`. The test checks that a deadline after
  dispatch reports possible effects. The executor has no
  injectable clock, so the deadline stays real; 2 s keeps the test
  short while leaving the child time to start.

Evidence: with a `tmux` shim that sleeps before each exec, the
operation_budget test errors at 0.7 s and 1.2 s per spawn before
and passes after, and the terminal attach test errors at both
delays before and passes after.
why: Joins and selects that wait for a spawned ruby child to start,
connect, exit or be reaped used a 0.5 s literal beside
`HANG_GUARD_SECONDS`, so the shared guard never applied to them.
Each wait returns when its event arrives; the literal only failed
a test whose child started late.

what:
- process_test: 16 asserted joins and selects, and the READY
  `read_event` deadline, move from 0.5 s to `HANG_GUARD_SECONDS`.
- operation_budget_test: the join after cancelling a blocked
  preflight moves from 0.5 s to `HANG_GUARD_SECONDS`.
- Joins inside `ensure` blocks stay at 0.5 s: they only release
  threads after a failure and assert nothing.

Evidence: pinned to one CPU beside 40 busy loops, process_test had
11 failures in 21 tests before this series and none after it.
why: The shipped examples bounded readiness, held requests and
process exits at 1 or 5 s. Each wait returns as soon as its event
arrives, so the bound only fails a hung example, yet a slow tmux
or ruby start on a loaded machine could still exhaust it. Readers
copy these numbers, so they should be sized for the slowest start.

what:
- Raise every positive wait in examples/ from 1 or 5 s to 10 s:
  control exchanges, `wait_for`, held requests, `output.next`,
  MCP frame reads, shell connect and setup, and the MCP process
  exit.
- Update the "five seconds" error text in api/capture.rb.
- Regenerate the recipe and async README excerpts with
  `scripts/examples --write`.
- With a `tmux` shim that sleeps 1.2 s before each exec,
  api/capture.rb fails with a control request deadline before this
  change and passes after.
why: Sizing a hang guard needs the slowest observed duration of the
steps it bounds. The stress logs held per-test durations but the
CSV and summary dropped them, and the unit tests that spawn ruby
children never ran on macOS under repetition.

what:
- Add a `slowest_seconds` column to `failures.csv`.
- Add a table of the five slowest tests to `summary.md`.
- Include `test/unit/process_test.rb` in each repetition.
why: The paused-control test refused to see "async-missed" in the
resumed stream, and failed about 3 runs in 30 on Linux. The pane
runs `cat`, so each sent line produces two outputs: the tty echo,
then cat's reply. The observer wait broke on the first copy and
the test resumed the pane. The second copy then reached tmux after
`%continue`, which sets the pane offset to the current end of
output (`control_continue_pane`, tmux 3.7c control.c), so it was
delivered to the resumed stream ahead of "async-resumed".
Output written while the pane is paused is dropped
(`control_pause_pane` discards queued blocks), so the library and
tmux behave as the test expects; only the ordering of the test's
own synchronization was wrong.

what:
- Collect the observer's output until both copies of
  "async-missed" have arrived, then resume the pane.
- No sleep and no timing change; the wait still ends on the event
  and stays bounded by `HANG_GUARD_SECONDS`.

Evidence: the old wait failed this test in 3 of 30 runs. A copy
of the test whose pane echoes immediately and replies to
"async-missed" 0.3 s later failed 3 of 3 runs with the leak
before this change and passed 3 of 3 after. With the default cat
pane as a control, the new wait failed 0 of 40 runs.
why: `ReleaseTest` builds a git repository, commits, then copies the
whole tree with `FileUtils.cp_r`. Since git runs automatic
maintenance after a commit, detached by default
(`maintenance.autoDetach`), a background process can hold
`.git/objects/maintenance.lock` (taken in builtin/gc.c before the
detach) and remove it while the copy is walking the directory. The
copy then fails with `ENOENT` on that file and 12 `ReleaseTest`
cases error with "release source git check failed". It failed once
on linux / Ruby 3.4.10 / tmux 3.7c in PR 18 (runner git 2.55.0).
Release is an unrelated suite; nothing in this series touches it.

what:
- Set `maintenance.auto=false` and `gc.auto=0` in the fixture
  repository's config before its first commit, so no maintenance
  process starts in it.

Evidence: with git 2.55.0, a commit spawned `git maintenance run
--auto --quiet --detach` under the default config and none with
the two settings. A seam that creates and deletes the lock during
the copy failed 67 and 72 of 200 copies with the CI error text; the
same copy with no lock activity failed 0 of 200. The race window
under real maintenance was too short to hit locally (0 of 300
copies, also under 8 busy loops), so the seam stands in for it.
Sources: git v2.55.0 builtin/gc.c lines 1791-1818 (lock, then
detach) and run-command.c lines 1956-1985 (`--detach` unless
autoDetach is false).
why:
Current lint rejects unfreezing an interpolated string. Contributors
also need the standalone stress command's supported options.

what:
Use the interpolated summary directly and document the invocation
at the top of the script.
why:
A Ruby warning interrupted a verbose Minitest line and the CSV
silently omitted that test.

what:
Parse Minitest's output separately from diagnostics. Check the rows
against the run total and keep both streams in the log.
why: The post-dispatch case waits for a real Ruby child to reach its
deadline, so it belongs in the outer suite.

what:
- Run the deadline case in integration through the normal executor.
- Check the child PID, possible delivery and complete reaping.
why:
Contributors need to know how the integration runs report slow and
failing tests.

what:
Record the longer event waits and weekly stress report under 0.1.4.
@tony
tony force-pushed the flaky-investigations-000 branch from 780166c to 5012c40 Compare December 5, 2026 18:21

This branch was previously deployed

1 inactive (outdated) deployment
docs-preview — ccda1cb7 Deployed Oct 4, 2026 by tony via publish (pr-18, pr, false, docs-preview, true) / publish #50
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