Repository navigation
hle, loader, app, docs: implement XLaunchRestartOnCrash - #47
PrivacyOnion wants to merge 5 commits into
Conversation
simnJS
left a comment
There was a problem hiding this comment.
Thanks for picking up #11. This is a clean, well-scoped change and the child-process test is a good idea. A few things before it can land:
- Overlapping processes: the replacement starts from inside the crash filter, and
wait_for_previousgives up after 30 s. If the crashed process lingers (a title filter, a Windows Error Reporting dialog) or the title's filter resumes execution, two instances share the title's files. Could the crash path end the process after spawning (asend()does), or could a crash restart wait without a time limit? XTP_CRASH_RESTARTEDis inherited by laterXLaunchNewGamerelaunches, so restart-on-crash stays off after the first restart. Please remove it at startup, likeXTP_RELAUNCHED_FROM.- Slot
0x28: public docs don't list the vtable order. How did you find it? A line in the PR or a comment would help future readers. - Docs: please update
docs/analysis/hle.md(the table row and R10), since the checklist says docs were updated.
Nits: use xasync::E_INVALIDARG; current does nothing when exe is empty. In the test, waiting for the grandchild to exit (or for a "faulting" marker) instead of a 400 ms sleep would make the "no third run" check solid, and the grandchild shouldn't be left running.
The gpu-debug-layer failure comes from xtp-gpu tests that landed on main today, not from your change; we're fixing it, and a rebase afterwards should clear it.
|
Addressed all review items and nits:
|
simnJS
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround — point 2 and the two code nits are resolved. A few small things remain:
end_withsaves the pipeline cache (and releases the mouse) from inside the crash filter; if that blocks (e.g. the faulting thread holds a pipeline-library lock), the replacement's 30 s wait expires and the overlap comes back. Could the crash path callTerminateProcessdirectly, or could a crash-restarted process wait without a limit?- Please say in the slot-0x28 comment that the order is inferred from declaration order, not confirmed.
- In
hle.md, the row should stay ◐ whileXLaunchNewGameis untested live, and the R10 Goal could say restart-on-crash is done. - Test: the "no third run" check can still race (a third run would only write its line after the grandchild exits). Asserting exit codes — 1 for the first child (from
end_with(1)) and0xC0000005for the grandchild — would make it deterministic. Please also kill the grandchild if the wait times out.
The gpu-debug-layer failures come from main, not from this PR.
|
Thanks @simnJS! Addressed all four items in
Verified locally with |
simnJS
left a comment
There was a problem hiding this comment.
Thanks, the last round is fully addressed: the crash path calls TerminateProcess directly, XTP_CRASH_RESTARTED is cleared at startup, the slot-0x28 comment says the order is inferred, the hle.md row stays ◐ with R10 updated, and the test asserts both exit codes and kills a stuck grandchild.
Two things I should have raised earlier:
-
The restart pre-empts the title's own filter.
run_on_crashruns insidextp_loader::report_unhandled, whichreport_unhandledinkernel.rs(L267–277 on main) calls before the filter the title registered. Since the hook now terminates, the title's filter never runs: whatever it does is skipped, and an exception it would resume (EXCEPTION_CONTINUE_EXECUTION) now ends and restarts the title. Could the hook run after the title's filter instead — from the kernel wrapper unless the filter returnedEXCEPTION_CONTINUE_EXECUTION, and from the loader's ownfilterwhen the title set none? The report itself can stay first. -
Unattended runs. Under
--autopilot(cargo xtask play), the replacement inherits--autopilotand the harness's pipes, while the harness sees exit code 1 and stops watching. The replacement keeps running the title unobserved, and the next session refuses to start because an xtp.exe is already running. Please skip the restart under--autopilotand log it instead, as #26 does for dialogs.
Test nit: nobody else holds a handle to the grandchild, so if it exits before OpenProcess, the exit-code check is skipped silently. Having the restarted probe wait for a file or event from the parent before faulting would close that.
Description: please mention the TerminateProcess(…, 1) exit and add docs/analysis/hle.md to the Area list.
fogydes
left a comment
There was a problem hiding this comment.
Review: PR #47 "hle, loader, app, docs: implement XLaunchRestartOnCrash" (issue #11)
Head reviewed: 476e92a (3 commits, base 448bb93, main is now f74e985). CI is green on the head,
and GitHub reports it MERGEABLE. I reviewed by reading only; no cargo build or test was run. Line numbers refer to the PR head.
Verdict: needs changes
The maintainer's points are almost all addressed. One new behaviour bug should be fixed before
merge: when restart is armed, the crash hook ends the process before the title's own
unhandled-exception filter runs (N1). The other findings are smaller.
1. Maintainer points (simnJS, reviews on 697953d and d3df5ae)
| # | Point | Addressed? | Where |
|---|---|---|---|
| R1.1 | Overlapping processes: end the crashed process after spawning, or wait without a limit | Yes | lifecycle.rs:278-287: TerminateProcess(GetCurrentProcess(), 1) right after a successful spawn. Side effect: see N1 |
| R1.2 | XTP_CRASH_RESTARTED inherited by later XLaunchNewGame relaunches |
Yes | lifecycle.rs:340-344 moves it into ALREADY_CRASH_RESTARTED and removes it at startup (run.rs:142, before any title thread) |
| R1.3 | How was slot 0x28 found? |
Yes | xgame.rs:178-181 comment |
| R1.4 | Docs: hle.md row and R10 |
Yes, one table missed | Row hle.md:180, R10 hle.md:375-378. The order table hle.md:513 ("11 | R10 relaunch, restart on crash") is not updated. #26 updated its equivalent row for R5 |
| Nit | xasync::E_INVALIDARG |
Yes | xgame.rs:100 |
| Nit | current unused when exe is empty |
Yes | xgame.rs:105 passes "" |
| Nit | Test: wait for the grandchild rather than sleep 400 ms; don't leave it running | Partly | restart_on_crash.rs:84-106 waits on its handle, but see T1 |
| R2.1 | end_with saves the pipeline cache from the crash filter; terminate directly |
Yes | lifecycle.rs:284-286. end() is back to its original form |
| R2.2 | Say slot 0x28 is inferred, not confirmed | Yes | xgame.rs:178-181 |
| R2.3 | Row stays ◐; R10 Goal says restart-on-crash is done | Yes | hle.md:180, hle.md:375-378 |
| R2.4 | Assert exit codes (1 and 0xC0000005); kill the grandchild if the wait times out |
Partly | Codes asserted at restart_on_crash.rs:39-43 and :101-104, kill at :87-93. If OpenProcess fails (:84), every check is skipped silently and the racy line count (:111) is all that is left (T1) |
2. New findings, by severity
N1 (High): restart-on-crash skips the title's own unhandled-exception filter
-
Where:
crates/xtp-loader/src/crash.rs:621-625(report_unhandlednow callsrun_on_crash).
Caller (unchanged):crates/xtp-hle/src/modules/kernel.rs:267-277. It calls
host::report_unhandled(info)first and the title's filter (TITLE_FILTER) afterwards. -
Problem:
report_unhandledused to only report. Now it runs the hook, and the hook calls
TerminateProcesswhen a replacement is spawned. Our filter runs ahead of the title's filter
(that is howcrash.rs:9-10documents it), so with restart armed the title's filter never runs. -
Scenario: a title calls
SetUnhandledExceptionFilterto write its own crash dump, flush
telemetry or saves, or handle a known fault and returnEXCEPTION_CONTINUE_EXECUTION. It also
callsXLaunchRestartOnCrashat boot. Its first unhandled fault then restarts the title, and its
filter never runs: no dump, nothing flushed, and a fault it would have recovered from becomes a
restart. The maintainer's first review warned about "the title's filter resumes execution". The
TerminateProcessfix turned "two instances" into "the title's filter is skipped". -
Fix: keep
report_unhandledreport-only and run the hook at the point where the process is
certain to die:- in
kernel.rsreport_unhandled, after the title's filter returns, unless it returned
EXCEPTION_CONTINUE_EXECUTION(-1); - in loader
report_crashes::filter(crash.rs:162-166) when no title filter is installed.
Add a test in which a title filter returns -1: no restart. And one in which it returns
EXCEPTION_CONTINUE_SEARCH: restart after the filter ran. - in
N2 (Medium): a crash while the title is ending can start a second process, or restart a title the user closed
- Where:
lifecycle.rs:265-288(relaunch_on_crash) does not check whetherend_title
(lifecycle.rs:205-236) is already running. - Scenario A: the title calls
XLaunchNewGame.end_titleruns the suspend routines, then
then()spawns the new game (:218-219). A title thread, orend()itself (release_mouse,
save_pipeline_cache), faults beforeTerminateProcessat:368. The hook then spawns a
crash-restart as well. Twoxtpprocesses wait on the same handle and both start the title,
which is the overlap R1.1 wanted to prevent, through another path. - Scenario B: the user closes the window and the title faults in a suspend routine (a common
place for shutdown bugs). The title restarts after the user quit it. - Fix: at the start of
end_title, takeRESTART_ON_CRASH
(RESTART_ON_CRASH.lock().take()). Or makeENDINGa module static and return early from
relaunch_on_crashwhen it is set.
N3 (Low/Medium): too much work in the crash filter on the faulting thread
-
Where:
lifecycle.rs:296-332(relaunch_with_env, called from the filter). -
Problem: inside the unhandled-exception filter, on the faulting thread, this code calls
tracing::info!/warn!(:324,:328), allocates (current_exe,Command, args, env block),
takes std's env lock, and callsCreateProcessW. -
Scenario:
- The fault is heap corruption, or the faulting thread holds the tracing writer or stdout lock:
the filter deadlocks, the process hangs, and there is no restart and no exit. EXCEPTION_STACK_OVERFLOW: the filter runs on the overflowed thread with only the guard
slack,Command::spawnoverflows again, and the process dies with no restart.
report()already allocates, so part of this risk exists today, but the spawn adds to it. - The fault is heap corruption, or the faulting thread holds the tracing writer or stdout lock:
-
Fix, either of:
- build the command line (UTF-16) once in
set_restart_on_crash, and callCreateProcessW
directly in the hook with no allocation and no tracing; log afterwards, or not at all; - start a helper thread from
XLaunchRestartOnCrashthat waits on an event. The filter signals
the event and waits a bounded time for "spawned". This also covers stack overflow.
- build the command line (UTF-16) once in
N4 (Low): crashes that do not pass through the unhandled filter never restart
- Where:
kernel.rs:247-256:terminateand_invalid_parameter_noinfo_noreturncallhost::abort;fastfail::install(crash.rs:176):__fastfail, failure exit codes.
- Problem: "restart on crash" only covers SEH exceptions that nobody handled. An uncaught C++
exception that ends instd::terminate, or a__fastfail, does not restart. The docs (hle.md
R10, the PR description) don't say this. - Fix: call the hook from the abort and fastfail paths too, or state the limit in R10 and in
therelaunch_on_crashdoc comment.
N5 (Low): the restarted process never restarts again, however long it runs
- Where:
lifecycle.rs:266-271.ALREADY_CRASH_RESTARTEDlasts for the whole life of the
process. - Problem: a crash-restarted session that runs for hours and crashes again is not restarted,
even if the title callsXLaunchRestartOnCrashagain. That prevents loops, but it is stricter
than "once per crash". I can't confirm the console's exact rule from public docs, so this is a
question rather than a defect. - Fix: clear the guard after a period without crashes (for example 60 s after startup), or
document "one restart per launch" in R10.
N6 (Low, existing code, now reused): title arguments are split on whitespace
- Where:
xgame.rs:126(args.split_whitespace()), now used byxgame.rs:105. - Problem:
XLaunchRestartOnCrash("-save \"slot 1\"", 0)arrives as-save,"slot,1".
Commandthen quotes each piece, so the title sees literal quote characters. - Injection: I checked this. A title string cannot become an
xtpoption: everything goes
after--, a later--in the string stays a title argument,take_while(|a| a != "--")stops
at our own separator, andCommandquotes each argument by MSVC rules. No injection. - Fix (follow-up, shared with
XLaunchNewGame): parse withCommandLineToArgvWrules
instead ofsplit_whitespace, and add a unit test forrelaunch_argswith quotes, an empty
string and an emptyexe.
N7 (Low): exit status and parent supervision
- Where:
lifecycle.rs:285. - Problem: the crashing process exits with
1, not the exception code. The new process is
detached, so whatever startedxtpsees a clean-looking exit 1 and stops supervising, while the
title keeps running unsupervised. For examplecargo xtask play --measurewaits on the first
xtpprocess. - Fix: exit with the exception code from the record. The test then needs another sign that the
restart path ran, for example a line the hook writes to the log. Or document exit code 1 and the
detached child in R10.
N8 (Nit): PR description is out of date
It describes the spawn without the TerminateProcess. Its Area list leaves out
docs/analysis/hle.md, and the pr-check box is unticked (local GPU failures are explained,
which is fine).
3. Repo rules (CONTRIBUTING.md)
- Area headers: the title and all 3 commits use
hle, loader, app[, docs]: ..., and
conventionspassed. - Tests without game files: OK. The child-process test is synthetic.
- Provenance: "all my own work" is ticked, AI help (Antigravity) is disclosed, and the
description says xcl was not read. - Rule 3 (console SDK): the slot order is "inferred from
XGame.hdeclaration order". The
public PC GDK header is not console-SDK material, and no code was transcribed. The maintainer
should still decide whether that inference is acceptable as provenance. The comment honestly
says "not confirmed". - No titles named: none in the added lines.
- Overlap with #26: both PRs change
apps/xtp/src/run.rsanddocs/analysis/hle.md. The
hunks don't touch (rows 176 and 180, three unchanged lines between them), so whichever lands
second needs a rebase, not a conflict resolution. The base is behind main (448bb93vs
f74e985). Main's later changes torun.rsare near lines 164-178 and 274, and do not overlap.
4. Test gaps
- T1: at
restart_on_crash.rs:84, ifOpenProcessreturns null, the exit-code checks are
skipped silently. Usepanic!(orassert!) instead ofif !handle.is_null(). Also, if
grandchild_pidisNone(:67), the test panics and a late grandchild keeps running. Kill by
PID on that path too, or have the child report its PID through a pipe or an inherited handle. - T2: no test covers a title filter being present (N1), or a crash during
end_title/XLaunchNewGame(N2). - T3:
relaunch_argshas no unit test for the restart shape: emptyexe, empty or null
args(no--), quoted args. - T4:
XLaunchRestartOnCrash(nullptr, 0)is not exercised (the probe passes null only with
reserved = 1). - T5: this is the first
harness = falsetest in the repo. It ignores test filters, so
cargo test -p xtp-hle lifecyclealso runs it, and it prints a faketest ... okline. The repo
already has a self-re-exec pattern for child-process tests (isolated()in
crates/xtp-loader/src/crash.rs:1958). A normal#[test]that re-executes itself with
--exactwould fit better. - T6: on a developer machine with the WER UI enabled, the grandchild's second fault can show a
dialog and hit the 10 s timeout. CI doesn't show WER dialogs.
Review drafted with Claude Code; findings checked against the PR head by reading the code.
476e92a to
b04fb41
Compare
The description now mentions |
simnJS
left a comment
There was a problem hiding this comment.
Thanks, this addresses my last review:
- The restart now runs after the title's filter, and is skipped when the filter resumes execution.
- Autopilot logs an armed restart and skips it.
- The probe holds the replacement's handle before it faults.
- The description is updated.
Two small points, neither blocking:
- In the
hle.mdsummary, row 11 puts its status in the "Depends on" column. - If a suspend routine faults during
end_title, the title would still restart. ClearingRESTART_ON_CRASHat the start ofend_titlewould fix that; a follow-up is fine.
CI is green apart from the known gpu-debug-layer failures, which also fail on main; we're fixing those there, and I'll merge once that job is green. Approving.
|
Thanks for the review. The follow-up is
Follow-ups from the review are moving restart work off the faulting thread (N3), evaluating abort and fast-fail coverage (N4), confirming the restart guard’s lifetime against the console (N5), parsing quoted arguments for both launch APIs (N6), and revisiting exit status and replacement supervision (N7). |
simnJS
left a comment
There was a problem hiding this comment.
Re-reviewed d50e45d. Both nits are fixed:
- row 11's status is in the item column;
end_titleclearsRESTART_ON_CRASHright after settingENDING, whichset_restart_on_crashand the crash hook also check.
fogydes's N1, N2 and T1–T5 look resolved; N3–N7 remain follow-ups. @fogydes, could you re-review when you have a moment? I'll merge once CI is green and your request is cleared.
What and why
Implement
XLaunchRestartOnCrashatTITLE_VTABLEslot0x28, inferred from the declaration order inXGame.h. The HLE call rejects a nonzeroreservedwithE_INVALIDARGand otherwise records the restart arguments and returnsS_OK.EXCEPTION_CONTINUE_EXECUTIONresumes execution with the restart still armed. The loader’s default filter handles the case where the title registers no filter.TerminateProcess(GetCurrentProcess(), 1).XTP_RELAUNCHED_FROMcarries the process handle the replacement uses to wait for its predecessor. The replacement consumesXTP_CRASH_RESTARTEDat startup to limit the launch to one crash restart. A laterXLaunchNewGamestarts a fresh launch.--autopilot, the hook logs that it skipped the restart so the supervisor can observe the original exception exit.Fixes: #11
Depends on: none
Area
crates/xtp-loader/src/crash.rs,crates/xtp-loader/src/lib.rscrates/xtp-hle/src/modules/kernel.rs,crates/xtp-hle/src/modules/lifecycle.rs,crates/xtp-hle/src/modules/xgame.rscrates/xtp-hle/Cargo.toml,crates/xtp-hle/tests/restart_on_crash.rsapps/xtp/src/run.rsdocs/analysis/hle.mdTests
cargo xtask pr-checkpasses on my machinecargo xtask pr-check --base upstream/main --title "hle, loader, app, docs: implement XLaunchRestartOnCrash" --skip testpassed formatting, both Clippy configurations, policy and conventions. The workspace test step was skipped.cargo test -p xtp-hle --lib,cargo test -p xtp-hle --test restart_on_crash, andcargo test -p xtp-loaderpassed.The five crash-restart tests re-execute their own test binary with
--exact. They cover the restart and loop guard, null arguments, the title filter resuming execution and then letting a fatal exception through, unattended mode, and a fault in a suspend routine duringXLaunchNewGame. The parent verifies exit codes and counts all processes that ran. A Windows job cleans up surviving probe processes on a failure. Unit tests cover empty executable overrides, empty and null arguments, and how the shared whitespace split handles quotes.Provenance
The original implementation used public Microsoft Learn documentation for
XLaunchRestartOnCrashand existing XTP relaunch and crash-reporting code. Review fixes use XTP’s child-process test pattern and the existing Windows bindings. No source from xcl or AnyPS5 was read or incorporated.AI assistance: AI coding tools wrote parts of this change: Antigravity (Gemini) the original implementation and test, OpenAI Codex the review changes.
Checklist
<area>: <summary>. The diff is 596 changed lines. The synthetic Windows crash probes account for 382 of those lines and cover process cleanup, synchronized exit-code checks, and the five restart scenarios.