hw_oracle: remove the temp workdir when the constructor fails (HW-ORACLE-WORKDIR-LEAK) #612

Manually merged
github merged 1 commit from gh-pr/612/head into gh-pr/612/base 2026-11-26 09:11:44 +00:00
Owner

@⁠TuroYT on GitHub · 2026-11-24 09:54 UTC · original
TuroYT:fix/hw-oracle-workdir-leak → main · +187 −13 · 3 files · 1 commits · merged 2026-11-24 12:44 UTC

Claimed in the coordination issue as: HW-ORACLE-WORKDIR-LEAK (#14)

What was wrong. DrmOracle.__init__ (tools/hw_oracle/drm.py, line 158) and Oracle.__init__ (tools/hw_oracle/runner.py, line 212) call tempfile.mkdtemp(...) and then can raise Unavailable: the launcher build, the arch lookup, the probe. close() removes the workdir, but it is never reached for an object whose constructor raised. Every skipped run (exit 77) therefore left a leonida-hw-oracle-* directory in the temp dir.

Reproduced on main (21625bca8) with the build step stubbed to raise: 3 skipped DrmOracle constructions left 3 directories, and 3 skipped Oracle constructions left 3 more.

What changed. In both constructors, the steps after mkdtemp run inside a try. On any BaseException the workdir is removed, but only if the constructor created it (owned = not workdir, the same test as workdir or mkdtemp(...)), and the exception is re-raised. A caller-supplied workdir is never removed. The new test is in tests/test_hw_oracle_workdir_leak.py; no existing file is touched beyond the two constructors.

Overlap with #297. #297 changes only tests/test_hw_oracle_drm.py, so the two PRs share no file. Its tests pass a caller-supplied workdir=, which this change leaves alone. I did not run a merge of the two branches.

Tested:

  • python3 -I tests/test_hw_oracle_workdir_leak.py on the unfixed constructors: FAILED (failures=5) (the 3 DrmOracle and 2 Oracle leak cases); the caller-workdir and success cases pass.
  • The same test after the fix: Ran 9 tests ... OK.
  • Repro with the build step stubbed to raise, TMPDIR set to an empty directory: before the fix 3 leftover directories (DrmOracle) and 6 in total; after the fix 0.
  • Edge runs on the fixed code: workdir="" (treated as not given) leaves nothing; a KeyboardInterrupt raised in the probe is re-raised and leaves nothing; an arch lookup failure after the build leaves nothing; a successful construction keeps its directory until close().
  • python3 -I tests/test_hw_oracle.py: OK (skipped=3); python3 -I tests/test_hw_oracle_asm_strip.py: OK; python3 -m compileall -q tools tests: ok; python3 -I tests/test_text_write_line_endings.py: OK; python3 -I scripts/check_no_game_data.py on the three changed files: exit 0.

Not tested: a real GPU or DRM run (the probe was replaced by a stub in every case), and the Windows path, which uses hw_oracle.c and was not exercised.

AI-assisted (Claude Code).

**@⁠TuroYT** on GitHub · 2026-11-24 09:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/612) `TuroYT:fix/hw-oracle-workdir-leak` → `main` · +187 −13 · 3 files · 1 commits · merged 2026-11-24 12:44 UTC Claimed in the coordination issue as: HW-ORACLE-WORKDIR-LEAK (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) **What was wrong.** `DrmOracle.__init__` (`tools/hw_oracle/drm.py`, line 158) and `Oracle.__init__` (`tools/hw_oracle/runner.py`, line 212) call `tempfile.mkdtemp(...)` and then can raise `Unavailable`: the launcher build, the arch lookup, the probe. `close()` removes the workdir, but it is never reached for an object whose constructor raised. Every skipped run (exit 77) therefore left a `leonida-hw-oracle-*` directory in the temp dir. Reproduced on `main` (21625bca8) with the build step stubbed to raise: 3 skipped `DrmOracle` constructions left 3 directories, and 3 skipped `Oracle` constructions left 3 more. **What changed.** In both constructors, the steps after `mkdtemp` run inside a `try`. On any `BaseException` the workdir is removed, but only if the constructor created it (`owned = not workdir`, the same test as `workdir or mkdtemp(...)`), and the exception is re-raised. A caller-supplied `workdir` is never removed. The new test is in `tests/test_hw_oracle_workdir_leak.py`; no existing file is touched beyond the two constructors. **Overlap with #297.** #297 changes only `tests/test_hw_oracle_drm.py`, so the two PRs share no file. Its tests pass a caller-supplied `workdir=`, which this change leaves alone. I did not run a merge of the two branches. Tested: - `python3 -I tests/test_hw_oracle_workdir_leak.py` on the unfixed constructors: `FAILED (failures=5)` (the 3 `DrmOracle` and 2 `Oracle` leak cases); the caller-workdir and success cases pass. - The same test after the fix: `Ran 9 tests ... OK`. - Repro with the build step stubbed to raise, `TMPDIR` set to an empty directory: before the fix 3 leftover directories (DrmOracle) and 6 in total; after the fix 0. - Edge runs on the fixed code: `workdir=""` (treated as not given) leaves nothing; a `KeyboardInterrupt` raised in the probe is re-raised and leaves nothing; an arch lookup failure after the build leaves nothing; a successful construction keeps its directory until `close()`. - `python3 -I tests/test_hw_oracle.py`: `OK (skipped=3)`; `python3 -I tests/test_hw_oracle_asm_strip.py`: `OK`; `python3 -m compileall -q tools tests`: ok; `python3 -I tests/test_text_write_line_endings.py`: `OK`; `python3 -I scripts/check_no_game_data.py` on the three changed files: exit 0. Not tested: a real GPU or DRM run (the probe was replaced by a stub in every case), and the Windows path, which uses `hw_oracle.c` and was not exercised. AI-assisted (Claude Code).
DrmOracle.__init__ and Oracle.__init__ create a temporary workdir and then
can raise Unavailable (a skipped probe, exit 77). close() is never reached
for an object whose constructor raised, so every skipped run left a
leonida-hw-oracle-* directory in the temp dir.

Remove the directory when the constructor made it and then fails. A
caller-supplied workdir is left alone. tests/test_hw_oracle_workdir_leak.py
covers the early, late and caller-supplied cases with no GPU or libdrm.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:11:42 +00:00
github manually merged commit b68ce6cccd into gh-pr/612/base 2026-11-26 09:11:44 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.