Fix flaky desktop server tests

Merged
CO
15h ago
+28-24

Summary

The two desktop server tests that start a stand-in server script ran in parallel, and a spawn on one test's thread could fork while the other test's script was still open for writing. The forked child holds that descriptor until it execs, so starting the script failed with Text file busy (os error 26) and made gates (rust) fail intermittently.

Writing the script and starting it now happen under one lock (SPAWNING), through a shared start_fake helper that both tests use. Test-only: the real server binary is never freshly written before it is started.

Testing

mkdir -p repro/src repro/tests/unit && cd repro
cp ../desktop/src/server.rs src/
cp ../desktop/tests/unit/server.rs tests/unit/
printf 'mod server;\nfn main() {}\n' > src/main.rs
cat > Cargo.toml <<'EOF'
[package]
name = "desktop-server"
version = "0.1.0"
edition = "2024"
[dependencies]
getrandom = "=0.4.3"
EOF
cargo test --no-run   # then run the printed test binary:
for i in $(seq 400); do ./target/debug/deps/desktop_server-* server::tests -q >/dev/null 2>&1 || echo fail; done | wc -l
failed runs of 400
before7 (every failure Text file busy, in both tests)
after0

Summary by cubic

Fixes flaky desktop/tests/unit/server.rs tests that intermittently failed with Text file busy when run in parallel.

The two tests that start a stand-in server script can race: a spawn on one test's thread can fork while the other test's script is still open for writing, and the forked child holds that descriptor until it execs. Writing the script and starting it now happen under one lock, SPAWNING, through a shared start_fake helper that both tests use. This is test-only; the real server binary is never freshly written before it is started.