Repository navigation
fix(libkernel): post guest raises to the delivery drain mailbox on Windows - #12005
Conversation
|
@akkadbakkad89 TryDeliver calls TimedWait::QueueForDeliveryDrain while the target is suspended, and it takes drainBoxesLock and allocates. DrainApcs takes the same lock in the spinner loop this PR targets, so SuspendThread can stop the target holding it and block the raiser forever. Could the post move after ResumeThread (the reference is already counted) or use a lock-free slot? Also ReleaseQueued sets closed for every owner, including ArmReturnBreak/DisarmReturnBreak, so a re-arm closes the delivery before the mailbox entry runs; closed should mean the raise settled. Please add a test for the exit/recycled-thread-id case and for a raise that survives a re-arm. |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Both points verified against the source before changing — thank you, both were real. Suspend/lock deadlock — confirmed:
Tests added to
Results (i7-10700): |
|
Windows A/B on the host that reproduces #11813 most strongly (Ryzen 7 5800X, RTX 3080, Windows 11, WinLibs GCC 15.2, Release,
All 20 runs passed. Alone, AI-assisted: yes (Claude Code) |
|
A Windows host where #11813 reproduces every time, for this head
My nightly run on AI-assisted: yes (Claude Code) |
|
A matched Windows comparison on a host that reproduces #11813: Windows 11 Enterprise 10.0.22621, AMD Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
Every abort, on On this machine #12005 removes the aborts and brings the test back to the ~13 s range reported before AI-assisted: yes (Claude Code). |
|
On head
Could the mailbox remove or cancel settled and exiting entries while coordinating safely with posting after AI-assisted: Codex |
|
@mashingaan Verified — the claim is source-correct, and slightly worse than a leak.
One honest residual: a post landing after the reap but before host-thread death — the µs window where a AI-assisted: yes (Devin) |
|
@mashingaan Good catch — that's exactly the gap
On the coordination edge you flagged: a Thanks @babyhuey, @ycbuzi, @JJenkx for the three independent repro-host validations. |
|
Matched Windows comparison at the current head
Per run: main 22.0 (abort), 38.3, 23.2 (abort), 25.2, 37.4, 37.0, 17.6 (abort), 38.5 s; PR 13.0, 13.1, 13.0, 12.9, 13.1, 13.0, 13.0, 13.1 s. The aborted main runs stop early, so main's real cost for a full pass is the 37–38 s of the clean runs. On this host the latest head still removes the abort and brings the test back under its old ~18 s. AI-assisted: yes (Claude Code). |
|
This no longer builds once merged with With only those three names changed to
AI-assisted: yes (Claude Code) |
|
Windows run of this head ( 1. It no longer compiles merged onto current
2. With that rename applied locally, it fixes #11813 here decisively.
Every AI-assisted: yes (Claude Code). |
759f5c5 to
4c5eec4
Compare
|
Rebased onto Verified on Windows 11 (MinGW GCC 15.2.0, Release, BUILD_TESTING=ON): |
|
Thanks, the build break is fixed. Re-run on Windows 11 Pro 10.0.22631, i9-9900K, MSYS2 UCRT64 GCC 16.1.0, Release, with this head (
As in my earlier comment, AI-assisted: yes (Claude Code) |
4c5eec4 to
25a6dfc
Compare
|
Rebased onto current Upstream Verified on the rebased tree (Windows, MinGW): |
25a6dfc to
f66d218
Compare
|
@akkadbakkad89 The mailbox connection appears to have been lost in the current head Compared with the previously reviewed Please restore the mailbox producer and callback on top of current main, preserving the reviewed requirements to enqueue after resuming the target, settle a delivery only at its final settle points, and suppress stale or duplicate delivery. Keep main's newer exception-delivery and thread-registry changes. Please also make a focused Windows regression prove that at least one raise was actually posted and delivered through the mailbox, then verify exactly-once delivery and exit cleanup. The existing reports where main also passes establish coexistence, but cannot discriminate this missing connection. No need to add an assertion that a valid unimplemented call throws. This is a source review; I have not run Windows here. |
…ndows Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f66d218 to
7ff3b70
Compare
|
@oneandonlydean Confirmed and restored at What came back, ported onto the current
Windows regression: |
|
@akkadbakkad89 The mailbox producer is restored in In Please coordinate enqueue admission with final exit cleanup so a post cannot be stranded after the target's final reap, while keeping mailbox locking outside the suspend window. Add a controlled test that pauses the producer after resume, lets the target finish and reap, then completes the post and checks pending entries and reference cleanup. The existing churn/zero-pending checks do not force this ordering; please also make the regression positively observe a mailbox post and delivery. This is a source-derived schedule, not a Windows reproduction. The restored producer fixes the earlier missing-connection finding; the remaining request concerns exit cleanup. |
|
@oneandonlydean Fixed at
Either way the callback executes exactly once and no entry can be left in a box nothing will drain. The lock is still only held around the admission check and the enqueue — never inside the suspend window — and the resolved inline run also happens after the lock is released, mirroring Regression in
|
QueueForDeliveryDrain ran after ResumeThread with no coordination with the target's final ReapDeliveryDrain: a target could exit between resume and enqueue, leaving the post in a box nothing would drain again and retaining the QueuedDelivery reference. Admission is now decided under drainBoxesLock against the target's _finished flag, which is stored before the final reap in program order; denied posts run their callback inline so the entry and the reference are resolved exactly once on either path. The GuestRaiseException regression arms a post hook, holds the producer between resume and enqueue, lets the target finish and reap, then completes the post and requires the drain to stay empty; the churn rounds also assert the post hook observed mailbox posts alongside the delivered raises.
7071ab6 to
2189fb0
Compare
|
Pushed Cause was a compile error, not a test failure: Verified locally: |
✅ AMD GPU test: passedPR head
Failing on main too (9, not counted against this PR)Validation IDs already present on main (5 IDs in 552 tests)Skipped (55)ReproduceAutomated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
What
Fixes the
guest_raise_exceptionregression reported in #11813 (3x slowdown, occasional 5 sAddressWakingtimeout) introduced by540364a24.That commit was right to stop queuing APCs to arbitrary host-code targets: an APC is observable (the wait returns
WAIT_IO_COMPLETION) and its callback must not run a guest handler while the target holds host locks. But for a host-code target that is not inside a libkernel wait it leftArmReturnBreakas the only vehicle, and the unwind's raw stack-pop fallback misses ~99.5% of attempts on thesceKernelSyncOnAddressWakespinner stack shape (16,120not-a-return-siterejections measured; raises surviving 100-1100+ deliverer sweeps).Instead of an APC,
TryDelivernow posts theQueuedDeliveryonce into a per-thread mailbox inTimedWait, drained byDrainApcs— the same place deferred APCs already run. No APC is queued, so host waits keep their contract (insideAlertableWaitstays 0), no handler runs under a host lock, and everyDeferringDeliveryboundary (sceKernelSyncOnAddressWake, waits, semaphore/unpark paths) becomes a deterministic delivery point.Delivery vehicles race through
takenexactly as before. Two guards cover stale entries:closed(set when the queued delivery is released by any owner) and the thread's_finishedflag, so an entry popped after the raise settled — or on a recycled thread id — is dropped instead of delivered.waitCount > 0targets keep the existing APC path unchanged.Related: #8592 adds a target-side deferral counter for shader-preparation host locks. Complementary — this PR's mailbox only fires at libkernel drain points; whichever lands second rebases the
ThreadWaitState/TryDeliverregion.Tested
Windows 11, i7-10700,
guest_raise_exception_tests.exe6/6 PASS at ~10.5-11.5 s (parent-commit timing; regression window measured 11-57 s depending on host) andguest_raise_leaves_host_code_tests.exe7/7 PASS:deliveries 40, outside the image 0, before the alertable wait 10, inside an alertable host wait 0. Instrumented run showed 52 drain-mailbox deliveries (the 50AddressWakingrounds included) and 1 stale entry correctly skipped. No games run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)