startup: report DirectStorage queue errors and signal the error event #1866
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1873"
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 Jess-Doit on 2026-11-24T19:54:31Z.
Refs #1843
Change
Implement queue slot 10
RetrieveErrorRecordand signal the queue's slot 9error event when the first read fails. The 656-byte x64 record counts failed
requests since retrieval and keeps the first request's HRESULT, options,
destination, source, sizes, cancellation tag and name. A file failure also
keeps the opened path and file offset. The existing failure sites still update
the heartbeat counters, including when a request could not start.
Layout: the record follows the published Xbox
DSTORAGE_ERROR_RECORD(dstorage_xs.h):FailureCountat 0x000.FirstFailure.HResultat 0x008 andFirstFailure.CommandTypeat 0x00c.A failed read sets
CommandTypetoDSTORAGE_COMMAND_TYPE_REQUEST(0). An empty record has a zero count andCommandTypeDSTORAGE_COMMAND_TYPE_NONE(-1), not all-zero bytes, which would read asREQUEST. That covers a new queue, the state after a retrieve, and a success-only queue. Only failed read requests are recorded; the status and signal arms are not synthesized.The request offsets match Microsoft's MIT-licensed Xbox-GDK-Samples Win32 header, which leaves out
CommandTypeand has padding at 0x00c. The stand-in's header pins every offset withstatic_assert. The consumer reads the record at the published offsets through its own declaration (xbox_error_recordintests/startup_stub_consumer.cpp), not the stand-in's struct.The header and DirectStorage overview
describe a fresh notification after retrieval. The stand-in implements this
with a manual-reset event and clears it under the same lock as the record.
Each caller owns the handle returned by
GetErrorEvent. The existing emergencypath that cannot allocate even a deferred entry can still fail at enqueue.
Differences from the sample's implementation: in
DirectStorageWin32WrapperQueue.cpp, the queue creates an auto-reset event once, returns that same handle and closes it when the queue is destroyed. This stand-in differs in two places:GetErrorEventcall returns a duplicate handle. That stays safe whether a title closes the handle or not; the cost of never closing one is a leaked handle, not a broken queue.RetrieveErrorRecord. That matches the header's contract (retrieve once the event is signaled, then not again until the next signal), and a second waiter can't consume the notification.The Xbox
GetErrorEventreference makes the returned handle the caller's.Verification
Tested
bd52841495856630fa22c0d067bb85824cf3b838from82749f0f84d9f8b571566fdf564d530e71f499aaon Windows 10 22H2 build19045.6456 with MSVC 19.44.35230. A separate MSVC compile compared all
request and error-record field offsets and sizes with the pinned Microsoft header.
devreleasestartup_stubschecks two failed file reads in enqueue order, a distinctsuccess-only queue, an empty second retrieve and a memory failure that
re-arms the event.
startup_storage_worker_startchecks a signaled eventfirst acquired after four requests fail with
E_OUTOFMEMORYduring Submit.The optional desktop DirectStorage NuGet tests require
DSTORAGE_SDK_ROOTand were outside this configuration's selection.
All three Debug mutation builds failed
startup_stubsat the intended check.SetEventfailed request did not leave the error event signaledfailure_countsecond retrieve did not clear the failure count and first recordResetEventretrieve did not reset the eventThe restored DLL passed
startup_stubsagain.To reproduce each control, make its single change in
xcrdapi.cpp, thenbuild and run the consumer check. Restore the source and repeat to confirm
the baseline.
At the head (
5f2edd46: the CommandType change, a merge of main at3041aab1, and test additions from review):startup_storage_worker_starttakes the error-event handle before the failure as well as after it. Removing the first-failureSetEventnow fails both that test andstartup_stubs.startup_storage_page_end_countstests. The command wasctest --preset <preset> --no-tests=error -R '^(startup_stubs|startup_storage_.*|startup_dstorage.*|docs_links)$'.NONErecord, failsstartup_stubswithsecond retrieve did not clear the failure count and first record.AI-assisted (Codex, driven with Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:59:57Z.
Review: COMMENTED
Peer review of head
abd992d20585de279df0f910704fdeaf8861db9f(PRODUCTION_REVIEW_ONLY, technical comment only).No P1 and no P2 found by reading the change. The C++ was not built or run locally, so the Windows test results are not verified here.
Merge state: the PR is
CONFLICTINGwith currentmain.git merge-treereports a content conflict insrc/startup_stubs/xcrdapi.cpp; the docs and test files merge cleanly. The branch needs a rebase before it can be merged. The review below is against the PR head.Read in full:
dstorage_error_record.h, thexcrdapi.cppchanges, the two tests, and both docs sections.What I checked, with no defect found:
static_asserts are consistent with each other:StorageRequestis 0x48; the record is 0x290 (4-byte count, 4 bytes of padding, 0x288 failure block) with the offsets listed. I could not compare them with the external sample header, whose revision the comment cites, so they are only checked for internal consistency.GetErrorEvent) and 10 (RetrieveErrorRecord) follow the existing slots 3 to 8, and the second takes the record pointer and returns void.count_failuretakes the record lock, sets the first-failure fields only when the count was 0, and increments with aUINT32_MAXcap. Retrieve copies, then zeroes the whole record, so the next failure becomes the new first.!outreturns early; the file object is read only afterreadable()and akind == filecheck; the request name is bounded byprobe_readableand leaves the zeroed buffer untouched when unreadable. The filename copy is a same-sizeMAX_PATHarray copy.GetErrorEventcall returns a duplicated handle, so the caller can close it. The event is reset under the same lock as the record, so a concurrent failure cannot lose its notification.release()closes the stored handle when the count reaches 0 and a latercount_failurechecks for null under the lock.Unrecordednow carries a copy of the 0x48 bytes, copied only for requests). Status and signal entries never reach the record code.selfis always a queue object where the cast is made.memory_source_option = 1ull << 22equals the old0x400000.E_OUTOFMEMORY.Notes (not blocking):
resultisE_FAILfor every read failure andE_OUTOFMEMORYfor an entry that could not be stored; it does not carry the cause that the heartbeat counters already know (range, source, and so on). A title that reads theHRESULTwill see the generic code. Passing the per-cause value would be a small extension.count_failureis stillnoexcept, but it now takes a mutex throughstd::lock_guard; a failure to lock would terminate instead of reporting. This is theoretical, but it is a change from the previous lock-free body.GetErrorEventas caller-owned. The change cites it, and the duplicate-handle choice is safe for a caller that closes it and cannot hurt one that does not.Commands run (Linux):
git diff --check <merge-base>...HEAD: cleantests/docs_links_check.py: passtests/bidi_controls_check.py: passgit merge-treeagainst currentmain: one conflict,src/startup_stubs/xcrdapi.cppstartup_stubsandstartup_storage_worker_startwere not run locally.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T20:05:19Z.
Review: CHANGES_REQUESTED
The documented Xbox error-record layout has a DWORD CommandType immediately after HResult. In the current 656-byte layout that word is padding at +0x0c, so a consumer using the documented Xbox ABI cannot reliably recognize the populated request arm. Please pin the Xbox discriminator and request layout from its published definition, including the empty/reset state, and have the consumer read them by an independent declaration or fixed offsets. The narrow first slice can remain failed read requests; it does not need to synthesize failures for other arms. The caller-owned duplicate handle is consistent with the public GetErrorEvent documentation. Please also resolve current main conflicts, keeping the new page-end counters from #1864 and the phase ordering of #1847.
Microsoft Xbox error record · Xbox event handle ownership.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:19:48Z.
Review: COMMENTED
Review of head
cf00afe3781ab56915c655c10fa99966192d690a(bd528414, two merges of main, thencf00afe3"record CommandType";git merge-tree --write-tree origin/main HEADmerges cleanly).Verdict: no P1, no P2. Three P3 notes. The stub and the tests that exercise it are Windows-only (MSVC
__try/__exceptinlocal_log.h), so I could not run them here.Layout and contract, checked against Microsoft's public documentation
DSTORAGE_ERROR_RECORD(gdk-2604):FailureCount, thenFirstFailurewithHResult,CommandType, and a union whoseRequestarm isOptions, Destination, DestinationSize, Filename[MAX_PATH], FileOffset, Source, SourceSize, CancellationTag, Name[64]. With 8-byte alignment ofFirstFailurethat gives 0x008HResult, 0x00cCommandType, 0x010Options, 0x018Destination, 0x020DestinationSize, 0x024Filename, 0x230FileOffset, 0x238Source, 0x240SourceSize, 0x248CancellationTag, 0x250Name, 0x290 in all: the same numbers asdstorage_error_record.hand as the independent constants in the consumer test. The Win32 sample header (revision1f44bdab) has the same struct withoutCommandType, so the offsets after it are unchanged, as the header comment says.DSTORAGE_COMMAND_TYPE: NONE = -1, REQUEST = 0, STATUS = 1, SIGNAL = 2, as in the header. The page says that with NONE "the remaining data is undefined", which is why an empty record is built with NONE rather than all zeros (zeros would read as REQUEST).GetErrorEventpage: "When an error occurs for a read request in this queue, the event will be signalled", and "CallCloseHandleafter the title no longer needs the event". The PR gives each call its ownDuplicateHandleof the queue's event, which fits both. The overview page says the title waits on the event and then callsRetrieveErrorRecordfor the first error since the last call; the PR resets the event in the same call that clears the record.Reading of the code
count_failure,queue_error_event,queue_retrieve_error_recordand the event close inrelease()all take the queue's ownerror_mutex.SetEvent(on the first failure) andResetEvent(on retrieve) both happen under it, so a failure that lands while a retrieve is running either goes into the record that is copied out or starts a new one with the event set again: no notification is lost. A queue whose event is created after a failure starts signaled (failure_count != 0). The mutex is separate fromQueueWork, so a queue with no worker still reports (theworker_starttest covers that).make(queue), which allocates aQueueObject; the onlystatic_cast<QueueObject*>sites are in the queue vtable functions,release()forkind == queue, andcount_failure, which is reached only with the queue pointer (or null, which returns before the cast). Files areFileObjectthroughmake(file).readable(source, sizeof(FileObject))and akind == filecheck; the request name is read withprobe_readableup to 63 bytes and stops at NUL; a memory-source request recordssourcewithout dereferencing it.Unrecordednow carries the 0x48 request bytes (zero-initialised by{}), sofail_unstartedrecords them withE_OUTOFMEMORY, the code the status entry gets.failure_countsaturates atUINT32_MAX; only the first failure fills the record.Commands run locally on Linux
x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrconsrc/startup_stubs/dstorage_error_record.h(all thestatic_asserts on sizes and offsets hold for the x64 Windows ABI) and ontests/startup_stub_consumer.cpp: no errors.python3 -B tests/docs_links_check.py(18 files, 376 links),python3 -B tests/bidi_controls_check.py(1331 files): pass.git diff --check origin/main...HEAD: clean.startup_stub_consumer dstorage-error-record,startup_stubsandstartup_storage_worker_startneed the MSVC build and a Windows run; there is no working Wine here. No mutations were possible for the same reason.Findings
CommandTypeSTATUS and SIGNAL are never produced. That matches the documented event ("an error occurs for a read request") and the PR says so in the docs; it only means those two arms of the union stay unused.count_failureisnoexceptand now takes astd::mutex; a failed lock would terminate. Practically unreachable, butfail_entryalso calls it from the exception path of the worker.failure_count == 2; nothing exercises the saturation atUINT32_MAX, the 63-byte name cut, or aFilenamelonger thanMAX_PATH. One case with a 70-character name would pin the cut.Not run: the Windows build,
startup_stubs,startup_storage_worker_start, any mutation of the stub.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T20:37:36Z.
Review: APPROVED
Re-reviewed
5f2edd46. The record now writes the Xbox request command discriminator independently of the HRESULT and resets to NONE. The independent byte-offset consumer checks the record, its trailing sentinel and repeat event notifications. Windows Release passed the exact caller and worker-start test. The adjacent page-end and failure-cause counters passed on the preceding revision; they are unchanged by the test-only increment.Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T20:13:14Z.
Build and tests on this machine
At the PR head, Windows 11 build 26300.9974, MSVC 19.44:
A note on getting there, because I first saw two failures that were mine, not the patch's: the full build stopped with
because this shell had no
ZIG_GLOBAL_CACHE_DIR; and with the build aborted early,startup_stubsran against stalestub DLLs and failed. Setting the cache variables and rebuilding turned both green with no change to the PR. Anyone
reviewing this on Windows should confirm the build completed before reading a
startup_stubsfailure.The record half is pinned; the event half is not
The new test acquires the event after the failing submit:
and
queue_error_eventcreates the event with the failure count as its initial state:That means the assertion holds whether or not
SetEventis ever called for this failure. I confirmed it by mutation:replacing the only
if (object.error_event) SetEvent(object.error_event);withif (false) SetEvent(...)and rebuildingstartup_storage_worker_startleaves the test green (rebuilt=True, thenPassed).So the "signal the queue's slot 9 error event when the first read fails" half of the description is currently unobservable in
the fixture. A case that decides it: call
queue_error_eventbefore submitting the failing request, assert it is notsignalled, then submit and assert it becomes signalled. That distinguishes a missed
SetEventfrom "created already set".The
failure_count, first-failure fields and the reset-after-retrieval behaviour are all pinned — those are what theassertions at line 319 actually compare.
Smaller points
easy to point at rather than surprising.
record.first_failure.destination != destination.data()and the source-symbol comparisons are the right shape: they checkthe recorded pointers are the caller's, not a copy.
Verdict
Reviewed. The record path is verified and the tests pass; the event-signalling claim is not distinguished by the current
fixture, and one extra case would close it.
Notes
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T20:16:50Z.
Thanks, done in
cf00afe3, plus a main merge in4117f0c0:FirstFailure.CommandTypeis now at +0x0c, as in the published XboxDSTORAGE_ERROR_RECORD, with the request arm from +0x10 and 0x290 bytes in all. A failed read recordsDSTORAGE_COMMAND_TYPE_REQUEST(0).DSTORAGE_COMMAND_TYPE_NONE(-1). That covers a new queue, the state after a retrieve, and a success-only queue. The all-zero bytes the reset used to leave would have read asREQUEST.xbox_error_record), not the stand-in's struct. It checks REQUEST for both failures and NONE for every empty record.startup_stubsat the empty-record check.count_page_requestcalls at all three failure sites, next to the error-record calls, and #1847's status-array initial state.Debug and Release now pass 16/16, including
startup_storage_page_end_counts.@tedin7, thanks for the review. The per-cause HRESULT is a fair extension; I've left it out of this slice to keep it to the layout fix.
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T20:24:27Z.
Thanks @ChazY11 and @tedin7. Both are in
5f2edd46.SetEvent, because its handle is created after the failure and starts signaled. That test now also takes a handle before the failure: it must be unsignaled then, signaled after the failing Submit, and reset byRetrieveErrorRecord. With your mutation (if (false) SetEvent(...)):startup_storage_worker_startfails withthe failure did not signal an event taken before it;startup_stubsfails withfailed request did not leave the error event signaled. That consumer test already took its handle before the failure.Debug and Release pass 16/16 on Windows 10 22H2 with the full stand-in targets rebuilt.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T20:54:25Z.
Confirmed the gap I raised in my review above is closed by the
earlyhandle, and recording one measurement trap onthe way, because it cost me two wrong readings.
The fixture does not load
XCRDAPI.dll:tests/startup_storage_worker_start.cppline 94 isso the implementation is compiled into the test executable. My first two attempts rebuilt
startup_stub_xcrdapi(the DLL) andreported "not caught" — with a fresh DLL timestamp and a green run, which looks exactly like a surviving mutant. Rebuilding
startup_storage_worker_startinstead gives:So the case at lines 301–326 does what it says: a handle taken before the failure can only become signaled through the
failure's
SetEvent, and removing that call now fails the run. That is precisely the distinction my review asked for — theprevious fixture acquired the event after the failure, when
CreateEventW(..., failure_count != 0, ...)already made itsignaled.
Anyone re-running this: build
startup_storage_worker_start, not the DLL.