Skip to content

KernelX: memory tracing keeps the caller's last error - #226

Closed
Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/kernelx-test-flake
Closed

Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/kernelx-test-flake

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly fails on main now and then (last error 203 where 8 is expected), now that CI runs the tests. The cause is in the code, not the test.

Cause

trace_mem reads XCL_TRACE_MEM into a OnceLock on its first call. With the variable unset, GetEnvironmentVariableW sets the thread's last error to 203 (ERROR_ENVVAR_NOT_FOUND). XMemAllocDefault and the VirtualAlloc hooks trace after the call has set its error. So the first traced call in a process replaced its own error with 203: XMemAllocDefault(usize::MAX, 0) reported 203 instead of ERROR_NOT_ENOUGH_MEMORY. A title would see the same.

The test fails whenever it is the process's first caller of trace_mem. That happens every time when it runs alone, and in some runs of the whole module, depending on which test thread gets there first.

Change

trace_mem runs its body under keep_last_error, a 6-line helper that saves the thread's last error and puts it back. That covers the first-call lookup and the logging.

Tested

Windows 11, MSVC, debug, CPU only; no GPU device is created by these tests.

  • The test alone, cargo test -p xcl --lib -- --exact hle::kernelx::tests::xmem_and_physical_page_overflows_fail_cleanly, 10 runs each: main af20b4b fails 10 of 10 (left: 203, right: 8); this branch (594f63a) passes 10 of 10.
  • The module, hle::kernelx with the library test binary built from each commit, 30 runs each: main af20b4b fails 9 of 30, every time in this test with 203; this branch fails 0 of 30 (16 tests per run, including the new one).
  • New test tracing_keeps_the_last_error. It sets ERROR_NOT_ENOUGH_MEMORY, then checks that the error survives an unset-variable lookup under keep_last_error, and a trace_mem call. It doesn't depend on test order. With the restore removed it fails with 203.

Not tested

AI use: Claude Code, Claude Opus 5.5.

trace_mem reads XCL_TRACE_MEM on its first call. When the variable is unset
that read sets the thread's last error to 203 (ERROR_ENVVAR_NOT_FOUND), which
replaced the error the traced call had just set. XMemAllocDefault's
ERROR_NOT_ENOUGH_MEMORY, for one, reached the title as 203 when it was the
process's first trace. xmem_and_physical_page_overflows_fail_cleanly failed
for that reason whenever it ran before every other test that traces: always
alone, and in some runs of the whole module.

trace_mem now runs under keep_last_error, which puts the error back after
it.

Test: tracing_keeps_the_last_error. It fails without the restore, in any
test order.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main d4f17ca (adeac80). The only conflict was the test module, next to the affinity tests #221 added; both are kept. cargo test --release -p xcl --lib hle::kernelx -- --test-threads=1: 18 passed, including tracing_keeps_the_last_error and xmem_and_physical_page_overflows_fail_cleanly.

🤖 Claude Opus 5.5

@Kalmarv
Kalmarv force-pushed the fix/kernelx-test-flake branch from 594f63a to adeac80 Compare November 23, 2026 21:26
ivall added a commit that referenced this pull request Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Merged into main (0f34407) as 594f63a, the head before your rebase - the rebased one has the same content (main and your head agree on every file it touches), so GitHub does not show it as merged; closing by hand. Checked on an RX 9070 (RDNA4): cargo build --workspace && cargo test --workspace -- --test-threads=4 - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.

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