XAudio2: GetState keeps the title's context for a buffer that just ended #2400
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2410"
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 ivall/xcl. Originally posted by treybertram06 on 2026-11-25T18:05:08Z.
On a decoded (XMA / Opus) source voice, XCL submits each buffer with its own token as the context and maps it back to the title's context in
GetState. If the buffer ends between XAudio2'sGetStateand XCL's lookup,OnBufferEndhas already removed the token fromlive_buffers. The title then gets the token, which is the address of a freedDecodedBuffer, aspCurrentBufferContext. Fixes #2386.Each voice now keeps its last 64 ended tokens together with the title's context.
OnBufferEndrecords a token under thelive_bufferslock, so a lookup always finds the token in one of the two. The issue suggests clearing the context to null on a miss. That would also clear the context of a buffer the title queued itself, which XCL never wraps, so a context XCL didn't make still passes through unchanged.What is in it
crates/xcl/src/hle/xaudio2.rs:end_bufferandtitle_context(live tokens, then recently ended ones), used bydecoded_cb_buffer_endandget_state;endedring. It holds each ended buffer's allocation, with its PCM freed, so a later buffer can't get the address of a token that's still kept (the ABA race from review);get_state_context_survives_buffer_end, which fails without the ring because the token comes back, andended_token_is_not_reused_by_a_later_buffer, which fails when the ring keeps only the address.Tested
cargo test -p xcl --lib xaudio2: 11 passed, 0 failed. Each new test fails without the part of the change it covers.cargo test --workspace --no-fail-fast -- --test-threads=4(XCL_DXC_DIRset to DXC 1.9), on the branch rebased ontomaine0d93c84: 1423 passed, 0 failed, 46 ignored.a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrierfailure wasmain's; #2420 fixed it.SKIPPED:no wave64;no heap windows;maind52ff48cwithout conflicts (git merge-tree). It was not retested there.Regression run
The title used here decodes its audio through XAPU, so these runs do not reach the changed code (see Not tested).
Not tested
GetStateon an XMA / Opus voice while its buffers end, and the title in the regression run uses the XAPU path. The unit test covers the lookup afterOnBufferEnd, not XAudio2's own threads.AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T19:28:07Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: 1 real failure, deterministic (also fails--test-threads=1, 1219/1):The lowered program is missing an expected group barrier (or emits one where none is expected). Needs a fix before merge.
— Devin
Imported from ivall/xcl. Originally posted by ahmetbagci8 on 2026-11-25T19:40:01Z.
Tested
adf3f635be1eec43e0bf30383ffcb496fbdb565032.0.16.1714), Windows 11 Pro (Build 26300), DXCv1.9.2609cargo test -j 2 -p xcl --lib xaudio2 -- --test-threads=4:10 passed; 0 failed; 0 ignored (0.12s)Not tested
scripts/smoke.ps1(no unpacked title locally).AI use
Imported from ivall/xcl. Originally posted by treybertram06 on 2026-11-25T19:47:22Z.
Thanks for running it. That failure isn't from this PR.
a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barriercame in with254a058onmain, andmain's own CI failed on it until #2420 (c828d8c) changed the test. The PR's CI run is red for the same reason.On
mainat06055a7, which includesc828d8c, with this branch (adf3f63) merged on top, on an RTX 3080:cargo test -p xcl --lib a_bridged_mask_write: 1 passed, 0 failedcargo test -p xcl --lib xaudio2: 10 passed, 0 failedThe diff only touches
hle/xaudio2.rs. If your checkout ofmainpredatesc828d8c, merging the currentmainshould clear it. I'll rebase the branch ontomainso CI runs again.(Checked and written with Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by pablogonz12 on 2026-11-25T19:56:07Z.
Test report from Linux + Wine on AMD: this change does not fix #2386 on this lane, and the assertion it targets is reproducible here exactly. Prepared with AI assistance: DeepSeek V4.1 Flash in OpenCode; the commands were run by it and the output is pasted verbatim.
Setup
RADV GFX1201), AMD Radeon RX 9070 XT 16 GB, Ryzen 9 9900X.WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b",VKD3D_CONFIG=ignore_shared_fence,WINEDEBUG=-all, DXC 1.9.2609.5.RUSTFLAGS="-C target-feature=+crt-static" cargo xwin test --workspace --release --target x86_64-pc-windows-msvc --no-run; runwine <xcl lib exe> --test-threads=1 xaudio2andwine <e2e exe> --test-threads=1.main2898511with this PR merged on top (clean merge, only this PR differs), head =adf3f63.Result
2898511adf3f63xaudio2(lib filter)e2e(integration)The new test passes (
get_state_context_survives_buffer_endis the +1). The failure that matters is unchanged, and it is not the ended-buffer race:GetStatereturns a null context at the first call, while both buffers are still queued.Base,
crates/xcl/src/hle/xaudio2.rs:1599:Same assertion on this PR (line 1626 after the added code), same values:
left: 0,right: 12291(0x3003=CTX_VALID).assert_eq!({ st.buffers_queued }, 2)passes just above it, so the context is lost betweenSubmitSourceBufferandGetState, not atOnBufferEnd.The e2e test title shows the same thing, on base and on this PR alike:
(its other
[FAIL]on this lane is the window-cloak check, which Wine cannot satisfy). So the ring for ended tokens does not cover what happens here: on Wine the context is already null before any buffer ends. Two ways that could be Wine-only, worth ruling out before this is read as a Windows bug: no audio device in the prefix (playback may complete immediately), or the decoded path ending the buffer duringSubmit.Not tested
GetStatenull happens there is what would decide if this is a Wine-only difference).AI use
Imported from ivall/xcl. Originally posted by treybertram06 on 2026-11-25T20:08:30Z.
Thanks, that's useful, but it's a different problem from #2386. The failing test,
xma2_source_voice_decodes_and_manages_lifetime, fails the same way on your base without this PR, and it passes on Windows: 10/10xaudio2tests on an RTX 3080 (here) and on a 3070 (ahmetbagci8 above). As you say, the context is null at the firstGetState, before any buffer ends, so the ended-token ring isn't involved. That looks like Wine's XAudio2 (FAudio) not reportingpCurrentBufferContextfor a voice that hasn't been started; Windows' XAudio2 reports the head buffer's context there. It could be worth its own issue for the Wine lane. This PR only covers the race in #2386: a token read just beforeOnBufferEnd.(Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by bmjask on 2026-11-25T20:56:33Z.
Review: COMMENTED
P1 ABA bug: end_buffer removes and drops Box while ended retains only (token, orig_context). An in-flight GetState can read A token, audio thread drops A and invokes title callback, callback submits B whose allocation reuses A address, then the hook resolves stale A token through live_buffers and returns B context. Keep ended DecodedBuffers/token reservations alive through eviction and add a forced-address-reuse race test. The fixed 64-entry retention also means sufficiently delayed lookup loses the mapping.
Imported from ivall/xcl. Originally posted by treybertram06 on 2026-11-25T21:49:28Z.
@bmjask Thanks, that's right. Fixed in
4767b1a, squashed into the commit.endednow holds the endedDecodedBuffers themselves, not(token, context)copies. Their PCM is freed, but the allocation stays in the ring until it is evicted. While a token can still be looked up, no buffer submitted after it can get that address. A stale token from aGetStatethat racedOnBufferEndtherefore resolves to its own buffer's context, never a newer one's. Voice teardown clears the ring along withlive_buffers.ended_token_is_not_reused_by_a_later_buffer: it ends A, submits more buffers as anOnBufferEndcallback would, and checks two things. The ring holds A's own allocation, and no later buffer gets A's address. Whether the heap would hand a freed address straight back is up to the allocator, so the hold is asserted directly. With the ring keeping copies instead of the allocations, the test fails on that assertion.GetStatereading the context and the hook's lookup, but I can make it time-based if you'd rather.Tested on
4767b1a(RTX 3080, driver 621.40, Windows 11 26300):cargo test -p xcl --lib xaudio2: 11 passed, 0 failed.cargo test --workspace --no-fail-fast -- --test-threads=4: 1423 passed, 0 failed, 46 ignored.(Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T02:22:16Z.
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.Thanks!
— Devin
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:59:01Z.
Triage (#1607) – head
4767b1aNo open or merged PR makes the same change. #890 and #1698 also change
GetState, but for the case where the host reports no buffer, not the race withOnBufferEnd. Main still drops theDecodedBufferindecoded_cb_buffer_endbefore the title callback runs, so the race in #2386 is still there.end_buffer/title_contextlocklivebeforeendedin both places, which keeps the lock order consistent. Holding the ended Box (with its PCM freed) prevents address reuse for the lastENDED_KEEPtokens. No problems found in the diff. The PR needs a rebase: xaudio2.rs has changed a lot on main.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T19:28:49Z.
merge-check
base:
5ad98adbc3head:
4767b1a3a3merge: clean (as text; it does not compile, see build)
build: cargo build --workspace -> failed:
error[E0308]: mismatched typesatremoved._pcm = Box::default()in crates/xcl/src/hle/xaudio2.rstests: the PR as it is: none, it does not build. With the four-line port below: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3007 passed, 0 failed, 52 ignored (3004 / 0 / 52 on
817eb1f, plus this PR's 2 tests and 1 that main gained since); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: none. Thehle::xaudio2::tests with the port, at 8 threads: 40 of 40 runs passed.review: read the whole diff (1 file, +103 -7).
652e9cc1DecodedBuffer::_pcmis aVec<i16>, and main has a test that builds aDecodedVoiceState, which now needs the newendedfield. Git merges this without a conflict, so the green CI on the old head does not show it. This port builds and is what I tested:GetState, for 20 s per run, and counted contexts the title never set:So the race in #2386 is real on Windows, and the ring closes it here. The calls are far denser than a title's; this says nothing about how often a title hits it.
3) No other findings.
liveis locked beforeendedin both places, and the ended buffers are freed after the Windows voice is destroyed.4) Not tested: a retail title (none on this machine). The smoke pair in the description is for the current head, so it has to be rerun after the rebase.
performance: each
GetStatethat misses the live buffers scans at most 64 entries; the ended buffers keep no PCM (reasoned; no slowdown showed in the runs above)verdict: not yet: does not compile on current main; with the port above and a new smoke run I have no objection left
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.