Loader: handle thread spawn failure in detach_all without panicking #1994

Closed
Androtim wants to merge 3 commits from upstream-pr/1996 into main
Androtim commented 2026-11-25 02:35:04 +00:00 (Migrated from github.com)

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


Root cause

detach_all() (registered via atexit) calls std::thread::spawn for the 5-second detach watchdog. During Windows ExitProcess teardown, CreateThread can fail with ERROR_ACCESS_DENIED (OS error 5), causing std::thread::spawn to panic inside atexit (unhandled exception 0x20474343 in crashes.log) instead of finishing DLL_PROCESS_DETACH.

Fix

  • Use std::thread::Builder::new().spawn(...) in crates/victory-loader/src/threads.rs instead of panicking on spawn.
  • On Ok, keep the existing watchdog TID wait loop and pass &[watchdog] to end_other_threads.
  • On Err, skip the 5-second watchdog wait loop and pass &[] to end_other_threads so DLL_PROCESS_DETACH runs immediately without stalling or panicking.
  • Add threads::tests::exit_process_runs_detach_all_without_panic (via exit_process_child calling attached(0) + ExitProcess(0)) to verify detach_all completes cleanly under ExitProcess.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings (passed)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (passed)
  • cargo test -p victory-loader --lib --locked (36 passed, 0 failed)
  • cargo test -p xtask --locked (6 passed, 0 failed)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6595873414 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-25T02:35:04Z. --- ### Root cause `detach_all()` (registered via `atexit`) calls `std::thread::spawn` for the 5-second detach watchdog. During Windows `ExitProcess` teardown, `CreateThread` can fail with `ERROR_ACCESS_DENIED` (OS error 5), causing `std::thread::spawn` to panic inside `atexit` (`unhandled exception 0x20474343` in `crashes.log`) instead of finishing `DLL_PROCESS_DETACH`. ### Fix - Use `std::thread::Builder::new().spawn(...)` in `crates/victory-loader/src/threads.rs` instead of panicking on `spawn`. - On `Ok`, keep the existing watchdog TID wait loop and pass `&[watchdog]` to `end_other_threads`. - On `Err`, skip the 5-second watchdog wait loop and pass `&[]` to `end_other_threads` so `DLL_PROCESS_DETACH` runs immediately without stalling or panicking. - Add `threads::tests::exit_process_runs_detach_all_without_panic` (via `exit_process_child` calling `attached(0)` + `ExitProcess(0)`) to verify `detach_all` completes cleanly under `ExitProcess`. ### Verification - `cargo clippy --workspace --all-targets --locked -- -D warnings` (passed) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (passed) - `cargo test -p victory-loader --lib --locked` (36 passed, 0 failed) - `cargo test -p xtask --locked` (6 passed, 0 failed)
DesertGamer commented 2026-11-25 03:03:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DesertGamer on 2026-11-25T02:40:47Z.


VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151036783 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7151036783). Originally posted by [DesertGamer](https://github.com/DesertGamer) on 2026-11-25T02:40:47Z. --- ### VIctory V2: community PRs, merged and tested Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch. **Where it stands today:** launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1). :warning: Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk. Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help: https://DesertGamer/VIctory-V2 *Unofficial community fork of Marvisak/VIctory, GPL-3.0.*
jeremygold02 commented 2026-11-25 03:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T02:59:22Z.


Triage verdict — Needs a regression test (267fc01)

Evidence

Checks and limits

  • In a standalone Windows probe with the same extern "C" atexit registration, both exit paths returned 0. The callback marker appeared with CRT exit(0) and was absent with ExitProcess(0). This was a synthetic runtime probe, not a rerun of the loader suite. CRT termination docs

Next step

  • Before landing, could the regression test assert that detach actually ran and force or observe the watchdog spawn-error branch? Invoking the CRT exit path with a callback marker would cover registration. A deterministic spawn failure would separately verify the fallback. That branch also runs detach without a watchdog, so I’d document that tradeoff explicitly.

This verification was AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7151471486 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7151471486). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T02:59:22Z. --- **Triage verdict — Needs a regression test** (`267fc01`) **Evidence** - The fallible watchdog spawn addresses a real panic path, but the new subprocess test can pass without reaching `detach_all`. At `267fc01`, [`attached(0)` registers the callback with the CRT](https://github.com/Marvisak/VIctory/blob/267fc01197b29d33747a56f1eb75e30efb79b47a/crates/victory-loader/src/threads.rs#L48-L61), then [`exit_process_child`](https://github.com/Marvisak/VIctory/blob/267fc01197b29d33747a56f1eb75e30efb79b47a/crates/victory-loader/src/threads.rs#L466-L472) calls Win32 `ExitProcess(0)` and the parent checks only exit status. **Checks and limits** - In a standalone Windows probe with the same `extern "C" atexit` registration, both exit paths returned 0. The callback marker appeared with CRT `exit(0)` and was absent with `ExitProcess(0)`. This was a synthetic runtime probe, not a rerun of the loader suite. [CRT termination docs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/exit-exit-exit?view=msvc-170) **Next step** - Before landing, could the regression test assert that detach actually ran and force or observe the watchdog spawn-error branch? Invoking the CRT exit path with a callback marker would cover registration. A deterministic spawn failure would separately verify the fallback. That branch also runs detach without a watchdog, so I’d document that tradeoff explicitly. This verification was AI-assisted.
catminou4 commented 2026-11-25 04:06:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:32:47Z.

Review: APPROVED


Verified locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b — cargo check -p victory-loader --locked passes clean (2 pre-existing warnings).

Thread-spawn failure in detach_all no longer panics — correct error propagation for a destructor-ish path that must not unwind. Note: victory-loader's test suite is broken on macOS/Linux on main pre-existing (prepare::tests parse Mach-O as PE), so runtime verification is Windows-only; the compile check + small diff is the evidence here. Approving.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051458531 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#pullrequestreview-6051458531). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T03:32:47Z. **Review:** APPROVED --- Verified locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b — `cargo check -p victory-loader --locked` passes clean (2 pre-existing warnings). Thread-spawn failure in `detach_all` no longer panics — correct error propagation for a destructor-ish path that must not unwind. Note: victory-loader's test suite is broken on macOS/Linux on main pre-existing (prepare::tests parse Mach-O as PE), so runtime verification is Windows-only; the compile check + small diff is the evidence here. Approving.
Androtim commented 2026-11-25 07:03:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Androtim on 2026-11-25T07:00:35Z.


Thanks for the catch @jeremygold02! Pushed commit f129d1f addressing all three points:

  • Replaced the ExitProcess(0) test with two deterministic subprocess tests:
    • atexit_runs_detach_all (atexit_child): registers a prior LIFO atexit callback and exits via CRT std::process::exit(0), asserting that detach_all ran (EXITING == true).
    • detach_all_handles_watchdog_spawn_failure (detach_spawn_failure_child): injects PermissionDenied into detach_all_with to exercise the watchdog spawn-error branch directly, verifying no panic, EXITING == true, and that other threads terminate.
  • Documented the spawn-failure tradeoff in start_detach_watchdog: when spawning fails during teardown, detach proceeds without a watchdog deadline rather than panicking across the extern "C" atexit boundary.

Verified locally on Windows 11 (x86_64-pc-windows-gnu):

  • cargo test -p victory-loader --lib --locked: 38 passed, 0 failed
  • cargo test -p xtask --locked: 6 passed, 0 failed
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features: clean
<!-- greyfinch-sync:Marvisak/VIctory:comment:7155389090 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7155389090). Originally posted by [Androtim](https://github.com/Androtim) on 2026-11-25T07:00:35Z. --- Thanks for the catch @jeremygold02! Pushed commit `f129d1f` addressing all three points: - Replaced the `ExitProcess(0)` test with two deterministic subprocess tests: - `atexit_runs_detach_all` (`atexit_child`): registers a prior LIFO `atexit` callback and exits via CRT `std::process::exit(0)`, asserting that `detach_all` ran (`EXITING == true`). - `detach_all_handles_watchdog_spawn_failure` (`detach_spawn_failure_child`): injects `PermissionDenied` into `detach_all_with` to exercise the watchdog spawn-error branch directly, verifying no panic, `EXITING == true`, and that other threads terminate. - Documented the spawn-failure tradeoff in `start_detach_watchdog`: when spawning fails during teardown, detach proceeds without a watchdog deadline rather than panicking across the `extern "C"` `atexit` boundary. Verified locally on Windows 11 (`x86_64-pc-windows-gnu`): - `cargo test -p victory-loader --lib --locked`: 38 passed, 0 failed - `cargo test -p xtask --locked`: 6 passed, 0 failed - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`: clean
Marvisak commented 2026-11-25 10:07:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:19:10Z.


Automated triage for the maintainers.
Merge candidate.

crates/victory-loader/src/threads.rs, detach_all: the watchdog is now started with std::thread::Builder::spawn (moved into start_detach_watchdog), so a failed CreateThread during teardown no longer panics inside the extern "C" atexit callback (which aborts the process). On failure, detach runs without the 5 s deadline and end_other_threads keeps no thread. The success path is unchanged from main. I checked that the moved WATCHDOG wait loop is identical, that the test helpers busy/stopped match main's signatures, and that the new tests cover both the CRT exit path (atexit_child, which asserts that detach_all actually ran, as an earlier review asked) and an injected spawn failure (detach_spawn_failure_child). No other open PR makes this change on its own; #1304 and #1456 touch the same file only as part of much larger rewrites.

Trade-off for the maintainer: if spawning fails, a detach that hangs is no longer bounded by the watchdog. The PR documents this, and it replaces an abort that happened every time.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158170495 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7158170495). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:19:10Z. --- <!-- victory-triage v1 head=fbef853a2f38856929d482924d45828baeadf2c3 verdict=merge-candidate at=2026-11-25T09:19:03Z --> Automated triage for the maintainers. **Merge candidate.** `crates/victory-loader/src/threads.rs`, `detach_all`: the watchdog is now started with `std::thread::Builder::spawn` (moved into `start_detach_watchdog`), so a failed `CreateThread` during teardown no longer panics inside the `extern "C"` `atexit` callback (which aborts the process). On failure, detach runs without the 5 s deadline and `end_other_threads` keeps no thread. The success path is unchanged from main. I checked that the moved `WATCHDOG` wait loop is identical, that the test helpers `busy`/`stopped` match main's signatures, and that the new tests cover both the CRT `exit` path (`atexit_child`, which asserts that `detach_all` actually ran, as an earlier review asked) and an injected spawn failure (`detach_spawn_failure_child`). No other open PR makes this change on its own; #1304 and #1456 touch the same file only as part of much larger rewrites. Trade-off for the maintainer: if spawning fails, a detach that hangs is no longer bounded by the watchdog. The PR documents this, and it replaces an abort that happened every time. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 23:03:31 +00:00
Marvisak commented 2026-11-25 23:05:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:04:51Z.


Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because the failure needs a toolchain the project does not build with. CreateThread is refused only when the atexit table runs after ExitProcess has begun, which is how the windows-gnu CRT behaves (the crash code cited, 0x20474343, is libgcc's). The repo pins x86_64-pc-windows-msvc, where the atexit table runs before ExitProcess, so the Err branch is unreachable there and the change would trade the watchdog's bounded exit for an unbounded one. If gnu builds are to be supported, three lines would do: Builder::spawn and an if let Ok around the wait.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175909535 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7175909535). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:04:51Z. --- Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because the failure needs a toolchain the project does not build with. `CreateThread` is refused only when the atexit table runs after `ExitProcess` has begun, which is how the windows-gnu CRT behaves (the crash code cited, 0x20474343, is libgcc's). The repo pins x86_64-pc-windows-msvc, where the atexit table runs before `ExitProcess`, so the `Err` branch is unreachable there and the change would trade the watchdog's bounded exit for an unbounded one. If gnu builds are to be supported, three lines would do: `Builder::spawn` and an `if let Ok` around the wait. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.
jaschinski97-ops commented 2026-11-26 16:05:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jaschinski97-ops on 2026-11-26T15:47:49Z.


Independent native Windows11 control now supports the toolchain distinction in the closing comment.

I built identical source 08b4585 (main63bcca43 plus the two Windows path/save fixes, without #1996) with Rust1.98.1 GNU and MSVC. The same tiny title returns 0,1,31,0x12345678,0x80000001 and0xFFFFFFFF. On GNU, all5 nonzero statuses become0xC0000409 after a teardown panic. On MSVC, all6 statuses are preserved and there is no panic on the same PC. Its four directory-change/DLL/storage controls pass too.

Source, exact matrix, commands and artifact SHA-256. MSVC build/test run passed19 app,156 HLE and35 loader tests before building the release executable. The earlier separate GNU experiment with original #1996 also passes, with its original author/history retained; this is verification evidence, not a new production PR.

AI-assisted with Codex; own synthetic programs only, no GPU/game data. No failure reproduced on the supported MSVC build.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196763497 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7196763497). Originally posted by [jaschinski97-ops](https://github.com/jaschinski97-ops) on 2026-11-26T15:47:49Z. --- Independent native Windows11 control now supports the toolchain distinction in the closing comment. I built identical source `08b4585` (main63bcca43 plus the two Windows path/save fixes, without #1996) with Rust1.98.1 GNU and MSVC. The same tiny title returns 0,1,31,0x12345678,0x80000001 and0xFFFFFFFF. On GNU, all5 nonzero statuses become0xC0000409 after a teardown panic. On MSVC, **all6 statuses are preserved and there is no panic** on the same PC. Its four directory-change/DLL/storage controls pass too. [Source, exact matrix, commands and artifact SHA-256](https://github.com/jaschinski97-ops/VIctory/blob/8312981/verification/windows-exit.md). [MSVC build/test run](https://github.com/jaschinski97-ops/VIctory/actions/runs/45850777211) passed19 app,156 HLE and35 loader tests before building the release executable. The earlier separate GNU experiment with original #1996 also passes, with its original author/history retained; this is verification evidence, not a new production PR. AI-assisted with Codex; own synthetic programs only, no GPU/game data. No failure reproduced on the supported MSVC build.

Pull request closed

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!1994
No description provided.