runtime: make RequestPublisher exit drain lock-free when writer holds lock #3854
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3854"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-12-02T13:22:39Z.
Fixes #3822.
Cause
At process exit,
ExitProcessterminates theRequestPublisherbackground writer thread beforeMeshRequestDrain::~MeshRequestDrain()runs duringDLL_PROCESS_DETACH. If the writer thread was terminated while holdinglock_(on entry torun(), while waking fromSleepConditionVariableSRW, or while popping a finished request),drain()sawTryAcquireSRWLockExclusive(&lock_) == FALSE, returned0, and silently dropped every queued mesh request.Change
src/startup_stubs/request_publisher.h:std::deque<Request>with an intrusive singly-linked list of heap-allocated immutableNodes (sentinel_, atomichead_, atomicNode::next, atomicNode::written) and madethread_atomic.enqueue()constructs the fullNodebefore publishing it with a single release store (tail_->next.store(raw, memory_order_release)), preserving OOM exception safety.write_front()marksnode->writtenafter all parts succeed, advanceshead_atomically, and deletes the previous dummy head node only afterhead_has moved past it.drain()checksthread_atomically without takinglock_. Once the writer thread has ended (or never started), it takes the locked path whenTryAcquireSRWLockExclusive(&lock_)succeeds; iflock_is still held by a terminated thread, it walks the immutablehead_ -> nextchain lock-free (without touchinglock_,stats_,seen_, or freeing nodes) and writes every unwritten request via the exit writer.tests/startup_request_publisher_check.cpp:drain_when_lock_held()covering:lock_, plus idempotence on repeateddrain()and clean node retirement after unlock without rewriting parts.writtenfollowed by an unwritten node (simulating termination after writing all parts but beforehead_advanced): lockeddrain()skips the completed front node and writes only the second request.Verification (
1f465743)10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.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_controlWILL_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).0on!TryAcquireSRWLockExclusive(&lock_)inRequestPublisher::drain()failsstartup_request_publisheron all 5 new locked-drain assertions.tests/standin_exports_check.py(470 cases, 6/6 mutations killed PASS),tests/docs_links_check.py,tests/bidi_controls_check.pyall passed.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 withstart_thread=false, enqueue one part, gate the first drain inside its write callback while it holds the lock, then call drain a second time. On5770f14ethe second drain returned 0 and the callback ran once. On1f465743it 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.
Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-12-02T15:17:00Z.
Thanks for catching that! Addressed all three points in
d1320c43:drain()from process-terminationdrain_at_process_exit()(src/startup_stubs/request_publisher.h,src/startup_stubs/d3d12_draw.inl):drain()now strictly requiresTryAcquireSRWLockExclusive(&lock_)when no background writer thread is running and returns0if another live caller holdslock_(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 whenlock_is abandoned, andMeshRequestDrain::~MeshRequestDrain()calls it only whenx2w_startup::d3d12_process_terminatingis true (DLL_PROCESS_DETACHwithlpReserved != nullptr, afterExitProcesshas already terminated all other threads). ExplicitFreeLibraryunload calls ordinarydrain().RequestPublisher::flush()):try_acquire_lock_until(deadline)soflush(timeout_ms)(anddrain(timeout_ms)waiting on a live writer thread) returns withintimeout_mseven iflock_is held.tests/startup_request_publisher_check.cpp:start_thread=false, one part queued, firstdrain()gated inside its write callback while holdinglock_; second concurrentdrain()returns0and the callback runs exactly once.lock_is held;flush(50)anddrain(50)both return within timeout (false/0,< 500 ms).drain_at_process_exit()(both two unwritten requests + idempotent repeat + post-unlock retire, and already-written front node + unwritten second node).Verification (
d1320c43)10.0.26300.9974), MSVC19.44.35230, Windows SDK / DXC10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver32.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.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T15:39:24Z.
Review: APPROVED
Re-reviewed
654cd45eafter 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 Debugctest --preset dev --no-tests=error -j 1 -R '^startup_request_publisher$' --output-on-failurepassed 1/1.