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

Open
geiyiliyabumo wants to merge 1 commit from upstream-pr/1304 into main
geiyiliyabumo commented 2026-11-24 17:17:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T17:17:36Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587418183 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1304). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T17:17:36Z. --- ## 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.*
Marvisak commented 2026-11-25 03:03:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T02:34:35Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150900776 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1304#issuecomment-7150900776). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T02:34:35Z. --- <!-- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1304:upstream-pr/1304'
git switch 'upstream-pr/1304'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1304'
git switch 'upstream-pr/1304'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1304'
git switch 'upstream-pr/1304'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1304'
git switch 'main'
git merge --squash 'upstream-pr/1304'
git switch 'main'
git merge --ff-only 'upstream-pr/1304'
git switch 'main'
git merge 'upstream-pr/1304'
git push origin 'main'
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
Marvisak/VIctory!1304
No description provided.