startup: free xcrdapi COM objects at final Release and join its queue worker #2792
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2800"
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 lambirou on 2026-11-26T10:48:57Z.
Change
The DirectStorage stand-in heap-allocates an Object, FileObject or QueueObject for every OpenFile, CreateStatusArray and CreateQueue, but release() only decrements the reference count. Files, status arrays and queues therefore leak for the life of the process. Queue entries also read the status array they complete and the file a request reads after the guest may have released them, and the queue worker keeps a raw self pointer and never exits.
This change frees every stand-in object at reference zero (the file handle, the status result buffer and the queue error event), keeps the process-wide factory singleton, and gives the queue a shutdown path: the final Release marks the worker stopping, joins it, and only then drops the entries that never ran. Entries retain the status array they complete and the source file of a request until consumed; deferred and discarded entries release both. A queue released from its own worker (a fence callback) hands the destruction to the worker as it exits.
Refs #2788.
Verification
Commits
a7efbf82,522afd71andfdfa6495, rebased on4989a6df(upstreammain). Windows x64 Release, MSVC 19.44.35228, Ninja. No GPU adapter is needed.Result:
2/2passed,startup_xcrdapi_lifetimeandstartup_xcrdapi_lifetime_naive_control.Result:
15/15passed, includingstartup_storage_worker_start(36.9 s) and bothstartup_storage_refused_reusecontrols.Evidence and follow-up
The new fixture is source-built: it loads
XCRDAPI.dll, drives the factory directly and uses only a temporary patterned file. No game data, dump or distribution asset is involved. It reads the fixture-only exportsx2w_xcrdapi_live_objectsandx2w_xcrdapi_live_workers, which count live objects by kind and running queue workers.Negative controls:
startup_xcrdapi_lifetime_naive_controlsetsX2W_TEST_XCRDAPI_LIFETIME_MUTANT=naivein the stand-in, which deletes at final Release without the worker stop or the entry references. It must fail on the retention check, and does.Assumptions and gaps:
Queue::Close()(slot 8) still only logs; it does not wait for pending requests as the PC method does. That is unchanged here.X2W_TEST_*convention.AI-assisted; the commands above were executed locally.
Checklist
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T10:52:26Z.
Review: COMMENTED
Review of head
a7efbf82(comment only, not a merge decision).Result: the new test
startup_xcrdapi_lifetimecrashes on Windows. It is not flaky.What I ran (Win2025 WARP, dev build, same result twice):
x2w_pipe.py win2025 2800 --regex 'startup_xcrdapi_lifetime|startup_storage_|startup_dstorage_destination'--regex 'startup_xcrdapi_lifetime'startup_xcrdapi_lifetime:***Exception: SegFaultboth times (0.65 s, then 0.15 s).Queue::EnqueueStatus(array "Pending", index 0),Queue::Submit,Queue::EnqueueRequest ... -> done, then twoQueue::EnqueueSignallines. The crash follows these, so it is in the pending-entry / final-Release path the test exercises (queue worker join, or the entry references on the status array and the source file).startup_xcrdapi_lifetime_naive_controlshows Passed, but it is WILL_FAIL. Until the main test runs cleanly, that pass does not prove the control fails on the retention check rather than on the same crash. Please check that the control fails for the intended reason.startup_storage_*tests in the same run passed (11 of 18 before the crash, no other failure).What looks right from reading the code:
defer_unrecordedmoves ownership to the node.shutdown_queue; hand-off when the last Release runs on the worker.Small points:
git diff --check: new blank line at EOF intests/startup_xcrdapi_lifetime.cpp:230.Not merged. Please look at the crash (a debug build of the fixture under the Windows debugger should show the frame) and push a fix.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T10:56:56Z.
Review: COMMENTED
Review of head
522afd71(comment only).The new commit only adds test code (+51 lines in
tests/startup_xcrdapi_lifetime.cpp);src/is unchanged sincea7efbf82. The crash is still there.Win2025 WARP dev,
x2w_pipe.py win2025 2800 --regex 'startup_xcrdapi_lifetime|startup_storage_|startup_dstorage_destination':startup_xcrdapi_lifetime:***Exception: SegFault(0.19 s). Third crash in a row across two heads. Last stub log lines before it:Queue::EnqueueStatus(array "Pending"),Queue::Submit,Queue::EnqueueRequest -> done,Queue::EnqueueSignal value=0x100.startup_xcrdapi_lifetime_naive_controlshows Passed, but it is WILL_FAIL, so it may be passing only because the binary crashes. It needs to fail on the retention check.startup_storage_directory_batch: Failed in this run (it passed in the earlier run ona7efbf82). I did not rerun it; please check whether it is flaky or related.Not merged. Please find the crash frame (debug build of the fixture) and fix it.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T11:02:41Z.
Review: COMMENTED
Review of head
fdfa6495(comment only).The earlier SegFault was a bug in the test, not in the stub: the test read the status array after the guest had released its only reference. This head keeps the guest reference for the post-completion checks and releases it last.
src/is unchanged sincea7efbf82.Checked:
startup_xcrdapi_lifetimepasses,startup_storage_directory_batchpasses again; receipt in the next comment).startup_xcrdapi_lifetime_naive_control(WILL_FAIL) passes in both builds, and the main test no longer crashes, so the control now fails on a real check, not on a crash.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by lambirou on 2026-11-26T10:58:32Z.
Fixed in
fdfa6495. Thank you, the crash was real and your log pointed straight at it.Root cause. The fixture released its only reference on the status array before
Submit, then readIsComplete/GetHResultafter the barrier. By then the status entry had consumed its own reference, so the array was already freed; the release build read the stale vtable by luck, the debug heap faulted. This was a test bug, not a stand-in bug: the newstartup_xcrdapi_lifetimewas the only test that used the array after dropping its reference.Fix. The guest's status reference now stays until the result has been read, then it is released and the live counter checked. The retention proof for both the status array and the file stays in the unsubmitted-entry section, which releases both while the entries are pending.
Evidence.
0xC0000005right after the twoQueue::EnqueueSignallines.startup_xcrdapi_lifetime_naive_controlstill exits 1 and names the intended check:FAIL: a file referenced by a pending entry was freed by the guest's Release.git show --checkis clean; the trailing blank line at EOF is gone.Could you rerun
x2w_pipe.py win2025 2800 --regex 'startup_xcrdapi_lifetime|startup_storage_|startup_dstorage_destination'onfdfa6495?Imported from R0Y00/Xbox2Windows. Originally posted by lambirou on 2026-11-26T11:01:32Z.
Both reviews were on
a7efbf82/522afd71; the crash fix is infdfa6495, and the head now has all three checks green.On
startup_storage_directory_batch: it does not reproduce here. I builtstartup_stub_consumerfrom the same tree and ran the Python fixture directly, five times:The test does not touch the objects this PR changes (it drives one 3,585-page RDS5 batch through the consumer), and its only edit in this branch is unrelated. I would treat the single failure as a timeout/flake until it reproduces; its CTest timeout is 30 s for that page count.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T11:02:42Z.
Win2025 WARP receipt for head
fdfa6495(tests run: startup_xcrdapi_lifetime, startup_xcrdapi_lifetime_naive_control and the startup_storage_* / startup_dstorage_destination siblings).Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
fdfa6495307cc83011e82f48a364500273b277a2:--preset dev):cmake --build --preset dev --target startup_stub_xcrdapi startup_xcrdapi_lifetime -- -j 2):1.91s(configure1.87s)ctest --preset dev -R 'startup_xcrdapi_lifetime|startup_storage_|startup_dstorage_destination' --parallel 6 --no-tests=error --output-on-failure): 18/18 passed (37.38s:startup_storage_refused_reuse0.04s,startup_storage_consumer_pair_accept_refused_control0.07s,startup_storage_consumer_pair0.06s,startup_storage_failure_counts_one_bucket_control0.05s,startup_storage_alloc_failure0.15s,startup_storage_refused_reuse_accept_refused_control0.03s,startup_storage_refusal_order0.04s,startup_storage_failure_counts0.04s,startup_dstorage_destination0.21s,startup_storage_root0.01s,startup_storage_page_end_counts0.04s,startup_storage_page_end_counts_one_bucket_control0.03s,startup_storage_root_separator_control0.03s,startup_storage_file_range0.03s,startup_xcrdapi_lifetime_naive_control0.02s,startup_xcrdapi_lifetime0.05s,startup_storage_directory_batch0.32s,startup_storage_worker_start37.21s)/O2(--preset release):cmake --build --preset release --target startup_stub_xcrdapi startup_xcrdapi_lifetime -- -j 2):37.39s(configure1.88s)ctest --preset release -R 'startup_xcrdapi_lifetime|startup_storage_|startup_dstorage_destination' --parallel 6 --no-tests=error --output-on-failure): 18/18 passed (37.32s:startup_storage_refused_reuse_accept_refused_control0.04s,startup_storage_consumer_pair0.06s,startup_storage_consumer_pair_accept_refused_control0.04s,startup_storage_page_end_counts0.04s,startup_storage_failure_counts0.03s,startup_storage_alloc_failure0.14s,startup_storage_refused_reuse0.04s,startup_storage_page_end_counts_one_bucket_control0.03s,startup_storage_failure_counts_one_bucket_control0.03s,startup_storage_refusal_order0.05s,startup_dstorage_destination0.20s,startup_storage_root0.03s,startup_storage_file_range0.02s,startup_storage_root_separator_control0.02s,startup_storage_directory_batch0.23s,startup_xcrdapi_lifetime_naive_control0.01s,startup_xcrdapi_lifetime0.04s,startup_storage_worker_start37.17s)sccache: 0/4 hits; wall85.5sReal-GPU run not available (RunPod stopped); this PR needs no GPU.
AI-assisted; commands listed were executed locally.