[PR #1725] tests: heap-lock touchers allocate from the process heap, not the debug CRT (RT-XMEM-HEAP-LOCK-DEADLOCK) #1740

Open
opened 2026-11-26 21:18:47 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1725
Author: @Tyrannys
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: claude/xmem-heap-deadlock


📝 Commits (2)

  • 596fe1f tests: xmem heap-lock toucher allocates from the process heap, not the debug CRT
  • 7c0215d tests: d3d12_existing_heap heap-lock touchers allocate from the process heap

📊 Changes

2 files changed (+14 additions, -8 deletions)

View changed files

📝 runtime/test/d3d12_existing_heap_test.cpp (+6 -4)
📝 runtime/test/xmem_test.cpp (+8 -4)

📄 Description

Claimed in the coordination issue as: RT-XMEM-HEAP-LOCK-DEADLOCK (ubiquitarianism/leonida#1608)

Item: RT-XMEM-HEAP-LOCK-DEADLOCK (kotekompotek's finding in #1608: xmem_test hangs in the "xmem heap-lock faults" section in a Debug build on Windows, all three variants).

Cause (measured, not xmem)

At the hang I walked every test thread's stack from the watchdog (temporary instrumentation, not committed):

  • a toucher holds HeapLock(GetProcessHeap()) and is in the debug CRT's _aligned_free → _free_dbg → EnterCriticalSection, waiting for the CRT's own heap lock;
  • another thread is in exception dispatch → vcruntime140d __CxxFrameHandler3 → _calloc_dbg, which holds that CRT lock, → RtlAllocateHeap, waiting for the process heap.

xmem's g_fault_lock isn't in the cycle, and the heap gate can't stop it because the CRT's internal calloc doesn't go through operator new. A release CRT's _aligned_malloc is just HeapAlloc and takes no lock of its own, which is why RelWithDebInfo passes, and why the title's XMemAlloc (its own release CRT) can't hit this. So the bug is in the test's emulation, not in the fault handler.

d3d12_existing_heap_test's check_heap_lock_faults has the same pattern and hangs the same way in Debug (all three variants: "vulkan slot-133 heap-lock faults: no progress for 30 s"). Its allocator threads take the CRT lock and then the heap lock directly.

Fix

The touchers in both tests HeapAlloc/HeapFree on the process heap while they hold its lock, instead of the test binary's _aligned_malloc/_aligned_free. They still write heap block headers under the heap's lock while faulting, which is what the section tests. Nothing is skipped and no check changes. The comments say why.

Tested

Windows 11 Pro (26300), MSVC 14.52, RTX 4070 SUPER, no game files:

  • Debug, before: xmem_test xmem exit 3 on every run (watchdog). After: no hang in 105 runs of the three xmem* variants. Debug ctest xmem* and d3d12_existing_heap*: 6/6 pass.
  • RelWithDebInfo: full ctest 400/402. The two failures were xmem_write_watch ("the reader of the views' last pages never faults or misreads") and an api_ref_state 30 s timeout. Both then passed --repeat until-fail:3 together with the other heap-lock tests (7/7 ×3).
  • Unrelated to this change but seen: two other xmem checks fail intermittently, both before the heap-lock section. "writing to 512 of them charges about those 512 pages" is the system-wide commit-charge check #1316 owns; it failed once in 30 quiet runs. "the reader of the views' last pages…" failed only while a parallel build loaded the machine. A full Debug build also fails on vk_presenter_sync_test.cpp (C1128, needs /bigobj), and the Debug run_tiny_* and replay_* tests fail on this machine. None of them link the two test files changed here.
  • python -I scripts/check_no_game_data.py on both files: clean.

djrolfes' Wine 11.22 read-only XMem findings in #1608 have a different cause (VirtualQuery state under Wine). This PR doesn't touch them, and the Wine skip of the heap-lock section stays as it is.

AI-assisted (Claude Code).

GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Pro 26300

AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1725 **Author:** [@Tyrannys](https://github.com/Tyrannys) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `claude/xmem-heap-deadlock` --- ### 📝 Commits (2) - [`596fe1f`](https://github.com/ubiquitarianism/leonida/commit/596fe1f6aca8514da79b09543d290836566b9a0e) tests: xmem heap-lock toucher allocates from the process heap, not the debug CRT - [`7c0215d`](https://github.com/ubiquitarianism/leonida/commit/7c0215dc296478aedc97ba996bb1c2309b7a55e4) tests: d3d12_existing_heap heap-lock touchers allocate from the process heap ### 📊 Changes **2 files changed** (+14 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `runtime/test/d3d12_existing_heap_test.cpp` (+6 -4) 📝 `runtime/test/xmem_test.cpp` (+8 -4) </details> ### 📄 Description Claimed in the coordination issue as: RT-XMEM-HEAP-LOCK-DEADLOCK (ubiquitarianism/leonida#1608) Item: **RT-XMEM-HEAP-LOCK-DEADLOCK** (kotekompotek's finding in #1608: `xmem_test` hangs in the "xmem heap-lock faults" section in a Debug build on Windows, all three variants). ## Cause (measured, not xmem) At the hang I walked every test thread's stack from the watchdog (temporary instrumentation, not committed): - a toucher holds `HeapLock(GetProcessHeap())` and is in the **debug CRT's** `_aligned_free` → `_free_dbg` → `EnterCriticalSection`, waiting for the CRT's own heap lock; - another thread is in exception dispatch → vcruntime140d `__CxxFrameHandler3` → `_calloc_dbg`, which **holds that CRT lock**, → `RtlAllocateHeap`, waiting for the process heap. xmem's `g_fault_lock` isn't in the cycle, and the heap gate can't stop it because the CRT's internal `calloc` doesn't go through `operator new`. A release CRT's `_aligned_malloc` is just `HeapAlloc` and takes no lock of its own, which is why RelWithDebInfo passes, and why the title's `XMemAlloc` (its own release CRT) can't hit this. So the bug is in the test's emulation, not in the fault handler. `d3d12_existing_heap_test`'s `check_heap_lock_faults` has the same pattern and hangs the same way in Debug (all three variants: "vulkan slot-133 heap-lock faults: no progress for 30 s"). Its allocator threads take the CRT lock and then the heap lock directly. ## Fix The touchers in both tests `HeapAlloc`/`HeapFree` on the process heap while they hold its lock, instead of the test binary's `_aligned_malloc`/`_aligned_free`. They still write heap block headers under the heap's lock while faulting, which is what the section tests. Nothing is skipped and no check changes. The comments say why. ## Tested Windows 11 Pro (26300), MSVC 14.52, RTX 4070 SUPER, no game files: - Debug, before: `xmem_test xmem` exit 3 on every run (watchdog). After: no hang in 105 runs of the three `xmem*` variants. Debug ctest `xmem*` and `d3d12_existing_heap*`: 6/6 pass. - RelWithDebInfo: full `ctest` 400/402. The two failures were `xmem_write_watch` ("the reader of the views' last pages never faults or misreads") and an `api_ref_state` 30 s timeout. Both then passed `--repeat until-fail:3` together with the other heap-lock tests (7/7 ×3). - Unrelated to this change but seen: two other xmem checks fail intermittently, both before the heap-lock section. "writing to 512 of them charges about those 512 pages" is the system-wide commit-charge check #1316 owns; it failed once in 30 quiet runs. "the reader of the views' last pages…" failed only while a parallel build loaded the machine. A full Debug build also fails on `vk_presenter_sync_test.cpp` (C1128, needs `/bigobj`), and the Debug `run_tiny_*` and `replay_*` tests fail on this machine. None of them link the two test files changed here. - `python -I scripts/check_no_game_data.py` on both files: clean. djrolfes' Wine 11.22 read-only XMem findings in #1608 have a different cause (VirtualQuery state under Wine). This PR doesn't touch them, and the Wine skip of the heap-lock section stays as it is. AI-assisted (Claude Code). GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Pro 26300 AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1740
No description provided.