runtime: make RequestPublisher exit drain lock-free when writer holds lock #3854

Merged
R0D4X merged 3 commits from upstream-pr/3854 into main 2026-12-02 15:47:28 +00:00
R0D4X commented 2026-12-02 13:22:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-12-02T13:22:39Z.


Fixes #3822.

Cause

At process exit, ExitProcess terminates the RequestPublisher background writer thread before MeshRequestDrain::~MeshRequestDrain() runs during DLL_PROCESS_DETACH. If the writer thread was terminated while holding lock_ (on entry to run(), while waking from SleepConditionVariableSRW, or while popping a finished request), drain() saw TryAcquireSRWLockExclusive(&lock_) == FALSE, returned 0, and silently dropped every queued mesh request.

Change

  • src/startup_stubs/request_publisher.h:
    • Replaced std::deque<Request> with an intrusive singly-linked list of heap-allocated immutable Nodes (sentinel_, atomic head_, atomic Node::next, atomic Node::written) and made thread_ atomic.
    • enqueue() constructs the full Node before publishing it with a single release store (tail_->next.store(raw, memory_order_release)), preserving OOM exception safety.
    • write_front() marks node->written after all parts succeed, advances head_ atomically, and deletes the previous dummy head node only after head_ has moved past it.
    • drain() checks thread_ atomically without taking lock_. Once the writer thread has ended (or never started), it takes the locked path when TryAcquireSRWLockExclusive(&lock_) succeeds; if lock_ is still held by a terminated thread, it walks the immutable head_ -> next chain lock-free (without touching lock_, stats_, seen_, or freeing nodes) and writes every unwritten request via the exit writer.
  • tests/startup_request_publisher_check.cpp:
    • Added drain_when_lock_held() covering:
      1. Two queued multi-part requests drained in order while another thread holds lock_, plus idempotence on repeated drain() and clean node retirement after unlock without rewriting parts.
      2. A front node already marked written followed by an unwritten node (simulating termination after writing all parts but before head_ advanced): locked drain() skips the completed front node and writes only the second request.

Verification (1f465743)

  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818).
  • ctest --preset dev --no-tests=error -R '^startup_request_publisher' -V: 2/2 passed (startup_request_publisher + startup_request_publisher_inline_control WILL_FAIL).
  • ctest --preset release --no-tests=error -R '^startup_request_publisher' -V: 2/2 passed.
  • ctest --preset dev --no-tests=error -R '^startup_indirect_mesh_cache_heap_wide_(warp|hardware)$' -V: 2/2 passed (both WARP and hardware).
  • Fail-first mutation: Returning 0 on !TryAcquireSRWLockExclusive(&lock_) in RequestPublisher::drain() fails startup_request_publisher on all 5 new locked-drain assertions.
  • Pure-Python checks: tests/standin_exports_check.py (470 cases, 6/6 mutations killed PASS), tests/docs_links_check.py, tests/bidi_controls_check.py all passed.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5607286883 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3854). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-12-02T13:22:39Z. --- Fixes #3822. ### Cause At process exit, `ExitProcess` terminates the `RequestPublisher` background writer thread before `MeshRequestDrain::~MeshRequestDrain()` runs during `DLL_PROCESS_DETACH`. If the writer thread was terminated while holding `lock_` (on entry to `run()`, while waking from `SleepConditionVariableSRW`, or while popping a finished request), `drain()` saw `TryAcquireSRWLockExclusive(&lock_) == FALSE`, returned `0`, and silently dropped every queued mesh request. ### Change - **`src/startup_stubs/request_publisher.h`:** - Replaced `std::deque<Request>` with an intrusive singly-linked list of heap-allocated immutable `Node`s (`sentinel_`, atomic `head_`, atomic `Node::next`, atomic `Node::written`) and made `thread_` atomic. - `enqueue()` constructs the full `Node` before publishing it with a single release store (`tail_->next.store(raw, memory_order_release)`), preserving OOM exception safety. - `write_front()` marks `node->written` after all parts succeed, advances `head_` atomically, and deletes the previous dummy head node only after `head_` has moved past it. - `drain()` checks `thread_` atomically without taking `lock_`. Once the writer thread has ended (or never started), it takes the locked path when `TryAcquireSRWLockExclusive(&lock_)` succeeds; if `lock_` is still held by a terminated thread, it walks the immutable `head_ -> next` chain lock-free (without touching `lock_`, `stats_`, `seen_`, or freeing nodes) and writes every unwritten request via the exit writer. - **`tests/startup_request_publisher_check.cpp`:** - Added `drain_when_lock_held()` covering: 1. Two queued multi-part requests drained in order while another thread holds `lock_`, plus idempotence on repeated `drain()` and clean node retirement after unlock without rewriting parts. 2. A front node already marked `written` followed by an unwritten node (simulating termination after writing all parts but before `head_` advanced): locked `drain()` skips the completed front node and writes only the second request. ### Verification (`1f465743`) - **Environment:** Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK / DXC `10.0.26100.0`, NVIDIA GeForce RTX 5080 (`10de:2c02`, driver `32.0.16.2140`), Microsoft Basic Render Driver (`10.0.26100.9818`). - `ctest --preset dev --no-tests=error -R '^startup_request_publisher' -V`: **2/2 passed** (`startup_request_publisher` + `startup_request_publisher_inline_control` `WILL_FAIL`). - `ctest --preset release --no-tests=error -R '^startup_request_publisher' -V`: **2/2 passed**. - `ctest --preset dev --no-tests=error -R '^startup_indirect_mesh_cache_heap_wide_(warp|hardware)$' -V`: **2/2 passed** (both WARP and hardware). - **Fail-first mutation:** Returning `0` on `!TryAcquireSRWLockExclusive(&lock_)` in `RequestPublisher::drain()` fails `startup_request_publisher` on all 5 new locked-drain assertions. - **Pure-Python checks:** `tests/standin_exports_check.py` (`470 cases, 6/6 mutations killed PASS`), `tests/docs_links_check.py`, `tests/bidi_controls_check.py` all passed.
R0Y00 commented 2026-12-02 14:05:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T14:05:55Z.

Review: CHANGES_REQUESTED


The unlocked traversal can also run when there is no background handle and a live caller owns lock_. I reproduced this with MSVC on the exact head: create the publisher with start_thread=false, enqueue one part, gate the first drain inside its write callback while it holds the lock, then call drain a second time. On 5770f14e the second drain returned 0 and the callback ran once. On 1f465743 it returned 1 and the callback ran twice. The unlocked reader can also retain a pointer to a node that a live locked consumer advances past and deletes. The new fixture currently holds the lock in a live helper and therefore does not distinguish a live owner from process termination.

Please separate the ordinary drain from the process-termination path and use the unlocked walk only where all other producers and consumers are known to have stopped. Keep the forced-abandoned-lock control for that path and add the gated live-owner control. Please also keep any wait for a live writer bounded while it holds the publisher lock.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6152366283 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3854#pullrequestreview-6152366283). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T14:05:55Z. **Review:** CHANGES_REQUESTED --- The unlocked traversal can also run when there is no background handle and a live caller owns `lock_`. I reproduced this with MSVC on the exact head: create the publisher with `start_thread=false`, enqueue one part, gate the first drain inside its write callback while it holds the lock, then call drain a second time. On `5770f14e` the second drain returned 0 and the callback ran once. On `1f465743` it returned 1 and the callback ran twice. The unlocked reader can also retain a pointer to a node that a live locked consumer advances past and deletes. The new fixture currently holds the lock in a live helper and therefore does not distinguish a live owner from process termination. Please separate the ordinary drain from the process-termination path and use the unlocked walk only where all other producers and consumers are known to have stopped. Keep the forced-abandoned-lock control for that path and add the gated live-owner control. Please also keep any wait for a live writer bounded while it holds the publisher lock.
R0D4X commented 2026-12-02 15:17:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-12-02T15:17:00Z.


Thanks for catching that! Addressed all three points in d1320c43:

  1. Separated ordinary drain() from process-termination drain_at_process_exit() (src/startup_stubs/request_publisher.h, src/startup_stubs/d3d12_draw.inl):
    • drain() now strictly requires TryAcquireSRWLockExclusive(&lock_) when no background writer thread is running and returns 0 if another live caller holds lock_ (never traverses unlocked, so no duplicate callback or UAF against a live locked consumer).
    • drain_at_process_exit() is the only path that performs the unlocked atomic traversal when lock_ is abandoned, and MeshRequestDrain::~MeshRequestDrain() calls it only when x2w_startup::d3d12_process_terminating is true (DLL_PROCESS_DETACH with lpReserved != nullptr, after ExitProcess has already terminated all other threads). Explicit FreeLibrary unload calls ordinary drain().
  2. Bounded lock acquisition for a live writer (RequestPublisher::flush()):
    • Added try_acquire_lock_until(deadline) so flush(timeout_ms) (and drain(timeout_ms) waiting on a live writer thread) returns within timeout_ms even if lock_ is held.
  3. New controls in tests/startup_request_publisher_check.cpp:
    • Gated live-owner control: start_thread=false, one part queued, first drain() gated inside its write callback while holding lock_; second concurrent drain() returns 0 and the callback runs exactly once.
    • Bounded-wait control: live writer thread active while lock_ is held; flush(50) and drain(50) both return within timeout (false / 0, < 500 ms).
    • Forced-abandoned-lock controls: kept for drain_at_process_exit() (both two unwritten requests + idempotent repeat + post-unlock retire, and already-written front node + unwritten second node).

Verification (d1320c43)

  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Microsoft Basic Render Driver (10.0.26100.9818).
  • ctest --preset dev --no-tests=error -R '^(startup_request_publisher|startup_indirect_mesh_cache_heap_wide_(warp|hardware)$)' -V: 4/4 passed.
  • ctest --preset release --no-tests=error -R '^startup_request_publisher' -V: 2/2 passed.
  • tests/standin_exports_check.py: 470 cases, 6/6 mutations killed PASS.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7350060012 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3854#issuecomment-7350060012). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-12-02T15:17:00Z. --- Thanks for catching that! Addressed all three points in `d1320c43`: 1. **Separated ordinary `drain()` from process-termination `drain_at_process_exit()` (`src/startup_stubs/request_publisher.h`, `src/startup_stubs/d3d12_draw.inl`):** - `drain()` now strictly requires `TryAcquireSRWLockExclusive(&lock_)` when no background writer thread is running and returns `0` if another live caller holds `lock_` (never traverses unlocked, so no duplicate callback or UAF against a live locked consumer). - `drain_at_process_exit()` is the only path that performs the unlocked atomic traversal when `lock_` is abandoned, and `MeshRequestDrain::~MeshRequestDrain()` calls it only when `x2w_startup::d3d12_process_terminating` is true (`DLL_PROCESS_DETACH` with `lpReserved != nullptr`, after `ExitProcess` has already terminated all other threads). Explicit `FreeLibrary` unload calls ordinary `drain()`. 2. **Bounded lock acquisition for a live writer (`RequestPublisher::flush()`):** - Added `try_acquire_lock_until(deadline)` so `flush(timeout_ms)` (and `drain(timeout_ms)` waiting on a live writer thread) returns within `timeout_ms` even if `lock_` is held. 3. **New controls in `tests/startup_request_publisher_check.cpp`:** - **Gated live-owner control:** `start_thread=false`, one part queued, first `drain()` gated inside its write callback while holding `lock_`; second concurrent `drain()` returns `0` and the callback runs exactly once. - **Bounded-wait control:** live writer thread active while `lock_` is held; `flush(50)` and `drain(50)` both return within timeout (`false` / `0`, `< 500 ms`). - **Forced-abandoned-lock controls:** kept for `drain_at_process_exit()` (both two unwritten requests + idempotent repeat + post-unlock retire, and already-written front node + unwritten second node). ### Verification (`d1320c43`) - **Environment:** Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK / DXC `10.0.26100.0`, NVIDIA GeForce RTX 5080 (`10de:2c02`, driver `32.0.16.2140`), Microsoft Basic Render Driver (`10.0.26100.9818`). - `ctest --preset dev --no-tests=error -R '^(startup_request_publisher|startup_indirect_mesh_cache_heap_wide_(warp|hardware)$)' -V`: **4/4 passed**. - `ctest --preset release --no-tests=error -R '^startup_request_publisher' -V`: **2/2 passed**. - `tests/standin_exports_check.py`: **470 cases, 6/6 mutations killed PASS**.
R0Y00 commented 2026-12-02 15:39:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T15:39:24Z.

Review: APPROVED


Re-reviewed 654cd45e after the main update. The scenario from my earlier review is covered: a live ordinary drain holds the lock while a second ordinary drain returns without traversing or writing the same node. Only the process-termination branch can traverse without the lock. The fixture also exercises the held-lock timeout and ordered recovery (including the already-written head). Independent MSVC Debug ctest --preset dev --no-tests=error -j 1 -R '^startup_request_publisher$' --output-on-failure passed 1/1.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6154066623 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3854#pullrequestreview-6154066623). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T15:39:24Z. **Review:** APPROVED --- Re-reviewed 654cd45e after the main update. The scenario from my earlier review is covered: a live ordinary drain holds the lock while a second ordinary drain returns without traversing or writing the same node. Only the process-termination branch can traverse without the lock. The fixture also exercises the held-lock timeout and ordered recovery (including the already-written head). Independent MSVC Debug `ctest --preset dev --no-tests=error -j 1 -R '^startup_request_publisher$' --output-on-failure` passed 1/1.
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!3854
No description provided.