runtime: hold the unmapped window for the reader-without-retry control #1841

Merged
Makto94 merged 1 commit from upstream-pr/1848 into main 2026-11-24 19:49:55 +00:00
Makto94 commented 2026-11-24 19:30:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-24T19:30:27Z.


Problem

xmem_shim_page_runs is flaky in its negative control reader-without-retry:

AssertionError: reader-without-retry: no fault: 'child: reader starts\nchild: reader survived\n'

It failed once in a serial main run (run 45421896173, Debug job 138545800299) and in the --parallel 4 run discussed in #1173. The control needs a Python reader thread to read page 20 in the short window between UnmapViewOfFile2 and the remap of the shared view inside XMemMapPhysicalPagesScatter. When the reader never lands there in 3 s, the child survives and the check fails.

Refs #1173, #1306

Change

  • src/xmem_mapped_pages.cpp, test build only (#ifdef X2W_XMEM_TEST_CONTROLS): with X2W_XMEM_TEST_HOLD_WINDOW=<name>, an unmap that took a run apart sets the event <name>.unmapped, then waits up to 5 s for <name>.release before mapping again. In the deployed shim hold_window() is empty.
  • tests/xmem_page_runs_check.py: the reader-without-retry child waits for <name>.unmapped without the GIL and reads the unmapped page, so the fault happens every time. If the hold never comes, the child says so and the parent fails with "the hold hook did not run" instead of "survived". The deployed-shim control now runs as a hold-ignored child with both variables set (still 3 s of toggling) and also checks that no unmap waited in the window.
  • docs/startup-runtime.md: names the new test switch next to X2W_XMEM_NO_FAULT_RETRY.

Not changed: the positive reader case (reader_while_toggling with the handler) still does not require an observed retry.

Verification

Windows 11 Pro 26300.9974, MSVC 19.44.35230 x64, Python 3.12.0. CPU only.

One core: cmd /c "start /wait /b /affinity 1 ctest --test-dir <build> --no-tests=error -R ^xmem_shim_page_runs$". Unconstrained: ctest --test-dir <build> --no-tests=error -R '^xmem_shim_page_runs$'.

Build Condition Before, 68969ed8 After, 64e57c51
Debug (dev) one core 0/10 passed 10/10
Debug (dev) unconstrained 16/20 passed 20/20
Release /O2 (release) one core not measured 10/10
Release /O2 (release) unconstrained not measured 20/20

Before the change, the one-core run with --output-on-failure shows the assertion above; the other "before" runs did not capture output, and their failures end after about 5.3 s against about 9.4 s for a pass, as that one does. With the reader-without-retry line skipped, the rest of the check passed 3/3 on one core.

A separate check of the same patch by a second reviewer on the earlier base 68969ed8 (before the rebase and the docs/3 s change; counts from that reviewer's report, not re-run here): Debug and /O2 10/10 each on one core and unconstrained; 10/10 with four busy-loop processes; 5/5 on two cores; ctest -R '^xmem_' -E 'warp|hardware' 22/22. Negative controls on modified copies:

  • control DLL with the retry handler always on: fails with no fault ... reader survived;
  • hold_window() call removed: fails in 12 s with the hold hook did not run, no hang.

Strings X2W_XMEM_TEST_HOLD_WINDOW / x2w-xmem test hold: 0 in xmem.dll (Debug, Release), 1 each in the test-controls DLL.

Device tests at e6ac2d05, Debug, ctest --no-tests=error -R '^xmem_page_runs_device_(warp|hardware)$' -j1 -V: xmem_page_runs_device_warp Passed (0.13 s); xmem_page_runs_device_hardware Passed (0.33 s) on NVIDIA GeForce RTX 3070 Ti (10de:2482), driver 32.0.16.2101 (621.01); 2/2, no skip. The log does not report the debug-layer state.

The pr preset does not run xmem_shim_page_runs, so the PR check will not exercise this change.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589743937 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1848). Originally posted by [Makto94](https://github.com/Makto94) on 2026-11-24T19:30:27Z. --- ## Problem `xmem_shim_page_runs` is flaky in its negative control `reader-without-retry`: ```text AssertionError: reader-without-retry: no fault: 'child: reader starts\nchild: reader survived\n' ``` It failed once in a serial `main` run (run 45421896173, Debug job 138545800299) and in the `--parallel 4` run discussed in #1173. The control needs a Python reader thread to read page 20 in the short window between `UnmapViewOfFile2` and the remap of the shared view inside `XMemMapPhysicalPagesScatter`. When the reader never lands there in 3 s, the child survives and the check fails. Refs #1173, #1306 ## Change - `src/xmem_mapped_pages.cpp`, test build only (`#ifdef X2W_XMEM_TEST_CONTROLS`): with `X2W_XMEM_TEST_HOLD_WINDOW=<name>`, an unmap that took a run apart sets the event `<name>.unmapped`, then waits up to 5 s for `<name>.release` before mapping again. In the deployed shim `hold_window()` is empty. - `tests/xmem_page_runs_check.py`: the `reader-without-retry` child waits for `<name>.unmapped` without the GIL and reads the unmapped page, so the fault happens every time. If the hold never comes, the child says so and the parent fails with "the hold hook did not run" instead of "survived". The deployed-shim control now runs as a `hold-ignored` child with both variables set (still 3 s of toggling) and also checks that no unmap waited in the window. - `docs/startup-runtime.md`: names the new test switch next to `X2W_XMEM_NO_FAULT_RETRY`. Not changed: the positive reader case (`reader_while_toggling` with the handler) still does not require an observed retry. ## Verification Windows 11 Pro 26300.9974, MSVC 19.44.35230 x64, Python 3.12.0. CPU only. One core: `cmd /c "start /wait /b /affinity 1 ctest --test-dir <build> --no-tests=error -R ^xmem_shim_page_runs$"`. Unconstrained: `ctest --test-dir <build> --no-tests=error -R '^xmem_shim_page_runs$'`. | Build | Condition | Before, `68969ed8` | After, `64e57c51` | |---|---|---|---| | Debug (`dev`) | one core | 0/10 passed | 10/10 | | Debug (`dev`) | unconstrained | 16/20 passed | 20/20 | | Release `/O2` (`release`) | one core | not measured | 10/10 | | Release `/O2` (`release`) | unconstrained | not measured | 20/20 | Before the change, the one-core run with `--output-on-failure` shows the assertion above; the other "before" runs did not capture output, and their failures end after about 5.3 s against about 9.4 s for a pass, as that one does. With the `reader-without-retry` line skipped, the rest of the check passed 3/3 on one core. A separate check of the same patch by a second reviewer on the earlier base `68969ed8` (before the rebase and the docs/3 s change; counts from that reviewer's report, not re-run here): Debug and `/O2` 10/10 each on one core and unconstrained; 10/10 with four busy-loop processes; 5/5 on two cores; `ctest -R '^xmem_' -E 'warp|hardware'` 22/22. Negative controls on modified copies: - control DLL with the retry handler always on: fails with `no fault ... reader survived`; - `hold_window()` call removed: fails in 12 s with `the hold hook did not run`, no hang. Strings `X2W_XMEM_TEST_HOLD_WINDOW` / `x2w-xmem test hold`: 0 in `xmem.dll` (Debug, Release), 1 each in the test-controls DLL. Device tests at `e6ac2d05`, Debug, `ctest --no-tests=error -R '^xmem_page_runs_device_(warp|hardware)$' -j1 -V`: `xmem_page_runs_device_warp` Passed (0.13 s); `xmem_page_runs_device_hardware` Passed (0.33 s) on NVIDIA GeForce RTX 3070 Ti (10de:2482), driver 32.0.16.2101 (621.01); 2/2, no skip. The log does not report the debug-layer state. The `pr` preset does not run `xmem_shim_page_runs`, so the PR check will not exercise this change.
upstream-sync closed this pull request 2026-11-24 20:01:00 +00:00
tedin7 commented 2026-11-24 20:01:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:38:25Z.

Review: COMMENTED


Review of e6ac2d05a956b987e1bd16971d3c092580332827 (runtime: hold the unmapped window for the reader-without-retry control), checked out at exactly that SHA. Verdict: no P1, no P2 found by reading. I could not run this change: it is Windows-only (named events, UnmapViewOfFile2, a second build of the shim), and this host is Linux.

What the change does

In the test build only (X2W_XMEM_TEST_CONTROLS; the #else branch is an empty function), hold_window() is called after the unmapped runs of a batch are recorded as gone and before they are mapped again. If X2W_XMEM_TEST_HOLD_WINDOW=<name> is set it signals the manual-reset event <name>.unmapped and waits up to 5 s for <name>.release. The reader thread of the control waits for .unmapped, then reads a page of the run that is unmapped at that moment, so the read always lands in the window and, without the fault handler, ends in an access violation. Before, the control relied on the reader hitting the window by chance within three seconds.

What I checked by reading

  • Race / synchronisation: the window is held deterministically between the unmap loop and the re-map, with the break state of the batch still active, which is the state the handler logic assumes. The reader only reads after .unmapped is set, so the read cannot precede the unmap; if the shim's 5 s wait ends first, the reader's read could miss the window, but the reader is released only by the read and the wait is long compared with a thread wake-up. The release event is manual-reset and stays signalled after the first read, so later unmaps do not wait again.
  • Leaks: both handles are created and closed inside hold_window() on every path, with null checks (if (unmapped) CloseHandle, same for release). Nothing is stored. The Python side creates its two events once per child and exits.
  • Name handling: GetEnvironmentVariableA returns a length at least the buffer size when the value is too long; the code refuses both zero and >= sizeof(name) - 16, so .unmapped/.release always fit in the 128-byte event name.
  • Deployed shim: hold_window is an empty function without X2W_XMEM_TEST_CONTROLS. The new child mode hold-ignored runs the same reader against the deployed shim and asserts WaitForSingleObject(unmapped, 0) != 0, i.e. that nothing signalled; expect_access_violation now fails if the child printed never held the window, so a missing hook cannot pass as an access violation. The marker child: reader reads the unmapped page is printed before the read, so a crash elsewhere before that point is not accepted.
  • Clean-room and title-neutral: only shim and test code; no title is named.

Commands run (Linux)

python3 -m py_compile tests/xmem_page_runs_check.py     -> ok
x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -Isrc src/xmem_mapped_pages.cpp   (with and without -DX2W_XMEM_TEST_CONTROLS)
    -> errors only for headers this cross toolchain lacks (__try in local_log.h, MapViewOfFile3, VirtualAlloc2); none in the new lines
git diff --check origin/main...HEAD                      -> clean
python3 -B tests/docs_links_check.py                     -> Checked 17 Markdown files and 366 links
python3 -B tests/bidi_controls_check.py                  -> no bidirectional controls

No mutations: the code under change cannot be built or run here, so I did not claim any. A useful pair for the Windows run: delete the hold_window() call (the control must then fail with never held the window) and move it before the unmap loop (the reader must then read a mapped page and the child must survive).

P3

  • The shim's lock and break state are held during the wait, up to 5 s per hold if nothing releases it; fine for a test build, but a one-line comment saying so would help.
  • The events are created with a plain name; a Local\\ prefix would keep them out of other sessions' namespace on a shared machine (the name already contains the parent's pid).

Limits

Not built, not run on Windows: please run ctest -R xmem_shim_page_runs (or the CI Windows job) to confirm; my verdict rests on reading the code and the test.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046076124 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1848#pullrequestreview-6046076124). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:38:25Z. **Review:** COMMENTED --- Review of `e6ac2d05a956b987e1bd16971d3c092580332827` (`runtime: hold the unmapped window for the reader-without-retry control`), checked out at exactly that SHA. Verdict: no P1, no P2 found by reading. **I could not run this change**: it is Windows-only (named events, `UnmapViewOfFile2`, a second build of the shim), and this host is Linux. ## What the change does In the test build only (`X2W_XMEM_TEST_CONTROLS`; the `#else` branch is an empty function), `hold_window()` is called after the unmapped runs of a batch are recorded as gone and before they are mapped again. If `X2W_XMEM_TEST_HOLD_WINDOW=<name>` is set it signals the manual-reset event `<name>.unmapped` and waits up to 5 s for `<name>.release`. The reader thread of the control waits for `.unmapped`, then reads a page of the run that is unmapped at that moment, so the read always lands in the window and, without the fault handler, ends in an access violation. Before, the control relied on the reader hitting the window by chance within three seconds. ## What I checked by reading - **Race / synchronisation:** the window is held deterministically between the unmap loop and the re-map, with the break state of the batch still active, which is the state the handler logic assumes. The reader only reads after `.unmapped` is set, so the read cannot precede the unmap; if the shim's 5 s wait ends first, the reader's read could miss the window, but the reader is released only by the read and the wait is long compared with a thread wake-up. The release event is manual-reset and stays signalled after the first read, so later unmaps do not wait again. - **Leaks:** both handles are created and closed inside `hold_window()` on every path, with null checks (`if (unmapped) CloseHandle`, same for `release`). Nothing is stored. The Python side creates its two events once per child and exits. - **Name handling:** `GetEnvironmentVariableA` returns a length at least the buffer size when the value is too long; the code refuses both zero and `>= sizeof(name) - 16`, so `.unmapped`/`.release` always fit in the 128-byte event name. - **Deployed shim:** `hold_window` is an empty function without `X2W_XMEM_TEST_CONTROLS`. The new child mode `hold-ignored` runs the same reader against the deployed shim and asserts `WaitForSingleObject(unmapped, 0) != 0`, i.e. that nothing signalled; `expect_access_violation` now fails if the child printed `never held the window`, so a missing hook cannot pass as an access violation. The marker `child: reader reads the unmapped page` is printed before the read, so a crash elsewhere before that point is not accepted. - Clean-room and title-neutral: only shim and test code; no title is named. ## Commands run (Linux) ``` python3 -m py_compile tests/xmem_page_runs_check.py -> ok x86_64-w64-mingw32-g++ -std=c++20 -fsyntax-only -Isrc src/xmem_mapped_pages.cpp (with and without -DX2W_XMEM_TEST_CONTROLS) -> errors only for headers this cross toolchain lacks (__try in local_log.h, MapViewOfFile3, VirtualAlloc2); none in the new lines git diff --check origin/main...HEAD -> clean python3 -B tests/docs_links_check.py -> Checked 17 Markdown files and 366 links python3 -B tests/bidi_controls_check.py -> no bidirectional controls ``` No mutations: the code under change cannot be built or run here, so I did not claim any. A useful pair for the Windows run: delete the `hold_window()` call (the control must then fail with `never held the window`) and move it before the unmap loop (the reader must then read a mapped page and the child must survive). ## P3 - The shim's lock and break state are held during the wait, up to 5 s per hold if nothing releases it; fine for a test build, but a one-line comment saying so would help. - The events are created with a plain name; a `Local\\` prefix would keep them out of other sessions' namespace on a shared machine (the name already contains the parent's pid). ## Limits Not built, not run on Windows: please run `ctest -R xmem_shim_page_runs` (or the CI Windows job) to confirm; my verdict rests on reading the code and the test. AI-assisted; commands listed were executed locally.
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
R0Y00/Xbox2Windows!1841
No description provided.