test(hle): two tests share process-wide state, so a full-suite run is red about one time in three #620

Closed
opened 2026-11-28 14:24:39 +00:00 by github-bot · 2 comments
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 14:13 UTC · original

What went wrong

cargo test --target x86_64-apple-darwin -p victory-hle --lib fails
intermittently — roughly one run in three, on a clean main — because tests
that share process-wide state run in parallel with each other. A green run
is therefore not yet a trustworthy signal, and a red one hides real regressions
(it cost two false reds today while landing #10–#14).

Two symptoms, measured on main at 2200a393 (M3 Pro, macOS 27.3), with
--lib run ten times each:

test rate assertion
modules::dsp::tests::dump_captures_the_titles_ir_blocks_and_output ~1 in 4 std::fs::read(dir.join("stream0_ir.f32")) — file missing
host_posix::winsock::tests::final_cleanup_invalidates_sockets_and_releases_their_ports ~1 in 5 winsock.rs:1607 — bind of a fixed port
host_posix::winsock::tests::off_machine_traffic_is_refused ~1 in 5 winsock.rs:1788 — "not started again"

Both pass when run alone, which is why they were not spotted earlier.

Why

dsp. The test sets VICTORY_DUMP_DSP with std::env::set_var — unsafe in
edition 2024 precisely because it is process-wide — comments "no other test
reads VICTORY_DUMP_DSP", and relies on that. But the module reads it on
every activation, and any other test that activates a stream while the
variable is set is captured too. Stream numbers come from a process-wide
counter, so a concurrent activation moves this test's dump from stream0_* to
stream1_* and the file it reads is not there.

winsock. WSAStartup/WSACleanup keep one process-wide reference count,
and final_cleanup_... asserts on it — "not started again" at
winsock.rs:1788 is false whenever another test has started Winsock meanwhile.
The same test binds a fixed port, so it fails when a concurrent test, or a
previous run's lingering socket, holds that port.

Suggested fix

A test-only lock, the same way the DSP/POSIX tests already serialize what they
cannot share:

  • one static LOCK: Mutex<()> in modules::dsp's tests, taken by the dump test
    and by every test that activates a stream (or, better, point the dump at a
    directory per activation so numbering cannot cross);
  • one in host_posix::winsock's tests, taken by every test that asserts on the
    WSAStartup reference count, with the fixed port replaced by an ephemeral
    one (bind port 0 and read the port back with getsockname).

Two of the suite's flakes were already fixed separately: which waiter a single
WakeByAddressSingle reaches (#12, b90ad5d4), and an address that is only
usually in the same stripe (#14, 2200a393).

Mac, macOS version and commit

M3 Pro, 36 GB, macOS 27.3, x86_64-apple-darwin (Rosetta), commit 2200a393.

Output or log

test host_posix::winsock::tests::final_cleanup_invalidates_sockets_and_releases_their_ports ... FAILED
test host_posix::winsock::tests::off_machine_traffic_is_refused ... FAILED
test result: FAILED. 358 passed; 2 failed; 1 ignored
**@⁠AnastasisKon** on GitHub · 2026-11-28 14:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/616) ### What went wrong `cargo test --target x86_64-apple-darwin -p victory-hle --lib` fails intermittently — roughly one run in three, on a clean `main` — because tests that share **process-wide** state run in parallel with each other. A green run is therefore not yet a trustworthy signal, and a red one hides real regressions (it cost two false reds today while landing #10–#14). Two symptoms, measured on `main` at `2200a393` (M3 Pro, macOS 27.3), with `--lib` run ten times each: | test | rate | assertion | |---|---|---| | `modules::dsp::tests::dump_captures_the_titles_ir_blocks_and_output` | ~1 in 4 | `std::fs::read(dir.join("stream0_ir.f32"))` — file missing | | `host_posix::winsock::tests::final_cleanup_invalidates_sockets_and_releases_their_ports` | ~1 in 5 | `winsock.rs:1607` — `bind` of a fixed port | | `host_posix::winsock::tests::off_machine_traffic_is_refused` | ~1 in 5 | `winsock.rs:1788` — "not started again" | Both pass when run alone, which is why they were not spotted earlier. ### Why **`dsp`.** The test sets `VICTORY_DUMP_DSP` with `std::env::set_var` — unsafe in edition 2024 precisely because it is process-wide — comments "no other test reads VICTORY_DUMP_DSP", and relies on that. But the module reads it on **every** activation, and any other test that activates a stream while the variable is set is captured too. Stream numbers come from a process-wide counter, so a concurrent activation moves this test's dump from `stream0_*` to `stream1_*` and the file it reads is not there. **`winsock`.** `WSAStartup`/`WSACleanup` keep one process-wide reference count, and `final_cleanup_...` asserts on it — "not started again" at `winsock.rs:1788` is false whenever another test has started Winsock meanwhile. The same test binds a **fixed** port, so it fails when a concurrent test, or a previous run's lingering socket, holds that port. ### Suggested fix A test-only lock, the same way the DSP/POSIX tests already serialize what they cannot share: - one `static LOCK: Mutex<()>` in `modules::dsp`'s tests, taken by the dump test and by every test that activates a stream (or, better, point the dump at a directory per activation so numbering cannot cross); - one in `host_posix::winsock`'s tests, taken by every test that asserts on the `WSAStartup` reference count, with the fixed port replaced by an ephemeral one (`bind` port 0 and read the port back with `getsockname`). Two of the suite's flakes were already fixed separately: which waiter a single `WakeByAddressSingle` reaches (#12, `b90ad5d4`), and an address that is only usually in the same stripe (#14, `2200a393`). ### Mac, macOS version and commit M3 Pro, 36 GB, macOS 27.3, `x86_64-apple-darwin` (Rosetta), commit `2200a393`. ### Output or log ``` test host_posix::winsock::tests::final_cleanup_invalidates_sockets_and_releases_their_ports ... FAILED test host_posix::winsock::tests::off_machine_traffic_is_refused ... FAILED test result: FAILED. 358 passed; 2 failed; 1 ignored ```
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:24:39 +00:00
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 14:26 UTC · original

Two of the three are fixed, and one of my diagnoses was wrong — correction below.

dsp::dump_captures_... — fixed in #16 (17f6d895). The cause is as
guessed, and worse than guessed: VICTORY_DUMP_DSP is process-wide and so is
DUMP_ID, the counter numbering each stream's capture. The other test in the
module that activates a stream (convolution_stream_as_the_title_drives_it)
could therefore start a stream inside the dump test's capture, take number 0,
and leave the dump test reading that stream's capture. The file it found had the
right length and its own stream's zeros, which is why it read as a capture bug:

panicked at modules/dsp.rs:949:
  left: [0.0, 0.0, ...]        // what it read from stream0_ir.f32
 right: [0.0, 0.25, 0.5, ...]  // the IR it asked for

The two now take turns through one lock. Nothing else in the crate activates a
stream — every other XDspConnectWithMaximumStreamLimit/XDspActivate call is
in that module's tests.

sync::wait_on_address_... — the flake in the report was fixed in #12, and my
follow-up in #14 was still wrong; that is fixed in #15 (4481ca91).
#14
searched forward from the first address for one in the same stripe, but stripes
repeat per page (((addr >> 2) ^ (addr >> 12)) & 31), so the search only
succeeded while the allocation sat far enough from the end of its page: it
panicked on its own expect on 1 run in 15. The neighbour is now taken on
whichever side of the address still has room in its page.

winsock — my explanation was wrong, so the fix needs a different one. Both
tests already take the module's serial() guard (winsock.rs:1568), so the
WSAStartup reference count is not what breaks. What is left is
final_cleanup_invalidates_sockets_and_releases_their_ports asserting that the
ephemeral port the old socket held is free again:

assert_eq!(WSAStartup(0x0202, std::ptr::null_mut()), 0);
let fresh = socket_(2, 2, 17);
...
assert_eq!(bind_(fresh, loopback_addr(port).as_ptr(), 16), 0);   // winsock.rs:1607

port came from bind_(old, loopback_addr(0), 16) — the OS chose it — and
between WSACleanup and the rebind any other bind_(.., 0) in the process (or a
lingering socket from an earlier run) can be handed the same number, so the
rebind fails for a reason that is not a leak. Suggested fix: choose the port
rather than let the OS choose it — bind once to prove it is free, release it, and
rebind the same number — and keep the existing "the old socket is no longer
usable" assertions unchanged.

I have not touched the winsock tests. Measured on main at 17f6d895, 8 runs of
the whole suite: 0 failures, i.e. the winsock pair is now the only residual and it
did not appear in those runs.

**@⁠AnastasisKon** on GitHub · 2026-11-28 14:26 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/616#issuecomment-7240671356) Two of the three are fixed, and one of my diagnoses was wrong — correction below. **`dsp::dump_captures_...` — fixed in #16 (`17f6d895`).** The cause is as guessed, and worse than guessed: `VICTORY_DUMP_DSP` is process-wide *and* so is `DUMP_ID`, the counter numbering each stream's capture. The other test in the module that activates a stream (`convolution_stream_as_the_title_drives_it`) could therefore start a stream inside the dump test's capture, take number 0, and leave the dump test reading that stream's capture. The file it found had the right length and its own stream's zeros, which is why it read as a capture bug: ``` panicked at modules/dsp.rs:949: left: [0.0, 0.0, ...] // what it read from stream0_ir.f32 right: [0.0, 0.25, 0.5, ...] // the IR it asked for ``` The two now take turns through one lock. Nothing else in the crate activates a stream — every other `XDspConnectWithMaximumStreamLimit`/`XDspActivate` call is in that module's tests. **`sync::wait_on_address_...` — the flake in the report was fixed in #12, and my follow-up in #14 was still wrong; that is fixed in #15 (`4481ca91`).** #14 searched forward from the first address for one in the same stripe, but stripes repeat **per page** (`((addr >> 2) ^ (addr >> 12)) & 31`), so the search only succeeded while the allocation sat far enough from the end of its page: it panicked on its own `expect` on 1 run in 15. The neighbour is now taken on whichever side of the address still has room in its page. **`winsock` — my explanation was wrong, so the fix needs a different one.** Both tests *already* take the module's `serial()` guard (`winsock.rs:1568`), so the `WSAStartup` reference count is not what breaks. What is left is `final_cleanup_invalidates_sockets_and_releases_their_ports` asserting that the **ephemeral** port the old socket held is free again: ```rust assert_eq!(WSAStartup(0x0202, std::ptr::null_mut()), 0); let fresh = socket_(2, 2, 17); ... assert_eq!(bind_(fresh, loopback_addr(port).as_ptr(), 16), 0); // winsock.rs:1607 ``` `port` came from `bind_(old, loopback_addr(0), 16)` — the OS chose it — and between `WSACleanup` and the rebind any other `bind_(.., 0)` in the process (or a lingering socket from an earlier run) can be handed the same number, so the rebind fails for a reason that is not a leak. Suggested fix: choose the port rather than let the OS choose it — bind once to prove it is free, release it, and rebind the same number — and keep the existing "the old socket is no longer usable" assertions unchanged. I have not touched the winsock tests. Measured on `main` at `17f6d895`, 8 runs of the whole suite: 0 failures, i.e. the winsock pair is now the only residual and it did not appear in those runs.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 14:36 UTC · original

All three are fixed now, and the third cause was not what I first wrote.

The real cause of the winsock pair: the Winsock tests already take a turn for
the process-wide startup count, but modules::net's only_loopback_is_reachable
drives the same functions by name — host!("ws2_32.dll", "socket"), connect,
WSAConnectByNameA, WSAIoctl, closesocket — and did not. On this host those
reach the native Winsock, so that test could start the stack inside
off_machine_traffic_is_refused's "not started again" assertion, or take an
ephemeral port inside final_cleanup_invalidates_sockets_and_releases_their_ports's
rebind of one it had just released. That is why the module alone never failed:
host_posix::winsock on its own was 0 failures in 20 runs, the suite was not.

The turn now lives in host_posix as winsock_turn(), taken by the Winsock tests
(through their existing serial()) and by that net test, compiled only where
the native host is. Fixed in #17 (d7c58a90).

Summary of the three, in the order they were fixed:

test cause PR
sync::wait_on_address_... a wake reaches whichever waiter registered first; also, an address only usually in the same stripe #12, then #15
dsp::dump_captures_... process-wide dump variable and a process-wide capture counter, so another test's stream took number 0 #16
winsock::final_cleanup_..., winsock::off_machine_traffic_... a test in another module drove Winsock with no turn taken #17

Measured after all of them, on main: the whole victory-hle suite ran 32
times in a row with no failure
(20 + 12), against roughly one in three when
this issue was filed. I am closing it.

**@⁠AnastasisKon** on GitHub · 2026-11-28 14:36 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/616#issuecomment-7240791632) All three are fixed now, and the third cause was not what I first wrote. **The real cause of the winsock pair**: the Winsock tests already take a turn for the process-wide startup count, but `modules::net`'s `only_loopback_is_reachable` drives the same functions by name — `host!("ws2_32.dll", "socket")`, `connect`, `WSAConnectByNameA`, `WSAIoctl`, `closesocket` — and did not. On this host those reach the native Winsock, so that test could start the stack inside `off_machine_traffic_is_refused`'s "not started again" assertion, or take an ephemeral port inside `final_cleanup_invalidates_sockets_and_releases_their_ports`'s rebind of one it had just released. That is why the module alone never failed: `host_posix::winsock` on its own was **0 failures in 20 runs**, the suite was not. The turn now lives in `host_posix` as `winsock_turn()`, taken by the Winsock tests (through their existing `serial()`) and by that `net` test, compiled only where the native host is. Fixed in #17 (`d7c58a90`). Summary of the three, in the order they were fixed: | test | cause | PR | |---|---|---| | `sync::wait_on_address_...` | a wake reaches whichever waiter registered first; also, an address only *usually* in the same stripe | #12, then #15 | | `dsp::dump_captures_...` | process-wide dump variable *and* a process-wide capture counter, so another test's stream took number 0 | #16 | | `winsock::final_cleanup_...`, `winsock::off_machine_traffic_...` | a test in another module drove Winsock with no turn taken | #17 | Measured after all of them, on `main`: the whole `victory-hle` suite ran **32 times in a row with no failure** (20 + 12), against roughly one in three when this issue was filed. I am closing it.
This discussion has been locked. Commenting is limited to contributors.
No description provided.