tools/shader_carve_accel: release non-readonly/empty buffer pins and guard native fallback #1869

Merged
kirigayakazima merged 1 commit from upstream-pr/1869 into main 2026-11-28 20:16:43 +00:00
kirigayakazima commented 2026-11-25 13:43:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T13:43:23Z.


Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164153026)

What and why

  • Check path.is_file() before calling ctypes.CDLL in _load_native() (tools/shader_carve_accel.py) so a missing optional native binary returns None cleanly.
  • Treat false, off, and no (case-insensitive, stripped) identically to 0 for LEONIDA_SHADER_CARVE_NATIVE.
  • Immediately release the acquired Py_buffer pin in entry_search() when not view.readonly, view.len <= 0, or not view.buf so writable (bytearray / writable mmap) and empty buffers do not stay pinned on fallback, and guard finally cleanup against double-release.
  • Validate isinstance(pos, int) and not isinstance(pos, bool) and 0 <= pos < view.len before invoking native scan, and also catch TypeError/OverflowError when falling back cleanly to reference(pos).
  • Add dedicated mocked unit tests in tests/test_shader_carve_accel.py.

Tested

  • Patched: python -I tests/test_shader_carve_accel.py -v -> Ran 4 tests in 0.015s (OK); python -I tests/test_shader_carve.py -v -> Ran 31 tests in 1.434s (OK); python -I tests/test_shader_carve_native.py -v -> Ran 3 tests in 0.016s (OK (skipped=1), NativeParityTest skipped because the optional compiled native library is not present on this host).
  • Baseline check (upstream/main 5e95fa2f with tests/test_shader_carve_accel.py copied into detached worktree): Ran 4 tests in 0.012s (FAILED (failures=6, errors=2)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 5.145s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 12.917s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q (exit 0), git diff --check (exit 0).

GPUs tested: n/a (Py_buffer lifetime, fallback, and native-scanner safety unit tests)

AI-assisted: yes

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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492502293 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1869). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T13:43:23Z. --- Claimed in the coordination issue as: SHADER-CARVE-ACCEL-SAFETY-AND-UNIT-TESTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164153026) ### What and why - Check `path.is_file()` before calling `ctypes.CDLL` in `_load_native()` (`tools/shader_carve_accel.py`) so a missing optional native binary returns `None` cleanly. - Treat `false`, `off`, and `no` (case-insensitive, stripped) identically to `0` for `LEONIDA_SHADER_CARVE_NATIVE`. - Immediately release the acquired `Py_buffer` pin in `entry_search()` when `not view.readonly`, `view.len <= 0`, or `not view.buf` so writable (`bytearray` / writable `mmap`) and empty buffers do not stay pinned on fallback, and guard `finally` cleanup against double-release. - Validate `isinstance(pos, int) and not isinstance(pos, bool) and 0 <= pos < view.len` before invoking native `scan`, and also catch `TypeError`/`OverflowError` when falling back cleanly to `reference(pos)`. - Add dedicated mocked unit tests in `tests/test_shader_carve_accel.py`. ### Tested - Patched: `python -I tests/test_shader_carve_accel.py -v` -> `Ran 4 tests in 0.015s` (`OK`); `python -I tests/test_shader_carve.py -v` -> `Ran 31 tests in 1.434s` (`OK`); `python -I tests/test_shader_carve_native.py -v` -> `Ran 3 tests in 0.016s` (`OK (skipped=1)`, `NativeParityTest` skipped because the optional compiled native library is not present on this host). - Baseline check (`upstream/main` `5e95fa2f` with `tests/test_shader_carve_accel.py` copied into detached worktree): `Ran 4 tests in 0.012s` (`FAILED (failures=6, errors=2)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 5.145s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 12.917s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (Py_buffer lifetime, fallback, and native-scanner safety unit tests) AI-assisted: yes ### 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. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
GeorgiyDemo commented 2026-11-25 14:08:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T14:08:50Z.


Tested 7e38f9b24 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 71a769d7d (Python shader tool; no GPU):

  • pytest 9.2.0 on tests/test_shader_carve_accel.py: 4 passed, 14 subtests passed at this head.
  • The same file on main: 8 failed, 1 passed — the is_file()/native-library load guard, the false/off/no env handling and the native-fallback cases are new tests.
  • Not run: a real native acceleration library; only the missing/declined path is exercised.

AI-assisted; synthetic fixtures only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164828241 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1869#issuecomment-7164828241). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T14:08:50Z. --- Tested `7e38f9b24` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `71a769d7d` (Python shader tool; no GPU): - pytest 9.2.0 on `tests/test_shader_carve_accel.py`: **4 passed, 14 subtests passed** at this head. - The same file on `main`: **8 failed**, 1 passed — the `is_file()`/native-library load guard, the `false/off/no` env handling and the native-fallback cases are new tests. - Not run: a real native acceleration library; only the missing/declined path is exercised. AI-assisted; synthetic fixtures only, no game data.
DeadlyChirp commented 2026-11-25 15:32:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:32:09Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_shader_carve_accel.py:

  • PR: 4 passed, 14 subtests passed.
  • The new test file run against main's tools/shader_carve_accel.py: 8 failed, 1 passed. So the tests catch the problem, and the tool change fixes it on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166993656 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1869#issuecomment-7166993656). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:32:09Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_shader_carve_accel.py`: - PR: 4 passed, 14 subtests passed. - The new test file run against main's `tools/shader_carve_accel.py`: 8 failed, 1 passed. So the tests catch the problem, and the tool change fixes it on Windows. (AI-assisted test run; output checked by hand.)
MoonEyes commented 2026-11-25 17:11:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-25T17:11:51Z.

Review: COMMENTED


Independent test report (Windows 11, Python 3.14, origin/main + PR head).

Verdict: LGTM (one optional nit)
New tests/test_shader_carve_accel.py: 4 tests, 14 subtests, all pass on the PR head (file absent on main). Mutation check: PR tests against the pre-PR tools/shader_carve_accel.py gives 8 failed (5 disable-env subtests, missing-DLL CDLL call, validation/fallback/release test, writable+empty release test), so the tests do catch the fixed behaviours.
No Windows-specific failure: the tests mock _load_native, so no native DLL is needed; the missing-file guard returns None before CDLL.
Nit: the released flag looks redundant. The early release (line 64) sets release = None and falls back to reference before the try/finally, and the finally block runs once per generator, so release is not None already prevents a double release.
AI-assisted: yes (Claude Code ran the tests; I read them).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061832310 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1869#pullrequestreview-6061832310). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-25T17:11:51Z. **Review:** COMMENTED --- Independent test report (Windows 11, Python 3.14, origin/main + PR head). Verdict: LGTM (one optional nit) New tests/test_shader_carve_accel.py: 4 tests, 14 subtests, all pass on the PR head (file absent on main). Mutation check: PR tests against the pre-PR tools/shader_carve_accel.py gives 8 failed (5 disable-env subtests, missing-DLL CDLL call, validation/fallback/release test, writable+empty release test), so the tests do catch the fixed behaviours. No Windows-specific failure: the tests mock `_load_native`, so no native DLL is needed; the missing-file guard returns None before CDLL. Nit: the `released` flag looks redundant. The early release (line 64) sets `release = None` and falls back to `reference` before the try/finally, and the finally block runs once per generator, so `release is not None` already prevents a double release. AI-assisted: yes (Claude Code ran the tests; I read them).
MoonEyes commented 2026-11-28 20:16:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T20:16:40Z.


Red/green: On main test_shader_carve_accel.py has 6 failures and 2 errors (buffer pins not released, native loader called despite the disable variable), and with the PR all 4 tests pass on Windows 11 at OreoFR's (RTX 4070).
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245167468 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1869#issuecomment-7245167468). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T20:16:40Z. --- Red/green: On main test_shader_carve_accel.py has 6 failures and 2 errors (buffer pins not released, native loader called despite the disable variable), and with the PR all 4 tests pass on Windows 11 at OreoFR's (RTX 4070). Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:55:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:55:31Z.


Verified by OreoFR

  • Base: main 3408969a7 + PR head 7e38f9b24
  • Test: python -I tests/test_shader_carve_accel.py
  • Red on main: 6 FAIL + 2 ERROR (BufferError "Existing exports", pin not released; native loader called despite the disable variable): the defect itself
  • Green with the PR: 4 OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246371776 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1869#issuecomment-7246371776). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:55:31Z. --- **Verified by OreoFR** - Base: main `3408969a7` + PR head `7e38f9b24` - Test: `python -I tests/test_shader_carve_accel.py` - Red on main: 6 FAIL + 2 ERROR (BufferError "Existing exports", pin not released; native loader called despite the disable variable): the defect itself - Green with the PR: 4 OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
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!1869
No description provided.