Skip to content

fix: make four intermittently failing Rust tests deterministic - #106

Open
teo-goulois wants to merge 5 commits into
mainfrom
fix/flaky-rust-tests
Open

teo-goulois wants to merge 5 commits into
mainfrom
fix/flaky-rust-tests

Conversation

@teo-goulois

Copy link
Copy Markdown
Contributor

Four tests failed now and then on CI (around #100 and #101), then passed on rerun. Each one comes from a real race. Three of the races are in production code.

Root causes

tests/inventory.rs — the_daemon_reports_each_state_and_revision_of_a_service (Ubuntu, inventory.rs:309, None.unwrap())

The inventory epoch is 16 random bytes encoded as base64url, so about one epoch in 64 starts with -. clap then read localify inventory --have -1aB…:3 as an unknown option, and the command failed with error: unexpected argument '-1' found. I caught that exact output with the test instrumented. The same failure was already on main on Nov 9 (run 42693843108). Fix: allow_hyphen_values on --have, plus a parse test.

While looking into it I also found and fixed a second race in the daemon. The DNS server bound its sockets in a task of its own while the management socket was already answering. If another program took the DNS port in between, the daemon answered status/inventory and then exited. The DNS sockets are now bound before anything is served, so such a start fails before anyone sees the daemon up, and the error names the port.

tests/remote_controls.rs — a_lost_acknowledgement_is_reported_exactly (Ubuntu, "timed out waiting for B registered")

An executor records its exit (launch record, then service record), then releases the service's lock. A launch arriving in between found the lock held and gave up with 'web' is already running (PID …), though the run had already ended. B never registered, and the test waited 30 s. Fix (admit): while the record or the launch record holds the run's exit and its executor still runs, the new launch waits for the lock instead of refusing. It waits up to the existing RELEASE_TIMEOUT of 10 s, the same wait as after a stop. Unit test added. User-visible: localify dev (or a remote/app start) issued right after a run ended no longer fails with "already running".

src/cli/service_supervisor.rs — a_worker_that_ignores_ctrl_c_is_stopped_after_its_launcher_exits (macOS, "the worker survived its launcher")

supervise decides whether to scan before it reads the Ctrl+C count. A press landing in between was handled with the previous scan, which could be from before the service forked its worker. Only the launcher got the SIGINT. The launcher exited, the worker was orphaned outside the tracked tree, and it was never stopped. Fix: the first interrupt rescans the tree itself. User-visible: a child that a service spawns right before Ctrl+C is now stopped with it instead of being left running.

src/infrastructure/runs/output.rs — the_finalize_lock_is_exclusive_and_never_recreates_a_folder (macOS, on main run 45164540766)

On macOS a flock belongs to the open file description. A child forked by another test in the same process keeps that description until it calls exec, so right after drop(held) the lock can still be held for a moment. I confirmed this with a fork/flock script. The test, and the_lock_admits_one_executor_at_a_time, which relies on the same thing, now wait for the release instead of asserting it at once. Production callers already wait (acquire_within, CLEANUP_WAIT).

Reproduction, before → after

Natural failure rates are low: the tests passed 0/48 at rest on this Mac. So I reproduced each race deterministically by injecting a delay into the window, then ran the fixed code under load. All injected delays are removed.

Test Before After
inventory 1/40 and 1/60 under load (≈1/64, the hyphen epoch); with a DNS-bind delay, the old daemon answered 14–15 inventories and then exited 0/80 under load; with the same delay the new daemon answers 0 times and fails cleanly
remote_controls fails every time with a 1.5 s delay before the executor releases its lock ('web' is already running) passes with the same delay; 0/40 under load
service_supervisor 10/10 failures with a 150 ms delay between the scan decision and the stop handling 0/10 with the same delay; 0/100 under load
finalize lock flock-held-after-fork confirmed 0 failures across 15 full runs of the binary's unit suite

"Under load" here means 4 copies in parallel on an 8-core Mac whose load average ran between 60 and 400 from unrelated Xcode/simulator builds.

Also touched

  • tests/runs.rs — a_stop_whose_executor_dies_goes_on_only_under_the_lock failed once locally at load ≈370: the SIGTERM comes after a journal write, which a saturated disk delayed past the 5 s guard. The guard is now 30 s, like the other waits in that file. It only bounds a hang; the timing assertions are unchanged.

Other flaky tests seen, not fixed

  • Under extreme local load (≈200–300), daemon::remote::tests::viewing::{a_large_follow_stays_inside_the_relays_budgets, a_viewer_flooding_gets_and_reads_keeps_the_target_bounded, a_viewer_gets_every_page_then_unchanged}, daemon::remote::tests::cli::following_goes_on_with_the_run_that_replaces_a_restarted_one and daemon::public_tunnel::tests::shutdown_kills_cloudflared failed in 7 of 15 full runs, mostly with not_responding ("It stopped answering") from the fake relay. They have not failed on CI. The cause is not clear or small enough to fix here.
  • Seen in older CI history (Nov 9): tests/app_bridge.rs a_launch_handed_to_the_app_runs_in_a_tab_and_its_restart_takes_that_tab_over (twice), desktop::app_bridge::tests::connections_past_the_limit_are_closed_and_the_bridge_goes_on, tests/run_pty.rs a_killed_executor_s_run_is_ended_and_finalized_complete_by_the_app, daemon::remote::tests::reconnecting_does_not_reset_the_pairing_limit. Not investigated here.

cargo fmt --check, cargo clippy --locked -- -D warnings and cargo test pass locally on Rust 1.99.1; the toolchain pin is unchanged.

An epoch is base64url, so one in 64 starts with '-'. clap then took
'--have -1aB:3' for an unknown option, and 'localify inventory --have'
failed. This is what made the_daemon_reports_each_state_and_revision_of_a_service
fail now and then at tests/inventory.rs:309.
The DNS server bound its sockets in a task of its own, while the
management socket already answered. When another program took the DNS
port in between, the daemon answered status and inventory requests and
then exited. The sockets are now bound first, so such a start fails
before anything sees the daemon up, with a message naming the port.
The loop decides whether to scan before it reads the Ctrl+C count. A
press that came in between was handled with the last scan, which could
predate a worker the service had just started. That worker got no
SIGINT, was orphaned when the launcher exited, and was never stopped.
The interrupt now scans the tree itself.
An executor records its exit, then releases the service's lock. A launch
in between found the lock held and refused with "'web' is already
running", though the run had ended. It now waits for the lock while the
record (or the launch record) holds the run's exit and its executor
still runs, up to the same 10 s as after a stop. This is what made
a_lost_acknowledgement_is_reported_exactly time out waiting for B.
…once

On macOS a flock belongs to the open file description, which a child
another test forks keeps until it execs: right after a drop, the lock
may still be held for a moment. the_finalize_lock_is_exclusive_and_never_recreates_a_folder
failed this way on main; the_lock_admits_one_executor_at_a_time relies on
the same thing. Both now wait for the release.

a_stop_whose_executor_dies_goes_on_only_under_the_lock waited 5 s for a
SIGTERM sent after a journal write; under heavy disk load that write
alone took longer. Its hang guard is now 30 s, as for the other waits of
that file.
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