Skip to content

KernelX: lazy pages – a thread that loses the race to commit a page retries instead of crashing - #250

Closed
Sr-0w wants to merge 1 commit into
ivall:mainfrom
Sr-0w:lazy-commit-race
Closed

Sr-0w wants to merge 1 commit into
ivall:mainfrom
Sr-0w:lazy-commit-race

Conversation

@Sr-0w

@Sr-0w Sr-0w commented Nov 23, 2026 •

Copy link
Copy Markdown

What happened

About two minutes into play (in the world, frame ~1400), the retail game crashed. Two threads faulted at the same millisecond in the same 64 KiB page:

unhandled exception 0xc0000005 in …+0x13936 (write at address 0x296cd3b2010; region 0x296cd3b2000+0xe000 of allocation 0x29289f90000 (protect 0x4), state 0x1000, protect 0x4, type 0x20000)
unhandled exception 0xc0000005 in …+0x13975 (write at address 0x296cd3b0000; region 0x296cd3b0000+0x10000 of allocation 0x29289f90000 (protect 0x4), state 0x1000, protect 0x4, type 0x20000)

(The region part comes from a local VirtualQuery in the crash logger, not part of this PR.) By the time the crash is logged, the page is committed read-write. That is a lazy physical page from 38b05b7's handler, which one thread committed while the other was faulting on it.

Cause

lazy::handler first checks LAZY under a read lock, then commit takes the write lock, removes the page and commits it. When two threads fault on one page:

  • the second one sees it lazy, waits for the write lock, finds it already removed: commit returns false; or
  • it sees it already removed from LAZY.

In both cases it returns EXCEPTION_CONTINUE_SEARCH and the process dies.

Change

If the page of a read or write fault is now committed PAGE_READWRITE, the handler returns EXCEPTION_CONTINUE_EXECUTION, so the access is retried. A read or write on such a page cannot fault again, so this cannot loop.

Testing

  • New test threads_racing_on_a_lazy_page_all_go_on: 8 threads write into the same 64 lazy pages behind a barrier.
    • Without the change, the test process dies with 0xc0000005 (3 runs out of 3).
    • With it, the test passes.
  • cargo test --release -p xcl --lib: 612 passed, 0 failed. A second run while the game was using the GPU had GPU-test failures (gpu_sparse_vram_descriptors_buffers_and_xmem, gpu_float_compares_random, …). They pass alone and on a quiet GPU.
  • In-game check with the change: in progress (the crash came about two minutes into play).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA

…he race retries

lazy::handler commits a lazy page on its first access. When two threads fault on one page at
once, the first removes it from LAZY and commits it; the second either finds it already
removed (commit returns false) or no longer lazy, passes the fault on, and the process dies on
a write to memory that is committed read-write by the time the crash is logged. A fault on a
page that is now committed read-write is retried.

The retail game crashed this way about two minutes into play (two threads building objects in
one page, at the same millisecond).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA
@Cattn

Cattn commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This is a duplicate of #244 in pretty sure

@Sr-0w

Sr-0w commented Nov 23, 2026

Copy link
Copy Markdown
Author

Closing as a duplicate of #244, which fixes the same race and was opened first. I hit the same two-thread crash in the same title and confirmed the fix there: past the point where it used to crash.

@Sr-0w Sr-0w closed this Nov 23, 2026
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This fixes the race: 10 of 10 runs pass on each of the three race tests below, all of which abort main 63b9930 every time. There is one case where the fallback retries a fault that can't succeed, though.

Finding: an endless retry on a genuine fault.

  • committed_rw asks whether the 64 KiB page's base is committed PAGE_READWRITE, and that's enough for any read or write to retry.
  • Take a lazy page that was committed and then had a 4 KiB part made read-only with VirtualProtect. A write to that part faults, the base is still read-write, so the handler returns CONTINUE_EXECUTION, and the write faults again, forever.
  • In a test, a real write like that hangs until killed: 15 s, no progress. On main and on Lazy physical pages: a thread that loses the race to commit a page retries #244 the same write is reported as the access violation it is.
  • Lazy physical pages: a thread that loses the race to commit a page retries #244 avoids it by querying the faulting address (ExceptionInformation[1]) and checking the access against that protection: reads need a readable page, writes a writable one, and guard pages are refused. Doing the same in committed_rw would close the case.

Tested

  • Windows 11, MSVC, debug, CPU only. Synthetic pages only. This PR at 093798e, merged onto main 63b9930 without conflicts.
  • Three race tests, 10 runs each, under a debugger that catches the second-chance exception: this PR's threads_racing_on_a_lazy_page_all_go_on, Lazy physical pages: a thread that loses the race to commit a page retries #244's test, and a two-thread test with a barrier per page. Main: an access violation, 10 of 10 each. This PR: passes 10 of 10 each.
  • The endless retry: a direct call of handler with a write record for the read-only part returns CONTINUE_EXECUTION. A real write in a test (#[ignore]d, run with a 15 s timeout) never returns.

Not tested

  • A title.
  • Wine.

🤖 Claude Opus 5.5

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