leonida-lua: a_scan_without_a_size_reaches_the_end_of_the_image fails about one run in twenty #3193

Closed
opened 2026-12-02 17:56:13 +00:00 by fiestorra · 2 comments
fiestorra commented 2026-12-02 17:56:13 +00:00 (Migrated from github.com)
Contributor

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T17:56:13Z.


leonida-lua has one test that fails about one run in twenty, and it is not attached to any pull request.

What

d3d12/crates/leonida-lua/src/tests.rs:576
fn a_scan_without_a_size_reaches_the_end_of_the_image()

It writes DE AD BE EF at base + size - 100 and asserts tail 1 1 - once from an unbounded m.scan("DE AD BE EF") and once from m.scan("DE AD BE EF", {start = base - 100}).

Rate, measured

cargo test -p leonida-lua --lib in a loop, on main 5b1fff821, Ryzen 7 7800X3D, Windows 11:

runs failures
20 on main 1
20 with #3189 applied 0

So it is pre-existing and unrelated to that pull request - it showed as "1 in 7" while I was checking #3189, which reads like a regression, and it is not.

Reproduce

for i in 1..20 ; do cargo test -p leonida-lua --lib ; done

Match test result: FAILED, not FAILED. My first attempt counted 20 failures in 20 runs on both sides because -match 'FAILED' also matches "0 failed" in the success line - a reader who repeats this without the exact phrase will get a different and wrong answer.

What I did not determine

Why. The marker is written at a fixed offset from the end of the image, so it is either not landing where the scans look or the image's size moving between the two calls. That needs the memory image and the scan path, not a repetition count.

No game, no GPU, no title needed - this is cargo test on the crate.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:6751278869 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3193). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T17:56:13Z. --- **`leonida-lua` has one test that fails about one run in twenty, and it is not attached to any pull request.** ## What ``` d3d12/crates/leonida-lua/src/tests.rs:576 fn a_scan_without_a_size_reaches_the_end_of_the_image() ``` It writes `DE AD BE EF` at `base + size - 100` and asserts `tail 1 1` - once from an unbounded `m.scan("DE AD BE EF")` and once from `m.scan("DE AD BE EF", {start = base - 100})`. ## Rate, measured `cargo test -p leonida-lua --lib` in a loop, on `main` `5b1fff821`, Ryzen 7 7800X3D, Windows 11: | runs | failures | | --- | --- | | 20 on `main` | **1** | | 20 with #3189 applied | **0** | **So it is pre-existing and unrelated to that pull request** - it showed as "1 in 7" while I was checking #3189, which reads like a regression, and it is not. ## Reproduce ``` for i in 1..20 ; do cargo test -p leonida-lua --lib ; done ``` **Match `test result: FAILED`, not `FAILED`.** My first attempt counted **20 failures in 20 runs on both sides** because `-match 'FAILED'` also matches **"0 failed"** in the success line - a reader who repeats this without the exact phrase will get a different and wrong answer. ## What I did not determine **Why.** The marker is written at a fixed offset from the end of the image, so it is either not landing where the scans look or the image's size moving between the two calls. **That needs the memory image and the scan path, not a repetition count.** **No game, no GPU, no title needed** - this is `cargo test` on the crate. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
fiestorra commented 2026-12-02 17:57:53 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T17:57:53Z.


Cause narrowed: it is the tests racing each other, not the test being flaky on its own.

cargo test -p leonida-lua --lib in a loop, 20 runs each, on main 5b1fff821:

how the tests run failures
default (cargo runs #[test]s in parallel) 3 / 20
-- --test-threads=1 0 / 20

So the test is fine alone and wrong when another test runs beside it. rig() builds a fake memory, the test writes DE AD BE EF at base + size - 100 and asserts the scans find it once - if the image or the base/size pair is shared with another rig, a second occurrence shows up and the assertion fails with a count of 2 instead of 1.

What that points at: a process-global in the Lua test rig (a static memory image, or the leonida.memory base/size resolved from something global rather than from the rig). I have not read far enough to name the static - that is the next step, and it is a grep for static in leonida-lua's rig, not another 20 runs.

What this does not say: that --test-threads=1 is a fix. It hides the race, it does not remove it - and two tests sharing a memory image will keep finding new ways to corrupt each other.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353814513 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3193#issuecomment-7353814513). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T17:57:53Z. --- **Cause narrowed: it is the tests racing each other, not the test being flaky on its own.** `cargo test -p leonida-lua --lib` in a loop, 20 runs each, on `main` `5b1fff821`: | how the tests run | failures | | --- | --- | | default (cargo runs `#[test]`s in parallel) | **3 / 20** | | `-- --test-threads=1` | **0 / 20** | **So the test is fine alone and wrong when another test runs beside it.** `rig()` builds a fake memory, the test writes `DE AD BE EF` at `base + size - 100` and asserts the scans find it **once** - **if the image or the base/size pair is shared with another `rig`, a second occurrence shows up and the assertion fails with a count of 2 instead of 1.** **What that points at**: **a process-global in the Lua test rig** (a static memory image, or the `leonida.memory` base/size resolved from something global rather than from the rig). **I have not read far enough to name the static** - **that is the next step, and it is a `grep` for `static` in `leonida-lua`'s rig, not another 20 runs.** **What this does not say**: **that `--test-threads=1` is a fix.** **It hides the race, it does not remove it** - **and two tests sharing a memory image will keep finding new ways to corrupt each other.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
fiestorra commented 2026-12-02 17:58:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T17:58:43Z.


Found the shared state, and the size of the fix.

d3d12/crates/leonida-lua/src/memory.rs:

19: static HOST: OnceLock<Host> = OnceLock::new();
20: static IMAGE: OnceLock<(usize, usize)> = OnceLock::new();

The memory image is process-global and set once. Every rig() in tests.rs writes into the same IMAGE, so two tests running side by side see each other's bytes - which is exactly why the marker is found twice and the assertion tail 1 1 fails. That it is a OnceLock also means the first rig() decides the base and size for every later one, which is worth knowing whoever fixes this.

The repository already has a pattern for this, from #2854 (d3d12-gpu: fix parallel unit test races in watch, descriptor): a static TEST_SYNC_LOCK: std::sync::Mutex<()> in the test module, taken by the tests that touch the global (descriptor.rs:378, fg.rs:456, host.rs:846). serial_test is not a dependency and #[serial] is not used anywhere, so the mutex is the convention.

Why this is not a two-line fix here: the lock has to be held for the length of a test, so it cannot be taken inside rig() and dropped - rig() would have to return the guard, and every test in the file destructures let (_d, fake, _rt) = rig(...) (~40 of them), so the tuple grows and all of them change. That is mechanical, but it is not small, and it is the owner's call whether the guard travels or the memory image stops being global.

Not done here: I did not write the fix. I found the cause and the pattern; the change touches 40 call sites in a crate I do not own, and a wrong one there would make the race quieter rather than gone.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353826883 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/issues/3193#issuecomment-7353826883). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T17:58:43Z. --- **Found the shared state, and the size of the fix.** `d3d12/crates/leonida-lua/src/memory.rs`: ``` 19: static HOST: OnceLock<Host> = OnceLock::new(); 20: static IMAGE: OnceLock<(usize, usize)> = OnceLock::new(); ``` **The memory image is process-global and set once.** Every `rig()` in `tests.rs` writes into **the same** `IMAGE`, so two tests running side by side see each other's bytes - **which is exactly why the marker is found twice and the assertion `tail 1 1` fails.** **That it is a `OnceLock` also means the first `rig()` decides the base and size for every later one**, which is worth knowing whoever fixes this. **The repository already has a pattern for this**, from #2854 (`d3d12-gpu: fix parallel unit test races in watch, descriptor`): a `static TEST_SYNC_LOCK: std::sync::Mutex<()>` in the test module, taken by the tests that touch the global (`descriptor.rs:378`, `fg.rs:456`, `host.rs:846`). **`serial_test` is not a dependency and `#[serial]` is not used anywhere**, so the mutex is the convention. **Why this is not a two-line fix here**: the lock has to be held for the length of a test, so it cannot be taken inside `rig()` and dropped - **`rig()` would have to return the guard**, and **every test in the file destructures `let (_d, fake, _rt) = rig(...)`** (~40 of them), so the tuple grows and all of them change. **That is mechanical, but it is not small, and it is the owner's call** whether the guard travels or the memory image stops being global. **Not done here**: I did not write the fix. **I found the cause and the pattern; the change touches 40 call sites in a crate I do not own, and a wrong one there would make the race quieter rather than gone.** **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy#3193
No description provided.