Skip to content

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

Closed
Androtim wants to merge 3 commits into
Marvisak:mainfrom
Androtim:fix/detach-watchdog-spawn-panic
Closed

Androtim wants to merge 3 commits into
Marvisak:mainfrom
Androtim:fix/detach-watchdog-spawn-panic

Conversation

@Androtim

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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.

@jeremygold02

jeremygold02 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

@catminou4 catminou4 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.

Verified locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f — 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

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026
@jaschinski97-ops

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants