Loader: fix thread shutdown targeting, registry lifetime, and CRT detach ordering #1304

Open
github-bot wants to merge 1 commit from github-bot/gh-1304 into main AGit
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 17:17 UTC · original
geiyiliyabumo:test/loader-threads-audit → main · +498 −156 · 2 files · 1 commits · open

Summary

Audits \crates/victory-loader/src/threads.rs\ (with a minimal companion registration call in \windows.rs) and fixes real lifecycle/shutdown bugs:

  • Validates thread enumeration entry sizes, rechecks opened thread handle ownership, and waits for confirmed termination before detaching images (falling back to process termination if enumeration/classification fails on a live thread).
  • Preserves attach order and state across repeated image loads until final unmap; honors reentrant detach, unload, and notification disabling during pending callbacks.
  • Registers CRT process-detach cleanup before DLL/executable initialization (\windows.rs), keeps host threads alive on empty process detach, skips images unloaded by earlier callbacks, and preserves uninitialized TLS snapshot padding.
  • Expands synthetic test coverage to 5 tests including isolated Windows thread and CRT exit tests.

Verification

  • \cargo test -p victory-loader --lib threads\ ? 5 passed, 0 failed
  • \cargo clippy -p victory-loader --all-targets -- -D warnings\ ? clean
  • \cargo xtask policy\ ? 154 files ok

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 17:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1304) `geiyiliyabumo:test/loader-threads-audit` → `main` · +498 −156 · 2 files · 1 commits · open ## Summary Audits \crates/victory-loader/src/threads.rs\ (with a minimal companion registration call in \windows.rs\) and fixes real lifecycle/shutdown bugs: - Validates thread enumeration entry sizes, rechecks opened thread handle ownership, and waits for confirmed termination before detaching images (falling back to process termination if enumeration/classification fails on a live thread). - Preserves attach order and state across repeated image loads until final unmap; honors reentrant detach, unload, and notification disabling during pending callbacks. - Registers CRT process-detach cleanup before DLL/executable initialization (\windows.rs\), keeps host threads alive on empty process detach, skips images unloaded by earlier callbacks, and preserves uninitialized TLS snapshot padding. - Expands synthetic test coverage to 5 tests including isolated Windows thread and CRT exit tests. ## Verification - \cargo test -p victory-loader --lib threads\ ? 5 passed, 0 failed - \cargo clippy -p victory-loader --all-targets -- -D warnings\ ? clean - \cargo xtask policy\ ? 154 files ok *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:26:57 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:34 UTC · original

Automated triage for the maintainers.

This PR rewrites most of crates/victory-loader/src/threads.rs and bundles several separate changes: earlier atexit registration (register_process_detach, called from LoadedImage::attach and spawn_main), load reference counting in mapped/unmapped, atomic per-image notice state, a reworked end_other_threads/end_thread, and MaybeUninit TLS snapshots. The registration-order point is plausible: on main, attached() registers detach_all only after the first DllMain returns, so a handler the title registers with the host CRT during that attach runs after ours. But the bundle also changes exit behaviour. end_other_threads now returns Err when OpenThread fails for any reason other than ERROR_INVALID_PARAMETER (for example access denied), when the start address cannot be read on a live thread, or when the snapshot is short. detach_all then calls end_process(), which skips every title DLL_PROCESS_DETACH. On main, such threads are skipped and detach still runs. After TerminateThread the code also waits with INFINITE and relies on the watchdog.

To move this forward, please split it: the atexit ordering fix as a small PR of its own, and the thread-ending and registry changes separately, each with a short note on the failure it fixes. Please also say whether title images actually register exit handlers in the host CRT's table rather than in their own (the new test assumes they do). No CI ran on this head.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 02:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1304#issuecomment-7150900776) <!-- victory-triage v1 head=98716d1a58bf8c297f8376da3761dbb14094a43d verdict=needs-work at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. This PR rewrites most of `crates/victory-loader/src/threads.rs` and bundles several separate changes: earlier `atexit` registration (`register_process_detach`, called from `LoadedImage::attach` and `spawn_main`), load reference counting in `mapped`/`unmapped`, atomic per-image notice state, a reworked `end_other_threads`/`end_thread`, and `MaybeUninit` TLS snapshots. The registration-order point is plausible: on main, `attached()` registers `detach_all` only after the first `DllMain` returns, so a handler the title registers with the host CRT during that attach runs after ours. But the bundle also changes exit behaviour. `end_other_threads` now returns `Err` when `OpenThread` fails for any reason other than `ERROR_INVALID_PARAMETER` (for example access denied), when the start address cannot be read on a live thread, or when the snapshot is short. `detach_all` then calls `end_process()`, which skips every title `DLL_PROCESS_DETACH`. On main, such threads are skipped and detach still runs. After `TerminateThread` the code also waits with `INFINITE` and relies on the watchdog. To move this forward, please split it: the `atexit` ordering fix as a small PR of its own, and the thread-ending and registry changes separately, each with a short note on the failure it fixes. Please also say whether title images actually register exit handlers in the host CRT's table rather than in their own (the new test assumes they do). No CI ran on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1304/head:github-bot/gh-1304'
git switch 'github-bot/gh-1304'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1304'
git switch 'github-bot/gh-1304'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1304'
git switch 'github-bot/gh-1304'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1304'
git switch 'main'
git merge --squash 'github-bot/gh-1304'
git switch 'main'
git merge --ff-only 'github-bot/gh-1304'
git switch 'main'
git merge 'github-bot/gh-1304'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1304
No description provided.