Skip to content

fix(nid): retry replacing the patched file on Windows - #23366

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
maskym:fix/nid-patcher-replace-retry
Nov 24, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
maskym:fix/nid-patcher-replace-retry

Conversation

@maskym

@maskym maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What

nid_patcher writes the patched library to <file>.patching and renames it over <file> in a single attempt (core/libs/nid/src/main.cpp#L42-L47). On Windows that rename (MoveFileExW with MOVEFILE_REPLACE_EXISTING) is refused while any process holds the target without delete sharing, for example a scanner or indexer reading the .prx that cmake -E copy wrote milliseconds earlier, and the build step fails with FAIL: <file>: cannot write: <file>: Permission denied.

The patcher now retries the rename up to 20 times, 0.25 s apart, the same bound as the test runtime staging copies in cmake/CopyRuntimeFiles.cmake#L4 and #L18-L29 (#18221). The .patching file is kept during the retries. After the last attempt it is removed and the patcher fails with the same cannot write: <file>: <error> message and exit code 2 as before. The retry applies to every error code and every platform, with no #ifdef; on POSIX a rename over a file is not refused because of open handles, so a real failure there is only reported 4.75 s later. Reading the input and the cmake -E copy step are unchanged.

Tests in core/libs/nid/tests/test_native_paths.py (Windows only, skipped elsewhere):

  • test_transiently_held_target_is_replaced: the target is held open with Python's open(file, "rb"), which shares read and write but not delete, for 0.75 s after the patcher starts. The patcher must exit 0, the file must carry the patched exports, and no .patching file may remain.
  • test_permanently_held_target_reports_failure: the target is held for the whole run. The patcher must exit 2 with cannot write and the path on stderr and no OK: on stdout; the file must be unchanged and no .patching file may remain.

nid_patcher_native_paths gets TIMEOUT 30 instead of 20, because the second case alone sleeps 4.75 s.

Tested

Windows 11 Pro (10.0.26300), GCC 15.2.0 (WinLibs UCRT), Ninja, Release, BUILD_TESTING=ON. First run on main 56c87ad; rerun after merging main 9a50e10 (nid_patcher_native_paths passed again, and ctest -R "^nid_" passed 14/14 three times on the merged tree, for the nid_ targets built there).

  • ctest --test-dir <build> -R "^(nid_patcher_native_paths)$" --output-on-failure: passed 3/3 (5.71 s, 5.72 s, 5.72 s).
  • Red check: with only core/libs/nid/src/main.cpp reverted to main (git stash push -- core/libs/nid/src/main.cpp, rebuild nid_patcher), the same command fails in test_transiently_held_target_is_replaced: AssertionError: 2 != 0 : FAIL: C:\Users\...\Temp\tmpwkwlg_5w\library.prx: cannot write: C:\Users\...\Temp\tmpwkwlg_5w\library.prx: Permission denied. test_permanently_held_target_reports_failure passes on both main and this branch.
  • ctest --test-dir <build> -R "nid_" -V: 18/18 passed, including nid_patcher_macho, nid_patcher_symbol_versions, nid_pe_references, nid_elf_layout, nid_patch_dependencies and nid_patch_dependencies_helper.
  • Not tested on Linux or macOS; there the two new tests are skipped and the existing ones exercise the unchanged success path.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions github-actions Bot added the build label Nov 18, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7275 core/libs/CMakeLists.txt
PR 8650 core/libs/CMakeLists.txt
PR 9156 core/libs/CMakeLists.txt
PR 9293 core/libs/CMakeLists.txt
PR 9297 core/libs/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
PR 9383 core/libs/CMakeLists.txt
PR 12642 core/libs/CMakeLists.txt
PR 13589 core/libs/CMakeLists.txt
core/libs/nid/src/main.cpp
core/libs/nid/tests/test_native_paths.py
PR 16793 core/libs/CMakeLists.txt
PR 20315 core/libs/CMakeLists.txt
PR 23786 core/libs/CMakeLists.txt
PR 24322 core/libs/CMakeLists.txt
PR 24917 core/libs/CMakeLists.txt

@zarmstrong

Copy link
Copy Markdown
Contributor

Ran it on a second Windows machine: head 0fadc64d70 merged onto main 3ead3ea870, with main itself as the baseline. Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Ninja, Release, Python 3.14.

  • nid_patcher_native_paths passes 3/3 with --repeat until-fail:3 (6.0 s each); the other 13 nid_ tests pass, as on main.
  • Red check: with only core/libs/nid/src/main.cpp reverted to main, it fails in test_transiently_held_target_is_replaced: AssertionError: 2 != 0 : FAIL: ...\library.prx: cannot write: ...\library.prx: Permission denied. Restored, it passes again.

AI-assisted: yes (Claude Code)

@ShootanoJumper

Copy link
Copy Markdown
Contributor

Windows 11 22631: builds and patches every .prx, and nid_patcher_native_paths passes 3/3 (about 6.4 s, under the new 30 s limit). No locked-file scenario was reproduced.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 0fadc64d70, applied with git apply on main fd4aeed483 together with the other 26 PRs of one batch (#16367, #16415, #22363, #23103, #24064, #21547, #21742, #22774, #22852, #23420, #23952, #24346, #16945, #21718, #21780, #21912, #21931, #22061, #22425, #22619, #22657, #18765, #19662, #19767, #20899, #21571). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
nid_patcher_native_paths, shader_cache_native_paths (2 tests, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6523 tests) on the RTX 3080 has 4 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked. The 2126 agc_/video tests on the iGPU have 2 failing: agc_driver_stencil_plane_memory, agc_driver_store_over_untracked (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

This was referenced Nov 21, 2026
@1tsethan

Copy link
Copy Markdown
Contributor

I also checked a lock held past the retry deadline on Windows using the NID patcher at 0fadc64d70 and a synthetic DLL. Keeping a handle open without delete sharing made the patcher exit with code 2 after 5.06 s. The original DLL bytes were unchanged and no staging file remained. A separate 0.7 s lock was recoverable, consistent with the transient-lock result above.

AI-assisted: yes (OpenAI Codex).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — no blocking findings

Nothing blocks this change, but Windows behavior remains unverified in this review, so a formal approval is not justified.

What I ran

Reviewed head 0fadc64d705c0c2ad7bca07446a72a422befb0cd. Read description.md, commits.md, manifest.json, diff.patch, and all three changed files: core/libs/CMakeLists.txt, core/libs/nid/src/main.cpp, and core/libs/nid/tests/test_native_paths.py. The manifest lists all three as reviewed; none were excluded. No commands or tests were run. The Windows test results in the PR description are author-reported.

Results

Check Result Note
Patch and changed code review passed All changed files read
Test execution not run PR description reports Windows runs
Negative control not run Not independently reproduced

Negative control

Could not run the reported red check with the retry removed.

Findings

Blocking

None.

Non-blocking

None.

Sources

The retry loop makes up to 20 rename attempts, sleeping 250 ms between failures, then removes the pending file and reports the final error (core/libs/nid/src/main.cpp:48). The Windows-specific tests cover transient and permanent target locks (core/libs/nid/tests/test_native_paths.py:61, core/libs/nid/tests/test_native_paths.py:78). The test timeout is raised to 30 seconds (core/libs/CMakeLists.txt:565).

Not verified

I did not run tests or verify Windows, Linux, or macOS behavior. The test outcomes and environment stated in the PR description were not independently confirmed.

To get this to a good state

Nothing is required. Optional: independently run the Windows native path tests and their red check.

@JJenkx JJenkx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed head 0fadc64d70 from the code and its Windows CI log; I didn't build it locally.

  • It still merges cleanly with main 957c68fb42, and main's later permission-preserving step (cdf5776033) is already in its base. The retry wraps only the final rename, and the existing cleanup and error message stay the same. std::filesystem::rename(..., error_code&) clears the code on success, so the loop exits on the first successful attempt.
  • The new Windows-only tests did run in this head's windows job: nid_patcher_native_paths ... Passed 5.79 sec. That time fits the permanently-held case going through the full 20 × 250 ms retry bound before reporting cannot write, and the 30 s timeout leaves room for it.
  • One thing to know: the retry covers every rename error, not just sharing violations. A non-transient failure, such as a missing directory, now takes about 5 s to report. That seems acceptable for a build tool.
  • The red windows check (run 44415376409, 2026-11-18) failed only guest_libressl3_aes_wrap and guest_libressl3_aes (0xc0000409). This PR doesn't touch LibreSSL, and both pass on current main in a full Windows non-device sweep at b5460b9e94 (4,576 of 4,576). I haven't checked which main change fixed them. Re-running the check would refresh it.

AI-assisted: yes.

@oneandonlydean
oneandonlydean merged commit 6766000 into boykopovar:main Nov 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants