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

Closed
github-bot wants to merge 3 commits from gh-pr/1996/head into gh-pr/1996/base
Owner

@⁠Androtim on GitHub · 2026-11-25 02:35 UTC · original
Androtim:fix/detach-watchdog-spawn-panic → main · +143 −21 · 1 files · 3 commits · closed without merging

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)
**@⁠Androtim** on GitHub · 2026-11-25 02:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996) `Androtim:fix/detach-watchdog-spawn-panic` → `main` · +143 −21 · 1 files · 3 commits · closed without merging ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:00 +00:00
github-bot closed this pull request 2026-11-26 04:51:00 +00:00
Author
Owner

@⁠DesertGamer on GitHub · 2026-11-25 02:40 UTC · original

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.

**@⁠DesertGamer** on GitHub · 2026-11-25 02:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7151036783) ### 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.*
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 02:59 UTC · original · edited 2026-11-25 03:22 UTC

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.

**@⁠jeremygold02** on GitHub · 2026-11-25 02:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7151471486) · edited 2026-11-25 03:22 UTC **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.
Author
Owner

@⁠Androtim on GitHub · 2026-11-25 07:00 UTC · original

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
**@⁠Androtim** on GitHub · 2026-11-25 07:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7155389090) 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
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:19 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7158170495) <!-- 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)_
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:04 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 22:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7175909535) 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.
Author
Owner

@⁠jaschinski97-ops on GitHub · 2026-11-26 15:47 UTC · original

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.

**@⁠jaschinski97-ops** on GitHub · 2026-11-26 15:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#issuecomment-7196763497) 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.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 03:32 UTC · original · approved these changes

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.

**@⁠catminou4** on GitHub · 2026-11-25 03:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1996#pullrequestreview-6051458531) · **approved these changes** 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.

Pull request closed

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