hle/dstorage: fix queue error reset, event handle duplication, close/release races, and pointer validation #81

Manually merged
github merged 1 commit from gh-pr/81/head into gh-pr/81/base 2026-11-26 04:08:40 +00:00
Owner

@⁠certified-retart on GitHub · 2026-11-23 01:52 UTC · original
certified-retart:fix/dstorage-queue-bugs → main · +279 −45 · 1 files · 1 commits · merged 2026-11-23 10:36 UTC

Summary

Fixes several queue lifecycle, error-reporting, and pointer-validation bugs in crates/xcl/src/hle/dstorage.rs:

  1. RetrieveErrorRecord never cleared errors or reset error_event:
    • Cleared the error count and first failure record and called ResetEvent(q.error_event) under q.errors.lock() (the same mutex held by QueueInner::fail when calling SetEvent, avoiding lost notifications).
  2. GetErrorEvent returned the internal HANDLE directly:
    • Duplicated error_event with DuplicateHandle(..., DUPLICATE_SAME_ACCESS) (matching file_get_handle), so the caller can close its returned handle via CloseHandle without invalidating the queue's internal event or causing a double-close in QueueInner::drop.
  3. Concurrent Release() could leak the worker thread and QueueInner:
    • Replaced the non-atomic refs.load() == 1 pre-check before fetch_sub with a single atomic fetch_sub(1, Ordering::AcqRel) - 1 == 0 check so exactly one thread takes worker and tears down the Arc<QueueInner> ↔ worker thread cycle.
  4. Close() vs. Enqueue*() / Submit() races and in-flight worker completion:
    • Acquired worker then pending (matching submit()'s lock order) in queue_close and checked closed under the respective locks in submit(), enqueue(), and enqueue_request(), preventing post-close enqueues or worker respawns.
    • Stopped executing queued requests/signals after closed is set in QueueInner::run (marking remaining submitted statuses E_QUEUE_CLOSED) and waited outside queue locks on a worker_active condvar in queue_close so no worker touches caller buffers after Close() returns (including for concurrent Close() callers).
    • Released worker before dropping any unsent batch on spawn/send failure so fence Release() cannot re-enter while holding the mutex.
  5. In-order refused error reporting & pointer validation:
    • Carried refused request diagnostics in Entry::Refused so errors are published in queue order (and skipped if closed before execution).
    • Validated readable() and status_vtbl() in queue_enqueue_status, and readable() on fence + vtable in queue_enqueue_signal.

Tests

  • Added error_record_resets_after_retrieval_and_event_is_duplicated
  • Added queue_release_concurrent_breaks_worker_cycle
  • Added queue_validates_status_and_fence_pointers_and_closed_rejects_work
  • Ran cargo test -p xcl --lib hle::dstorage::tests -- --test-threads=2: 8 passed; 0 failed.
**@⁠certified-retart** on GitHub · 2026-11-23 01:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/81) `certified-retart:fix/dstorage-queue-bugs` → `main` · +279 −45 · 1 files · 1 commits · merged 2026-11-23 10:36 UTC ## Summary Fixes several queue lifecycle, error-reporting, and pointer-validation bugs in `crates/xcl/src/hle/dstorage.rs`: 1. **`RetrieveErrorRecord` never cleared errors or reset `error_event`**: - Cleared the error count and first failure record and called `ResetEvent(q.error_event)` under `q.errors.lock()` (the same mutex held by `QueueInner::fail` when calling `SetEvent`, avoiding lost notifications). 2. **`GetErrorEvent` returned the internal `HANDLE` directly**: - Duplicated `error_event` with `DuplicateHandle(..., DUPLICATE_SAME_ACCESS)` (matching `file_get_handle`), so the caller can close its returned handle via `CloseHandle` without invalidating the queue's internal event or causing a double-close in `QueueInner::drop`. 3. **Concurrent `Release()` could leak the worker thread and `QueueInner`**: - Replaced the non-atomic `refs.load() == 1` pre-check before `fetch_sub` with a single atomic `fetch_sub(1, Ordering::AcqRel) - 1 == 0` check so exactly one thread takes `worker` and tears down the `Arc<QueueInner>` ↔ worker thread cycle. 4. **`Close()` vs. `Enqueue*()` / `Submit()` races and in-flight worker completion**: - Acquired `worker` then `pending` (matching `submit()`'s lock order) in `queue_close` and checked `closed` under the respective locks in `submit()`, `enqueue()`, and `enqueue_request()`, preventing post-close enqueues or worker respawns. - Stopped executing queued requests/signals after `closed` is set in `QueueInner::run` (marking remaining submitted statuses `E_QUEUE_CLOSED`) and waited outside queue locks on a `worker_active` condvar in `queue_close` so no worker touches caller buffers after `Close()` returns (including for concurrent `Close()` callers). - Released `worker` before dropping any unsent batch on spawn/send failure so fence `Release()` cannot re-enter while holding the mutex. 5. **In-order refused error reporting & pointer validation**: - Carried refused request diagnostics in `Entry::Refused` so errors are published in queue order (and skipped if closed before execution). - Validated `readable()` and `status_vtbl()` in `queue_enqueue_status`, and `readable()` on fence + vtable in `queue_enqueue_signal`. ### Tests - Added `error_record_resets_after_retrieval_and_event_is_duplicated` - Added `queue_release_concurrent_breaks_worker_cycle` - Added `queue_validates_status_and_fence_pointers_and_closed_rejects_work` - Ran `cargo test -p xcl --lib hle::dstorage::tests -- --test-threads=2`: **8 passed; 0 failed**.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:08:39 +00:00
github manually merged commit a4b2e9226c into gh-pr/81/base 2026-11-26 04:08:40 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:36 UTC · original

Merged. Fortnite reads all of its data through DirectStorage – with this it still loads to its splash screen, no DirectStorage errors in the log. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 10:36 UTC · [original](https://github.com/xcl-dev/xcl/pull/81#issuecomment-7098217361) Merged. Fortnite reads all of its data through DirectStorage – with this it still loads to its splash screen, no DirectStorage errors in the log. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.