leonida-lua: each mod gets a Lua state of its own, so its memory limit is its own (D3D12-LUA-MEMORY-LIMIT-IS-STATE-WIDE) #3179

Merged
eipastel merged 3 commits from upstream-pr/3179 into main 2026-12-02 21:30:46 +00:00
eipastel commented 2026-12-02 16:51:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by eipastel on 2026-12-02T16:51:33Z.


Claimed in the coordination issue as: D3D12-LUA-MEMORY-LIMIT-IS-STATE-WIDE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7352279491)

Depends on: none. Touches d3d12/crates/leonida-lua/src/sandbox.rs and tests.rs like #3115 (per-mod PRNG); with a state per mod, math.random/math.randomseed are per mod already, so #3115's own PRNG would no longer be needed.

What and why

One Lua state held every mod, so MEMORY_LIMIT (128 MiB, sandbox.rs:28) was one budget for all of them: a mod holding memory made another mod's ordinary allocation fail, and collectgarbage("count") reported the other mods' memory too (finding D3D12-LUA-MEMORY-LIMIT-IS-STATE-WIDE).

Each mod now loads into a Lua state of its own (sandbox::new_lua(&guard)), with the same 128 MiB limit. The time guard stays one, shared by the states (one call runs at a time), and its hook is installed on each state. Runtime keeps the states alive for the handlers and callbacks that point into them (mlua 0.12 handles do not keep a state alive). Runtime::new can no longer fail, so it returns Runtime. docs/lua-modding.md says each mod has its own state and limit.

Not changed: a mod that fails to load, or is switched off, keeps its state until the next reload (as before, when its objects stayed in the shared state).

Tested

  • New tests::a_mod_that_holds_memory_does_not_starve_another: mod A holds 120 MiB, mod B allocates 40 MiB and counts its own memory. python -I scripts/red_green.py lua-memory-limit-per-mod --crate leonida-lua on base 5b1fff821: red then green (1), pass on both: 44.
  • cargo test -p leonida-lua --lib --locked: 45 passed, 0 failed. cargo clippy -p leonida-lua --all-targets --locked -- -D warnings: clean. cargo check --workspace --all-targets --locked: clean.
  • python -m unittest discover -s tests -p "test_docs_*.py": OK. python -I scripts/check_no_game_data.py on the changed files: clean.

GPUs tested: n/a (no GPU or game files involved)

AI-assisted: yes. Code: Claude Opus 5.5. Independent read-only reviewer: Claude Opus (no blockers; its should-fix items, Runtime::new returning Result with nothing left to fail and the singular "Lua state" in comments, are addressed in the second commit).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5610981952 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179). Originally posted by [eipastel](https://github.com/eipastel) on 2026-12-02T16:51:33Z. --- Claimed in the coordination issue as: D3D12-LUA-MEMORY-LIMIT-IS-STATE-WIDE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7352279491) Depends on: none. Touches `d3d12/crates/leonida-lua/src/sandbox.rs` and `tests.rs` like #3115 (per-mod PRNG); with a state per mod, `math.random`/`math.randomseed` are per mod already, so #3115's own PRNG would no longer be needed. ### What and why One Lua state held every mod, so `MEMORY_LIMIT` (128 MiB, `sandbox.rs:28`) was one budget for all of them: a mod holding memory made another mod's ordinary allocation fail, and `collectgarbage("count")` reported the other mods' memory too (finding D3D12-LUA-MEMORY-LIMIT-IS-STATE-WIDE). Each mod now loads into a Lua state of its own (`sandbox::new_lua(&guard)`), with the same 128 MiB limit. The time guard stays one, shared by the states (one call runs at a time), and its hook is installed on each state. `Runtime` keeps the states alive for the handlers and callbacks that point into them (mlua 0.12 handles do not keep a state alive). `Runtime::new` can no longer fail, so it returns `Runtime`. `docs/lua-modding.md` says each mod has its own state and limit. Not changed: a mod that fails to load, or is switched off, keeps its state until the next reload (as before, when its objects stayed in the shared state). ### Tested - New `tests::a_mod_that_holds_memory_does_not_starve_another`: mod A holds 120 MiB, mod B allocates 40 MiB and counts its own memory. `python -I scripts/red_green.py lua-memory-limit-per-mod --crate leonida-lua` on base 5b1fff821: **red then green (1)**, pass on both: 44. - `cargo test -p leonida-lua --lib --locked`: 45 passed, 0 failed. `cargo clippy -p leonida-lua --all-targets --locked -- -D warnings`: clean. `cargo check --workspace --all-targets --locked`: clean. - `python -m unittest discover -s tests -p "test_docs_*.py"`: OK. `python -I scripts/check_no_game_data.py` on the changed files: clean. GPUs tested: n/a (no GPU or game files involved) AI-assisted: yes. Code: Claude Opus 5.5. Independent read-only reviewer: Claude Opus (no blockers; its should-fix items, `Runtime::new` returning `Result` with nothing left to fail and the singular "Lua state" in comments, are addressed in the second commit). ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a)
fiestorra commented 2026-12-02 17:34:08 +00:00 (Migrated from github.com)

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


Verified here: it applies on the current main and the whole leonida-lua suite passes.

merge base 5b1fff821 (= current origin/main), head 057cc94d8
git apply on main   (5 files)

cargo test -p leonida-lua --lib
  test result: ok. 45 passed; 0 failed; 0 ignored

What the change does, and why it is worth having: one Lua state held every mod, so MEMORY_LIMIT (128 MiB) was one budget shared by all of them - a mod that eats it starves the rest. A state per mod makes the limit per mod, which is what a limit means when the thing it limits is a mod.

The risk of a change like this is the opposite of what a test usually catches: not that one state misbehaves, but that something which used to be shared (a globals table, a registry entry, a handle) is now per-state and quietly duplicated or lost. 45 tests passing on the crate, with the memory ones included, is the evidence that the split did not lose anything the tests can see.

What it does not cover: a real mod actually reaching 128 MiB. That needs a launched title with two mods, one of them greedy - this machine can run neither, so the number that matters (does the second mod survive the first one's limit) is not measured here.

For the merge: the body says it touches sandbox.rs and tests.rs "like #3115 (per-mod PRNG)" - if #3115 is still open, whichever lands second has a conflict in the same files, and that is cheaper to sort now than after a review.

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

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353431596 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7353431596). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T17:34:08Z. --- **Verified here: it applies on the current `main` and the whole `leonida-lua` suite passes.** ``` merge base 5b1fff821 (= current origin/main), head 057cc94d8 git apply on main (5 files) cargo test -p leonida-lua --lib test result: ok. 45 passed; 0 failed; 0 ignored ``` **What the change does, and why it is worth having**: **one Lua state held every mod, so `MEMORY_LIMIT` (128 MiB) was one budget shared by all of them** - **a mod that eats it starves the rest.** **A state per mod makes the limit per mod**, **which is what a limit means when the thing it limits is a mod.** **The risk of a change like this is the opposite of what a test usually catches**: **not that one state misbehaves, but that something which used to be shared (a globals table, a registry entry, a handle) is now per-state and quietly duplicated or lost.** **45 tests passing on the crate, with the memory ones included, is the evidence that the split did not lose anything the tests can see.** **What it does not cover**: **a real mod actually reaching 128 MiB.** That needs a launched title with two mods, one of them greedy - **this machine can run neither**, **so the number that matters (does the second mod survive the first one's limit) is not measured here.** **For the merge**: the body says it touches `sandbox.rs` and `tests.rs` "like #3115 (per-mod PRNG)" - **if #3115 is still open, whichever lands second has a conflict in the same files**, and that is cheaper to sort now than after a review. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
MoonEyes commented 2026-12-02 18:18:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:18:08Z.


Verification of #3179 (third party, Linux x86_64, Rust 1.98.1, no GPU, no game files)

  • Head verified: 057cc94d8a7f3050a058dde61396068a11cfa256 (5 files, +65/-46).
  • Merge base 5b1fff821; merged with git merge --no-ff onto main 45dc41f95, no conflict.
  • leonida-lua builds and tests on Linux: its Windows-only dependencies are behind cfg(windows).

Green (main + PR)

Command (from d3d12/) Result
cargo test -p leonida-lua --locked lib 45 passed, 0 failed; doc-tests 0
python -I scripts/check_no_game_data.py <the 5 files> exit 0

Red

  • python -I scripts/red_green.py --no-fetch --base origin/main --work-dir <scratch> pr/3179 gives
    red does not build. The PR's tests.rs uses the new Runtime::new(..) -> Runtime signature (no .unwrap()),
    so the tests alone do not compile on main. This is not evidence either way.
  • Manual red: I added only the new test a_mod_that_holds_memory_does_not_starve_another, unchanged, to main's
    tests.rs (it uses rig and status, which main has). Then I ran
    cargo test -p leonida-lua --lib --locked a_mod_that_holds_memory:
    assertion `left == right` failed
      left: ["false false false"]
     right: ["true 41943040 true"]
    test result: FAILED. 0 passed; 1 failed
    
    On main, B's 40 MiB string.rep fails because A holds about 120 MiB of the shared 128 MiB. With the PR it
    passes. Red then green.

Review of the diff

  • Root cause fixed, not worked around: MEMORY_LIMIT is per Lua in mlua. Giving each mod its own state
    (runtime.rs:267-269) makes the limit and collectgarbage("count") per mod. The time budget stays shared
    through one Guard (sandbox.rs:50-65), which is right because one call runs at a time.
  • Cross-state mixing: handlers and menu callbacks are stored as Function with their owning state, and are called
    only with Rust values (runtime.rs:291-296, :388-389). I found no Lua value passed from one mod's state into
    another's.
  • Nit (not blocking): runtime.rs:268-269 pushes the state into states before build_env and load can
    fail, so a mod that fails to load keeps its state until the runtime is dropped. Push it after a successful load,
    or say why it stays.
  • The total cap is now mods x 128 MiB, not 128 MiB, and docs/lua-modding.md says so ("128 MiB times their
    number").
  • Not run here: cargo clippy and cargo fmt --check, because the 1.98.1 clippy and rustfmt components are not
    available on this machine. CI covers both.

AI disclosure (models and reviewer) and the claim are in the body. The body notes the overlap with #3115.

Verdict: ready. Red then green shown by hand, and the crate suite is green on Linux.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7354325489 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7354325489). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T18:18:08Z. --- **Verification of #3179 (third party, Linux x86_64, Rust 1.98.1, no GPU, no game files)** - Head verified: `057cc94d8a7f3050a058dde61396068a11cfa256` (5 files, +65/-46). - Merge base `5b1fff821`; merged with `git merge --no-ff` onto `main` `45dc41f95`, no conflict. - `leonida-lua` builds and tests on Linux: its Windows-only dependencies are behind `cfg(windows)`. **Green (main + PR)** | Command (from `d3d12/`) | Result | |---|---| | `cargo test -p leonida-lua --locked` | lib 45 passed, 0 failed; doc-tests 0 | | `python -I scripts/check_no_game_data.py <the 5 files>` | exit 0 | **Red** - `python -I scripts/red_green.py --no-fetch --base origin/main --work-dir <scratch> pr/3179` gives `red does not build`. The PR's `tests.rs` uses the new `Runtime::new(..) -> Runtime` signature (no `.unwrap()`), so the tests alone do not compile on `main`. This is not evidence either way. - Manual red: I added only the new test `a_mod_that_holds_memory_does_not_starve_another`, unchanged, to `main`'s `tests.rs` (it uses `rig` and `status`, which `main` has). Then I ran `cargo test -p leonida-lua --lib --locked a_mod_that_holds_memory`: ``` assertion `left == right` failed left: ["false false false"] right: ["true 41943040 true"] test result: FAILED. 0 passed; 1 failed ``` On `main`, B's 40 MiB `string.rep` fails because A holds about 120 MiB of the shared 128 MiB. With the PR it passes. **Red then green.** **Review of the diff** - Root cause fixed, not worked around: `MEMORY_LIMIT` is per `Lua` in mlua. Giving each mod its own state (`runtime.rs:267-269`) makes the limit and `collectgarbage("count")` per mod. The time budget stays shared through one `Guard` (`sandbox.rs:50-65`), which is right because one call runs at a time. - Cross-state mixing: handlers and menu callbacks are stored as `Function` with their owning state, and are called only with Rust values (`runtime.rs:291-296`, `:388-389`). I found no Lua value passed from one mod's state into another's. - Nit (not blocking): `runtime.rs:268-269` pushes the state into `states` before `build_env` and `load` can fail, so a mod that fails to load keeps its state until the runtime is dropped. Push it after a successful load, or say why it stays. - The total cap is now `mods x 128 MiB`, not 128 MiB, and `docs/lua-modding.md` says so ("128 MiB times their number"). - Not run here: `cargo clippy` and `cargo fmt --check`, because the 1.98.1 clippy and rustfmt components are not available on this machine. CI covers both. AI disclosure (models and reviewer) and the claim are in the body. The body notes the overlap with #3115. **Verdict: ready.** Red then green shown by hand, and the crate suite is green on Linux. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
eipastel commented 2026-12-02 18:28:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by eipastel on 2026-12-02T18:28:50Z.


Thanks to both of you for checking it.

  • The nit (state kept for a mod that fails to load): fixed in 2f2f697c0. run_script now pushes the state into Runtime::states only after the script's load call succeeds, so a failed mod's state and its memory are dropped at once; the handles it registered before failing go when load_mod switches it off. cargo test -p leonida-lua --lib --locked: 45 passed; clippy -D warnings clean.
  • red_green.py says red does not build: right, the second commit changed Runtime::new to return Runtime, so the PR's tests.rs does not compile on main. The red side was shown on the first commit (2d2ae41b9) with red_green.py lua-memory-limit-per-mod --crate leonida-lua: red then green (1), and MoonEyes' manual run above shows the same ["false false false"].
  • #3115: still open; whichever lands second takes the conflict in sandbox.rs/tests.rs. With a state per mod, #3115's per-mod PRNG is no longer needed, since math.random already has a state of its own per mod.

AI-assisted: Claude Opus 5.5.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7354656656 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7354656656). Originally posted by [eipastel](https://github.com/eipastel) on 2026-12-02T18:28:50Z. --- Thanks to both of you for checking it. - **The nit (state kept for a mod that fails to load):** fixed in `2f2f697c0`. `run_script` now pushes the state into `Runtime::states` only after the script's load call succeeds, so a failed mod's state and its memory are dropped at once; the handles it registered before failing go when `load_mod` switches it off. `cargo test -p leonida-lua --lib --locked`: 45 passed; clippy `-D warnings` clean. - **`red_green.py` says `red does not build`:** right, the second commit changed `Runtime::new` to return `Runtime`, so the PR's `tests.rs` does not compile on `main`. The red side was shown on the first commit (`2d2ae41b9`) with `red_green.py lua-memory-limit-per-mod --crate leonida-lua`: red then green (1), and MoonEyes' manual run above shows the same `["false false false"]`. - **#3115:** still open; whichever lands second takes the conflict in `sandbox.rs`/`tests.rs`. With a state per mod, #3115's per-mod PRNG is no longer needed, since `math.random` already has a state of its own per mod. AI-assisted: Claude Opus 5.5.
Kal0s-design commented 2026-12-02 18:53:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T18:53:03Z.


Ran natively on Windows 11, head 2f2f697c0 merged into current main (f1a559851, clean merge).

The thread covers Linux and the first commit's red. This is the Windows run on the current head, plus a few cases the new test doesn't cover.

Results (measured)

Check Result
cargo test -p leonida-lua --lib --locked on the merge 45 passed, 0 failed
Red: only a_mod_that_holds_memory_does_not_starve_another added to main 44 passed, 1 failed, left: ["false false false"], same as on Linux
cargo clippy -p leonida-lua --all-targets no warnings
cargo fmt --check only the two api.rs diffs already on main

Isolation probes (measured, throwaway tests, same probe on main and on the merge)

Case main PR
two mods each holding 100 MiB at load the second Failed with "not enough memory" both Running
A goes past 128 MiB at load, then B allocates 40 MiB A Failed, B's allocation fails A Failed, B gets its 40 MiB
A goes past its limit inside a frame handler, B allocates 40 MiB each frame A off, B's allocation fails every frame A off, B fine every frame, still Running
A registers a frame handler and a menu page, then errors at load no panic, handler never runs, B runs same

The last one was to check the new early drop. A's state goes away before disable removes its handlers, and passes after that are fine.

What 2f2f697c0 adds (measured). A mod that holds 100 MiB, registers a frame handler and a page, then calls error() at load. With the runtime still alive after load_dir, process private memory was back at base with the PR. On main it stayed about 101 MiB higher. So a mod that fails at load gives its memory back right away now.

math.random (measured). Mod B seeds with 7 and draws from a frame handler. Mod A calls math.randomseed(1) in its own frame handler, before B. With the PR, B's numbers are the same as when B runs alone. On main they change. That backs the author's point that #3115 isn't needed once this lands.

Unload (measured). A mod that loads fine and is then switched off by an error in a handler keeps its state. With 100 MiB held at load, process private memory went from 2 to 102 MiB and stayed at 102 after the mod was disabled. It only went back down when the runtime was dropped. main does the same (101 MiB), so it's not a regression. In code, states is only ever pushed to. Now that each mod has its own state, dropping it on disable would be cheap to add, and it would give back that mod's memory without a reload. Up to you whether that belongs here or in a follow-up.

Cost of a state per mod (measured, rough, debug build). 300 near-empty mods add 11 to 14 MiB of private memory with the PR and 4 to 5.5 MiB on main (3 runs each), so about 25 KiB per extra state. Load time for 100 mods was 130 ms with the PR and 180 ms on main. At that size the difference is noise.

Merge

Against Result
main clean
#3189 textually clean, but it does not build. #3189's new test calls Runtime::new(..).unwrap(), which doesn't exist after this PR (E0599). Removing the .unwrap() gives 46 passed
#3115 conflicts in sandbox.rs and tests.rs (already mentioned in the thread)
#3125 conflicts in sandbox.rs, runtime.rs and docs/lua-modding.md. This one isn't mentioned yet. #3125 turns the guard's deadline into an Option and wraps the load call in guard.run(..), so whichever lands second has to move that into the new Guard::default and keep the states.push after the call

The pre-existing flake a_scan_without_a_size_reaches_the_end_of_the_image failed 6 times in 40 runs on the merge and 1 in 20 on main, with --test-threads=4. Every failure was left: ["tail 1 0"], meaning the second scan found nothing. That's a wrong count, not a budget or memory error, so I don't think it comes from this change. It deserves its own issue though.

Not checked: a real mod reaching the limit in the game, Linux or Wine on this head, and the per-state cost in a release build.

GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7355297331 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7355297331). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T18:53:03Z. --- Ran natively on Windows 11, head `2f2f697c0` merged into current `main` (`f1a559851`, clean merge). The thread covers Linux and the first commit's red. This is the Windows run on the current head, plus a few cases the new test doesn't cover. **Results (measured)** | Check | Result | |---|---| | `cargo test -p leonida-lua --lib --locked` on the merge | 45 passed, 0 failed | | Red: only `a_mod_that_holds_memory_does_not_starve_another` added to `main` | 44 passed, 1 failed, `left: ["false false false"]`, same as on Linux | | `cargo clippy -p leonida-lua --all-targets` | no warnings | | `cargo fmt --check` | only the two `api.rs` diffs already on `main` | **Isolation probes (measured, throwaway tests, same probe on `main` and on the merge)** | Case | `main` | PR | |---|---|---| | two mods each holding 100 MiB at load | the second Failed with "not enough memory" | both Running | | A goes past 128 MiB at load, then B allocates 40 MiB | A Failed, B's allocation fails | A Failed, B gets its 40 MiB | | A goes past its limit inside a frame handler, B allocates 40 MiB each frame | A off, B's allocation fails every frame | A off, B fine every frame, still Running | | A registers a frame handler and a menu page, then errors at load | no panic, handler never runs, B runs | same | The last one was to check the new early drop. A's state goes away before `disable` removes its handlers, and passes after that are fine. **What `2f2f697c0` adds (measured).** A mod that holds 100 MiB, registers a frame handler and a page, then calls `error()` at load. With the runtime still alive after `load_dir`, process private memory was back at base with the PR. On `main` it stayed about 101 MiB higher. So a mod that fails at load gives its memory back right away now. **`math.random` (measured).** Mod B seeds with 7 and draws from a frame handler. Mod A calls `math.randomseed(1)` in its own frame handler, before B. With the PR, B's numbers are the same as when B runs alone. On `main` they change. That backs the author's point that #3115 isn't needed once this lands. **Unload (measured).** A mod that loads fine and is then switched off by an error in a handler keeps its state. With 100 MiB held at load, process private memory went from 2 to 102 MiB and stayed at 102 after the mod was disabled. It only went back down when the runtime was dropped. `main` does the same (101 MiB), so it's not a regression. In code, `states` is only ever pushed to. Now that each mod has its own state, dropping it on `disable` would be cheap to add, and it would give back that mod's memory without a reload. Up to you whether that belongs here or in a follow-up. **Cost of a state per mod (measured, rough, debug build).** 300 near-empty mods add 11 to 14 MiB of private memory with the PR and 4 to 5.5 MiB on `main` (3 runs each), so about 25 KiB per extra state. Load time for 100 mods was 130 ms with the PR and 180 ms on `main`. At that size the difference is noise. **Merge** | Against | Result | |---|---| | `main` | clean | | #3189 | textually clean, but it does not build. #3189's new test calls `Runtime::new(..).unwrap()`, which doesn't exist after this PR (E0599). Removing the `.unwrap()` gives 46 passed | | #3115 | conflicts in `sandbox.rs` and `tests.rs` (already mentioned in the thread) | | #3125 | conflicts in `sandbox.rs`, `runtime.rs` and `docs/lua-modding.md`. This one isn't mentioned yet. #3125 turns the guard's deadline into an `Option` and wraps the load call in `guard.run(..)`, so whichever lands second has to move that into the new `Guard::default` and keep the `states.push` after the call | The pre-existing flake `a_scan_without_a_size_reaches_the_end_of_the_image` failed 6 times in 40 runs on the merge and 1 in 20 on `main`, with `--test-threads=4`. Every failure was `left: ["tail 1 0"]`, meaning the second scan found nothing. That's a wrong count, not a budget or memory error, so I don't think it comes from this change. It deserves its own issue though. Not checked: a real mod reaching the limit in the game, Linux or Wine on this head, and the per-state cost in a release build. GPUs tested: n/a (CPU-only tests). AI-assisted: yes (Claude Code).
eipastel commented 2026-12-02 19:21:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by eipastel on 2026-12-02T19:21:58Z.


Thanks for the Windows run and the extra probes.

  • The a_scan_without_a_size_reaches_the_end_of_the_image flake: the cause is in the test's fake, not in the scan or this PR. The fake took the image base from the heap Vec's address; the second scan starts at base - 100, cannot read its range whole, so memory::readable reads it by absolute 4 KiB pages, and when base % 4096 is 1..=99 the marker at base + 3996 sits in the piece that also covers bytes before the image and is skipped (["tail 1 0"]). Forcing the base to a page boundary + 50 fails every run; + 0 passes. The fix (a fixed page-aligned base, as a real image has) is ready on eipastel:lua-scan-test-page-aligned (claimed as D3D12-LUA-SCAN-TEST-PAGE-ALIGNED). I can't open it yet: accounts without write access get one open pull request at a time, so it goes up once this one lands.
  • Dropping a mod's state on disable: agreed it is cheap now; I'll keep it for a follow-up so this PR stays one topic.
  • #3125 and #3189: noted, whichever lands second adapts (Guard::default and the states.push after the load call; Runtime::new without .unwrap()).

AI-assisted: Claude Opus 5.5.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7356058921 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7356058921). Originally posted by [eipastel](https://github.com/eipastel) on 2026-12-02T19:21:58Z. --- Thanks for the Windows run and the extra probes. - **The `a_scan_without_a_size_reaches_the_end_of_the_image` flake:** the cause is in the test's fake, not in the scan or this PR. The fake took the image base from the heap `Vec`'s address; the second scan starts at `base - 100`, cannot read its range whole, so `memory::readable` reads it by absolute 4 KiB pages, and when `base % 4096` is 1..=99 the marker at `base + 3996` sits in the piece that also covers bytes before the image and is skipped (`["tail 1 0"]`). Forcing the base to a page boundary + 50 fails every run; + 0 passes. The fix (a fixed page-aligned base, as a real image has) is ready on `eipastel:lua-scan-test-page-aligned` (claimed as D3D12-LUA-SCAN-TEST-PAGE-ALIGNED). I can't open it yet: accounts without write access get one open pull request at a time, so it goes up once this one lands. - **Dropping a mod's state on `disable`:** agreed it is cheap now; I'll keep it for a follow-up so this PR stays one topic. - **#3125 and #3189:** noted, whichever lands second adapts (`Guard::default` and the `states.push` after the load call; `Runtime::new` without `.unwrap()`). AI-assisted: Claude Opus 5.5.
MoonEyes commented 2026-12-02 19:27:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T19:27:59Z.


Re-verification of #3179 on the current head (third party, Linux x86_64, Rust 1.98.1, no GPU, no game files): the earlier nit is fixed, ready.

Head 2f2f697c0
Merge base 5b1fff821, 16 commits behind main b41763227; git merge --no-ff is clean
Since 057cc94d8 one commit, 2f2f697c0 "leonida-lua: keep a mod's state only once it has loaded" (runtime.rs +5/-1). The other four files are unchanged.

The earlier nit (state kept for a mod that fails to load): fixed. run_script now pushes the Lua into Runtime::states only after the load call returns Ok (d3d12/crates/leonida-lua/src/runtime.rs:285-288). On any earlier ? the state is dropped at the end of the function, and load_mod then calls Shared::disable (runtime.rs:242-245), which drops the mod's handlers and pages. Dropping those handles after their state is safe: in mlua 0.12.3 a ValueRef holds a WeakLua, and its Drop touches the state only when try_lock succeeds.

Extra probe (local only, not committed): a mod that registers a frame handler, a menu page with an action and a held key, then calls error("load boom"), next to a good mod. After three passes the bad mod is Failed, its handler never ran, its page is gone, and the good mod logged three times. cargo test -p leonida-lua --locked probe_: 1 passed.

Commands (from d3d12/ on main + head, detached):

  • cargo test -p leonida-lua --locked: lib 45 passed, 0 failed; doc-tests 0.
  • python -I scripts/check_no_game_data.py on the 5 changed files: exit 0.
  • Full standalone suite (CI selection into python -I scripts/run_standalone_tests.py -j 8): 660 files, 1 failed (tests/test_run_game_wine.py). Plain main b41763227 gives the same result, and the failure comes from the local environment, not this PR.

Red (main + only the new test): I added a_mod_that_holds_memory_does_not_starve_another unchanged to the tests.rs of main b41763227. cargo test -p leonida-lua --lib --locked a_mod_that_holds_memory: FAILED, left: ["false false false"], right: ["true 41943040 true"]. With the head it passes. Red then green. (scripts/red_green.py still reports "red does not build", because the PR's tests.rs uses the new Runtime::new signature.)

Notes, not blocking:

  • Other heads show a Windows leonida-lua failure in a_scan_without_a_size_reaches_the_end_of_the_image (tests.rs:587, ["tail 1 0"]). That test is in main and this PR does not touch it; the author has a separate fix ready.
  • CI: no check runs recorded on 2f2f697c0 yet.

Verdict: ready.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7356218851 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7356218851). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T19:27:59Z. --- **Re-verification of #3179 on the current head (third party, Linux x86_64, Rust 1.98.1, no GPU, no game files): the earlier nit is fixed, ready.** | | | |---|---| | Head | `2f2f697c0` | | Merge base | `5b1fff821`, 16 commits behind `main` `b41763227`; `git merge --no-ff` is clean | | Since `057cc94d8` | one commit, `2f2f697c0` "leonida-lua: keep a mod's state only once it has loaded" (`runtime.rs` +5/-1). The other four files are unchanged. | **The earlier nit (state kept for a mod that fails to load): fixed.** `run_script` now pushes the `Lua` into `Runtime::states` only after the load call returns `Ok` (`d3d12/crates/leonida-lua/src/runtime.rs:285-288`). On any earlier `?` the state is dropped at the end of the function, and `load_mod` then calls `Shared::disable` (`runtime.rs:242-245`), which drops the mod's handlers and pages. Dropping those handles after their state is safe: in mlua 0.12.3 a `ValueRef` holds a `WeakLua`, and its `Drop` touches the state only when `try_lock` succeeds. Extra probe (local only, not committed): a mod that registers a `frame` handler, a menu page with an action and a held key, then calls `error("load boom")`, next to a good mod. After three passes the bad mod is `Failed`, its handler never ran, its page is gone, and the good mod logged three times. `cargo test -p leonida-lua --locked probe_`: 1 passed. **Commands (from `d3d12/` on `main` + head, detached):** - `cargo test -p leonida-lua --locked`: lib 45 passed, 0 failed; doc-tests 0. - `python -I scripts/check_no_game_data.py` on the 5 changed files: exit 0. - Full standalone suite (CI selection into `python -I scripts/run_standalone_tests.py -j 8`): 660 files, 1 failed (`tests/test_run_game_wine.py`). Plain `main` `b41763227` gives the same result, and the failure comes from the local environment, not this PR. **Red (main + only the new test):** I added `a_mod_that_holds_memory_does_not_starve_another` unchanged to the `tests.rs` of `main` `b41763227`. `cargo test -p leonida-lua --lib --locked a_mod_that_holds_memory`: FAILED, `left: ["false false false"]`, `right: ["true 41943040 true"]`. With the head it passes. **Red then green.** (`scripts/red_green.py` still reports "red does not build", because the PR's `tests.rs` uses the new `Runtime::new` signature.) **Notes, not blocking:** - Other heads show a Windows `leonida-lua` failure in `a_scan_without_a_size_reaches_the_end_of_the_image` (`tests.rs:587`, `["tail 1 0"]`). That test is in `main` and this PR does not touch it; the author has a separate fix ready. - CI: no check runs recorded on `2f2f697c0` yet. **Verdict: ready.** AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
lixonik commented 2026-12-02 20:10:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T20:10:01Z.


Verified PR #3179 (2f2f697c0) merged cleanly onto main (f1a559851) on native Windows 10 x64 (Rust 1.98.1):

  • Clean-room check: python -I scripts/check_no_game_data.py PASS.
  • Unit tests: cargo test -p leonida-lua --lib --locked -- 45 passed, 0 failed (0.62 s).

Verdict: PASS (CPU/unit tests; no game launch).

AI-assisted: yes (Antigravity).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7357316446 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3179#issuecomment-7357316446). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T20:10:01Z. --- Verified PR #3179 (`2f2f697c0`) merged cleanly onto `main` (`f1a559851`) on native Windows 10 x64 (Rust 1.98.1): - **Clean-room check:** `python -I scripts/check_no_game_data.py` PASS. - **Unit tests:** `cargo test -p leonida-lua --lib --locked` -- **45 passed, 0 failed** (0.62 s). Verdict: **PASS** (CPU/unit tests; no game launch). AI-assisted: yes (Antigravity).
Sign in to join this conversation.
No reviewers
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!3179
No description provided.