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

Closed
Sr-0w wants to merge 1 commit from upstream-pr/250 into main
Sr-0w commented 2026-11-23 21:52:49 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Sr-0w on 2026-11-23T21:52:49Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6569578284 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/250). Originally posted by [Sr-0w](https://github.com/Sr-0w) on 2026-11-23T21:52:49Z. --- ## 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.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:57:50 +00:00
Cattn commented 2026-11-24 07:04:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Cattn on 2026-11-23T21:58:06Z.


This is a duplicate of #244 in pretty sure

<!-- greyfinch-sync:ivall/xcl:comment:7115154774 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/250#issuecomment-7115154774). Originally posted by [Cattn](https://github.com/Cattn) on 2026-11-23T21:58:06Z. --- This is a duplicate of #244 in pretty sure
Sr-0w commented 2026-11-24 07:04:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Sr-0w on 2026-11-23T22:01:05Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7115217438 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/250#issuecomment-7115217438). Originally posted by [Sr-0w](https://github.com/Sr-0w) on 2026-11-23T22:01:05Z. --- 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.
Kalmarv commented 2026-11-24 07:04:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T22:02:06Z.


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 #244 the same write is reported as the access violation it is.
  • #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, #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

<!-- greyfinch-sync:ivall/xcl:comment:7115239532 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/250#issuecomment-7115239532). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T22:02:06Z. --- 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 #244 the same write is reported as the access violation it is. - #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`, #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

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
xcl-dev/xcl!247
No description provided.