Skip to content

shader_carve: a failed read, write or index rename is one ERROR line (BUILD-1625) - #6985

Open
mewajda wants to merge 2 commits into
ItsNotRexed:mainfrom
mewajda:shader-carve-errors
Open

mewajda wants to merge 2 commits into
ItsNotRexed:mainfrom
mewajda:shader-carve-errors

Conversation

@mewajda

@mewajda mewajda commented Nov 23, 2026

Copy link
Copy Markdown

Item BUILD-1625 (claim: #6910 (comment)).

What and why

tools/shader_carve.py ended in a traceback (exit 1) when it could not:

  • open or mmap its archive,
  • write a carved .dxbc,
  • create the staging file, fsync it, or os.replace index.json into place.

make_game.py takes index.json as proof that the carve finished, so a run that could not write it must not fail in a way that leaves a half-finished folder reported as complete. Each of the three paths now prints one ERROR: line naming the path and the OS reason and returns 2, which is what the surrounding checks (is not a file, is empty, cannot create) already do.

Two details worth reviewing:

  • the .dxbc write is inside its own try, so a full disk or a directory in the way does not get reported as a read failure;
  • index.json is still written last, under another name and renamed, so a failed run leaves no index.json at all.

Tested

  • python -I tests/test_shader_carve.py: 35/35 pass (31 existing + 4 new).
  • Against main's tool the four new tests error (traceback, 4 errors): a refusing mmap, a refusing open, a directory where the .dxbc has to go, and a failing os.replace. With the change all four return 2 with the matching ERROR: line.
  • python -I tests/test_bad_input_errors.py and tests/test_tool_startup.py: OK.
  • Real data, counts only: carving shaders.rpf from my own 1.0.2.1 copy gives 26,503 shaders, 1 candidate rejected, stages vs 2,236 / ms 9,282 / ps 12,101 / gs 195 / cs 2,689, every part present on all 26,503, and index.json written — identical to the counts before the change.

The tests build their own bytes; no game data is in the diff, the tests or this body.

Checklist

  • No game data, keys or decryption, including in logs and reports.
  • Standard library only, python -I compatible.
  • A new translator rule in the contract table: not a translator change.

AI-assisted.

A carve that could not open or map its archive, write a .dxbc, or stage
and rename index.json ended in a traceback (exit 1) instead of the
one-line ERROR + exit 2 the other tools use. Each of the three paths now
reports the path and the OS reason, and leaves no index.json behind, so
make_game.py's "the carve finished" marker is not written by a run that
did not finish.

Four synthetic tests: a refusing mmap, a refusing open, a directory where
the .dxbc has to go, and a failing os.replace.

Item: BUILD-1625
@mewajda
mewajda force-pushed the shader-carve-errors branch from d7b1613 to d1126c6 Compare November 23, 2026 15:40
@JoaoPauluu

Copy link
Copy Markdown

Tested d1126c6 (merged onto current main, no conflicts) on Linux 7.3, Python 3.12, no GPU: pass.

  • python -I tests/test_shader_carve.py: 35/35 OK; test_bad_input_errors.py, test_tool_startup.py, compileall OK. (test_text_write_line_endings.py fails on main too, from f64_omod_probe.py:268 / leonida_tui.py:753; unrelated.)
  • Real failures, not mocks: an archive with mode 000 gives ERROR: cannot read …: Permission denied, exit 2 (main: PermissionError traceback). A read-only out folder gives ERROR: cannot write …/valid.dxbc: Permission denied, exit 2, with no index.json. A normal carve is unchanged.
  • Read: no index.json after a failure keeps make_game.py's "carve again when missing" check right. The os.replace mock goes through replace_retry as intended. Nit: a stray line break in make_rpf_entry(...) in the first new test.

Overlaps #6997 and #5211 (same item). AI-assisted review.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no GPU/llvm-mc/MSVC used, no game files): pass.
Ran python -I tests/test_shader_carve.py: OK; tests/test_manifest_verifier.py: OK, 2 skipped.

AI-assisted (Claude Code). Only the tests this PR touches were run, from a clean checkout of the PR head.

@lucasgillet2009-lab lucasgillet2009-lab left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The four new tests reproduce the I/O errors on main and pass here. I am requesting one change: please make reruns safe in a directory that already contains a successful carve. The stale-index bug already exists on main, but it contradicts this PR’s stated guarantee that a failed carve cannot leave a half-finished directory reported as complete.

Two Windows reproductions

I started each case with two synthetic shaders, 76 bytes each, and a valid index.json. I reran the tool in the same directory with the same names and 140 bytes per shader.

Failure on the second carve Sizes in surviving index Actual sizes on disk
Mark the second .dxbc read-only 76, 76 bytes 140, 76 bytes
Hold the existing index.json open without delete sharing through the final rename 76, 76 bytes 140, 140 bytes

In both cases the PR returned 2 with one error line, and the old index survived byte-for-byte. I called make_game.shaders() and intercepted the next tool launch. It chose fssm_unpack.py both times. The mere presence of the index allowed it to skip carving with inconsistent inputs.

One fix would be to invalidate an existing index immediately before the first DXBC write. If that fails, abort before modifying any payload. Could you add regression tests starting from a successful carve? With the second payload read-only, the next build should retry carving. With the index locked, the previous index and both previous payloads should remain consistent.

Test results

Windows x64, Python 3.12.14, synthetic data only. Testing was assisted by Codex. I compared a66e6e30d on main with PR commit d1126c644.

  • Original shader-carve suite on main: 31/31 passed. With the PR tests on the same tool: 31 passed, 4 errored (source open, memory mapping, DXBC write, index replacement). The PR passed 35/35.
  • Bad inputs 11/11, replacement retries 10/10, and tool startup 2/2 passed on the PR. The global line-ending check was 2/3 on both revisions, flagging the same two files outside the diff.
  • In a fresh output directory, failures injected during staging-directory creation, partial JSON serialization, fsync, and the final rename each produced one error line and exit code 2. Each left the final index absent, cleaned up staging, and allowed the next attempt to succeed.

Two separate, non-blocking observations

  1. On both revisions, removing the source between isfile() and getsize() lets a FileNotFoundError escape. Checking the size on the opened file inside the read-error handling would cover that race.
  2. On both revisions, Windows can prevent the staging directory from being removed after a successful index replacement. On the PR that produces “cannot write …index.json”, although the published index and both payloads verified correctly. Naming the cleanup failure would help diagnose what actually remains on disk.

For coordination, #5211 and #6997 are also open for BUILD-1625 and cover overlapping shader_carve.py error paths. The existing-output cases above could serve as shared acceptance tests.

A second carve into a directory that already holds a finished carve could
fail with the previous index.json still in place, beside payloads it no
longer describes: make_game.py reads the file's presence as "the carve
finished" and skips carving with inconsistent inputs.

The stale index is now dropped just before the first payload write. If
it cannot be dropped, the run aborts before touching anything, so the
published index and the previous payloads stay consistent; if a later
write fails, no index is left and the next build carves again.

Two regressions start from a successful carve: a directory in the way of
the second payload must leave no index, and a locked index must leave the
old index and both old payloads byte-identical.

Also: the size check now reads the opened file, so a source removed
between isfile() and getsize() is one ERROR line; and a staging folder
that cannot be removed after a successful publication is named in a
WARNING instead of being reported as a failed index write.

Review: ItsNotRexed#6985 (review)
@mewajda

mewajda commented Nov 23, 2026

Copy link
Copy Markdown
Author

Done in d581d85: a rerun drops the stale index.json immediately before the first payload write.

  • Payload write fails — the old index is already gone, so nothing is left that claims the carve finished and the next build carves again.
  • The index cannot be dropped — the run aborts before touching a payload, so the published index and the previous payloads stay byte-identical and consistent with each other.

Two regressions start from a successful carve, as you asked:

  • test_rerun_after_a_failed_payload_write_leaves_no_index: carve, put a directory where the second .dxbc has to go, rerun with different payloads → exit 2 with one ERROR: cannot write line, no index.json left.
  • test_rerun_with_a_locked_index_touches_no_payload: os.unlink on the index raises PermissionError → exit 2 with ERROR: cannot replace, and every file in the output folder, index included, is byte-identical to before.

Both fail on d1126c644 (2 failures) and pass here: tests/test_shader_carve.py 37/37. Also re-ran test_bad_input_errors (11/11), test_tool_startup, test_replace_retry, and a real carve from my own copy: 26,503 shaders, same stage counts, index written.

Your two observations are in as well:

  1. The size check now runs on the opened file, so a source removed between isfile() and getsize() is one ERROR: cannot read line.
  2. The staging folder is cleaned up after the index is published; a cleanup failure now prints WARNING: cannot remove <dir>: <reason> instead of being reported as a failed index write.

On the overlap with #5211 and #6997: the two tests above are self-contained and start from a completed carve, so they can be lifted into whichever of the three lands, and they pin exactly the stale-index behaviour you reproduced.

AI-assisted.

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Duplicate of #5211

#5211 makes the same change and was opened earlier (#6997 is duplicates too). Topic: BUILD-1625 shader_carve error handling.

Suggest closing this PR in favour of #5211, and porting anything this one adds that #5211 lacks there.

@lucasgillet2009-lab lucasgillet2009-lab left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed commit d581d85 on Windows 11 using synthetic inputs. Both previously reported rerun bugs are fixed.

A failed .dxbc write now invalidates the stale index, while a locked index stops extraction before modifying any shader. In both cases, make_game.py resumes correctly.

After removing the injected failures, extraction succeeds and the payloads match the index byte-for-byte.

All 37 shader-carve tests pass. No GTA VI files were used. AI-assisted with Codex.

APPROVED — thank you for addressing the review!

@KoertLichtendonk

Copy link
Copy Markdown
Contributor

Coordinator note: BUILD-1625 already has an open PR, #5211 by @tomgamer22 (opened 09:22 UTC), so this looks like a duplicate. Please compare the two and either close this one or say here what it adds. The maintainer decides which one is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants