Skip to content

xcl run: xcl.exe uses the segment heap; the exit is logged before ntdll locks the heap - #3014

Draft
Anon64 wants to merge 1 commit into
xcl-dev:mainfrom
Anon64:build/segment-heap
Draft

Anon64 wants to merge 1 commit into
xcl-dev:mainfrom
Anon64:build/segment-heap

Conversation

@Anon64

@Anon64 Anon64 commented Nov 27, 2026 •

Copy link
Copy Markdown

xcl.exe asks Windows for the segment heap as its process heap, through a manifest embedded at link time. During the shader preparation, DXC and NVIDIA's compiler allocate from the process heap on many threads at once, and the NT heap's lock serialises them (#156, @wondercrash's finding). In a cold preparation of a retail title on NVIDIA, the segment heap took 9.9 % less time: 3 357 s against 3 724 s, the mean of two runs each (numbers on #156).

The segment heap brought out a hang at process exit. XCL's NtTerminateProcess hook logged during ntdll's NtTerminateProcess(0, …), which runs while ntdll holds the loader lock and the process heap's lock. The NT heap lets the thread that holds its lock take it again; the segment heap does not, so every title that ended through ExitProcess hung there. That included the e2e test title. It was found with an in-process probe of the loader lock's owner and its stack (details on #156). The end of the process is now logged from a hook on RtlExitUserProcess, before ntdll takes those locks and while the other threads still run, so the thread dump on a non-zero exit is kept. TerminateProcess of the process itself still goes through the NtTerminateProcess hook.

Part of #156. Related: #2698 fixes the same kind of wait on another path. Its watchdog's TerminateProcess goes through the same hook while a suspended thread holds the heap, and it calls the real NtTerminateProcess there. Both PRs change eh.rs, so whichever lands second needs a small rebase.

What is in it

  • crates/xcl/xcl.manifest (new) and crates/xcl/build.rs: the manifest (<heapType>SegmentHeap</heapType>), embedded into the xcl binary on MSVC with /MANIFEST:EMBED /MANIFESTINPUT:.
  • crates/xcl/src/eh.rs: the RtlExitUserProcess hook, with the logging shared with the NtTerminateProcess hook (log_process_end).
  • crates/xcl/tests/segment_heap.rs (new): xcl.exe has a process manifest that asks for the segment heap.
  • crates/xcl/tests/e2e.rs: the test title's end is in the log. Without the hook change the test hangs at the title's exit under the segment heap.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (driver 581.80), Intel i9-14900KF, 64 GB RAM, Windows 11 26200, Rust stable MSVC.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 (debug), on main 06f90c19 plus this change: 1459 passed, 0 failed, 47 ignored.
  • cargo test --release -p xcl --test e2e: 4 passed. Before the eh.rs change, all four hung at the title's exit.
  • Memory: the retail title, 240 s, XCL_ACTIVITY=1, warm caches, one run each, main 06f90c19 against this PR (f0ccacd7). Peak process commit 37 063 MB against 36 908 MB (−0.4 %), local video memory 18 029 against 18 102 MB, frames 1 795 against 1 793. So the segment heap leaves RAM and VRAM use where they were; the gain is the preparation time.

Regression run
scripts/smoke.ps1 -Game <title folder>, 60 s, release, A pressed every 4 s from 20 s, warm caches:

main:

xcl-smoke v1
commit: 06f90c1986f43698d0405eff3c2ab0af230e1ed9
gpu: NVIDIA GeForce RTX 3060
title: retail-1
seconds: 60
exit: 0x0
frames shown: 1387 (PresentX 1387)
presents per 2 s (last 3): 10 23 41
device removals: 0
crashes: 0
peak commit: 27730 MB

this PR:

xcl-smoke v1
commit: f0ccacd77d3c039f794b3ed55a01263f9268ca0f
gpu: NVIDIA GeForce RTX 3060
title: retail-1
seconds: 60
exit: 0x0
frames shown: 1391 (PresentX 1391)
presents per 2 s (last 3): 7 27 21
device removals: 0
crashes: 0
peak commit: 26996 MB

Not tested

  • AMD; Linux / Wine. Under Wine the manifest's heap type is probably ignored; not checked.
  • A warm-cache preparation time. The 9.9 % is from cold runs only.
  • RAM use past 240 s (a full world load) under the segment heap.

AI use

  • Claude Code (Claude Opus 5.5) found the hang's cause, wrote the code, the tests and this description, and ran the tests and the title runs on this machine.

🤖 Generated with Claude Code

…ll locks the heap

xcl.exe's manifest (embedded by build.rs on MSVC) asks Windows for the
segment heap as the process heap. During the shader preparation DXC and
NVIDIA's compiler allocate from it on many threads at once, and the NT
heap's lock serialises them (xcl-dev#156): a cold preparation of a retail title
took 3357 s against 3724 s (mean of two runs each, RTX 3060).

The segment heap brought out a hang at every ExitProcess: the
NtTerminateProcess hook logged (and allocated) during ntdll's
NtTerminateProcess(0, ...), which runs under the loader lock and the
process heap's lock. The NT heap lets the same thread take that lock
again; the segment heap does not. The end is now logged from a hook on
RtlExitUserProcess, before those locks and while the other threads still
run, so the thread dump of a non-zero exit is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Royalflamejlh

Copy link
Copy Markdown
Contributor

Under Wine + vkd3d-proton on an RTX 4070, the RtlExitUserProcess hook installed and logged the test title's exit in 2 of 2 e2e runs, and two 120 s runs of the title ended as they do without this change. A GNU build gets no manifest (build.rs embeds it on MSVC only), so only the eh.rs change applies under Wine.

Setup: main 623d02e4 + #684 (which our runs need) + this commit picked (06f90c19 changes only licence headers), against the same tree without it; release, x86_64-pc-windows-gnu, Wine 11.21 Staging + vkd3d-proton v3.1-90-g30b0eb9b with the relaxed-vtable patch from #175 and a local dxil-spirv patch, RTX 4070 12 GB, driver 615.71.09. Title runs: 120 s, A pressed once the title presented steadily, XCL_PREPARE=0, XCL_PSO_PRELOAD=0, the first-run screens completed once, no story progress.

Run Tree Last 2 s window with presents ends at Frames End of the process logged from
title 1 without 72.1 s 1338 hk_nt_terminate_process (from end_title)
title 2 without 70.0 s 1357 hk_nt_terminate_process (from end_title)
title 3 with 70.1 s 1372 hk_nt_terminate_process (from end_title)
title 4 with 70.3 s 1333 hk_nt_terminate_process (from end_title)
e2e × 2 without — — hk_nt_terminate_process
e2e × 2 with — — hk_rtl_exit_user_process
  • The title runs do not reach the new hook: XCL_RUN_SECONDS=120 ends them through end_title, whose TerminateProcess of the process itself (real.rs:3629) still goes through NtTerminateProcess. No hook warning was logged.
  • test_title_runs_and_game_folder_is_untouched failed in all four e2e runs at e2e.rs:159, on the Wine difference it has on main here ("W: not cloaked"), so the new assertion at e2e.rs:171 is never reached under Wine. The test title then exits with status 1; both trees logged that and a dump of 46 threads, and each e2e run took 3.6 s, so neither tree hung at the exit.

Not tested: native Windows; the segment heap itself (no manifest in a GNU build); the shader preparation's time; the whole test suite.

AI use: Claude Code (Claude Opus 5.5) ran the title and the tests and wrote this comment.

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.

2 participants