Skip to content

D3D12-GAME-ACTIONS-AND-WATCH-NO-STDERR-PANIC: never panic on closed stderr in game_actions and watch - #2945

Open
bobcatchris wants to merge 2 commits into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-game-actions-watch-stderr-panic
Open

bobcatchris wants to merge 2 commits into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-game-actions-watch-stderr-panic

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Summary

Claimed in the coordination issue as: D3D12-GAME-ACTIONS-AND-WATCH-NO-STDERR-PANIC (#2570 (comment)), following @KelboLive's review note on #2840.

Replaces panicking eprintln! calls on background/hook paths in apps/leonida with fallible let _ = writeln!(std::io::stderr(), ...):

  1. d3d12/apps/leonida/src/game_actions.rs:
    • log / log_to: writes %TEMP%\leonida-actions\log.txt first and writes to stderr with let _ = writeln!(std::io::stderr(), ...) so a closed stderr pipe at shutdown never panics the title frame hook or leonida-game-actions thread and never loses the log.txt entry.
    • deferred (unsafe extern "win64" fn): replaces the eprintln! in the catch_unwind error handler (which sat outside catch_unwind on an extern "win64" callback, where a stderr panic would abort across FFI) with log(...), keeping it panic-free and also recording the failure in log.txt.
  2. d3d12/apps/leonida/src/run.rs:
    • Extracts report_watch (under #[cfg(all(windows, feature = "diagnostics"))], placed below watch so tools/d3d12_switches.py keeps LEONIDA_WATCH's existing doc summary verbatim) using let _ = writeln!(std::io::stderr(), ...) so the detached LEONIDA_WATCH polling thread does not panic if stderr is closed.

Tests & Verification

  • Hardware / OS: Windows 11, Rust 1.98.1, no game files or GPU needed.
  • Red -> Green:
    • With eprintln! restored in log_to and report_watch, both new subprocess closed-stderr tests fail (RED_EXIT=101, child panics on closed stderr pipe):
      • game_actions::tests::log_writes_file_and_does_not_panic_when_stderr_pipe_is_closed
      • run::tests::watch_report_does_not_panic_when_stderr_pipe_is_closed
    • With the fix applied:
      • cargo test --locked -p leonida --bin leonida --features diagnostics -- run::tests game_actions::tests: 11 passed, 0 failed.
      • cargo test --locked -p leonida --bin leonida: 43 passed, 0 failed.
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md: exit 0.
  • python -I scripts/check_no_game_data.py d3d12/apps/leonida/src/game_actions.rs d3d12/apps/leonida/src/run.rs: exit 0.

AI Assistance

AI-assisted using Gemini 4 Neon, including a separate read-only independent review pass (which caught a d3d12_switches.py doc-comment shadowing risk in run.rs before opening the PR).

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent closed-stderr check for the launcher helpers on 8dc38550 merged into 1e3e316c. Under Win64 Wine 11.22, the two unmodified closed-pipe subprocess tests passed 3/3 each here and 3/3 each with #2946 and #2947 applied on the same base. The child checks that stderr really rejects a write. The action fixture verifies both file lines, and the watch fixture invokes the helper with three synthetic values.

Restoring the old eprintln! placement in log_to and the old write in report_watch on the combined tree made each of the same fixtures exit 101 in all three repeats. The game_actions::tests and run::tests groups passed 11/11 on both unmodified PR trees. The title watch worker and deferred game callback were outside this run.

MEASURED with synthetic inputs, OpenAI Codex and a separate read-only review.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 8dc3855. log_to writing the file sink before the fallible stderr write is the right order, and restoring eprintln! makes the child test exit 101, so the guard is real. The FFI path is safe too: log sits outside catch_unwind, but no longer panics, so the extern "win64" callback cannot unwind across the boundary. No diagnostic regression — game actions still reach log.txt (the deferred failure now gains a file entry too), and watch output is identical on a valid stderr.

Tests are 11/11 under diagnostics and 43/43 default, switches.md check passes, and the merge is clean against current main.

On the duplication with #2946/#2947: three PRs hand-rolling let _ = writeln!(stderr, ...) across four files is tolerable for one-line call sites, and a shared helper would add a leonida_core API for little gain — worth revisiting only if more detached diagnostic paths adopt it. Note the other detached paths still on eprintln! (player.rs, player_find.rs, native_scan.rs, agility.rs), which is outside this PR's claim but the natural next follow-up.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Updated in fast-forward commit $(git rev-parse --short HEAD) so watch_report_does_not_panic_when_stderr_pipe_is_closed also compiles and runs under default cargo test --workspace --exclude xtask --locked (.github/workflows/d3d12.yml:76), matching profile/stacks in #2947 (#[cfg(all(windows, any(feature = "diagnostics", test)))] on report_watch).

Verified locally:

  • cargo clippy --locked --no-deps -p leonida --all-targets -- -D warnings: 0 warnings, exit 0.
  • cargo build --locked -p leonida --all-targets: 0 warnings, exit 0.
  • cargo test --locked -p leonida --bin leonida -- run::tests game_actions::tests: 11 passed, 0 failed.
  • cargo test --locked -p leonida --bin leonida --features diagnostics -- run::tests game_actions::tests: 11 passed, 0 failed.

AI-assisted.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants