XAudio2: GetState keeps the title's context for a buffer that just ended #2400

Open
treybertram06 wants to merge 1 commit from upstream-pr/2410 into main
treybertram06 commented 2026-11-25 18:05:08 +00:00 (Migrated from github.com)

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's GetState and XCL's lookup, OnBufferEnd has already removed the token from live_buffers. The title then gets the token, which is the address of a freed DecodedBuffer, as pCurrentBufferContext. Fixes #2386.

Each voice now keeps its last 64 ended tokens together with the title's context. OnBufferEnd records a token under the live_buffers lock, 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_buffer and title_context (live tokens, then recently ended ones), used by decoded_cb_buffer_end and get_state;
    • the per-voice ended ring. 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);
    • the unit tests get_state_context_survives_buffer_end, which fails without the ring because the token comes back, and ended_token_is_not_reused_by_a_later_buffer, which fails when the ring keeps only the address.

Tested

  • RTX 3080 10 GB (driver 621.40 / 32.0.16.2140), Ryzen 9 5900X, Windows 11 build 26300, Rust 1.100.0, debug tests and release smoke builds.
  • 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_DIR set to DXC 1.9), on the branch rebased onto main e0d93c84: 1423 passed, 0 failed, 46 ignored.
    • The earlier a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier failure was main's; #2420 fixed it.
  • 25 SKIPPED:
    • 21 no wave64;
    • 1 no heap windows;
    • 3 refused for the #1426 f16 / VOP3 semantics.
  • 2026-11-30: the branch still merges into main d52ff48c without 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).

xcl-smoke v1
commit: 68a284a596bd558b6bd9db6eedda6d6aa178c360
gpu: NVIDIA GeForce RTX 3080
title: 4ba6c38e
seconds: 60
exit: 0x0
frames shown: 1497 (PresentX 1497)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15658 MB
xcl-smoke v1
commit: 4767b1a3a3506e6597044152996c5b1ec1ebe01d
gpu: NVIDIA GeForce RTX 3080
title: 4ba6c38e
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14756 MB

Not tested

  • The race itself in a title. I don't have a title that queries GetState on 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 after OnBufferEnd, not XAudio2's own threads.
  • AMD and Intel GPUs. The change is CPU-only.

AI use

  • Claude Code with Claude Opus 5.5. It read the issue and the code, wrote the fix, the test and this description, and ran the tests and the smoke runs above. The author reviewed the diff.
<!-- greyfinch-sync:ivall/xcl:issue:5496565873 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410). Originally posted by [treybertram06](https://github.com/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's `GetState` and XCL's lookup, `OnBufferEnd` has already removed the token from `live_buffers`. The title then gets the token, which is the address of a freed `DecodedBuffer`, as `pCurrentBufferContext`. Fixes #2386. Each voice now keeps its last 64 ended tokens together with the title's context. `OnBufferEnd` records a token under the `live_buffers` lock, 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_buffer` and `title_context` (live tokens, then recently ended ones), used by `decoded_cb_buffer_end` and `get_state`; - the per-voice `ended` ring. 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); - the unit tests `get_state_context_survives_buffer_end`, which fails without the ring because the token comes back, and `ended_token_is_not_reused_by_a_later_buffer`, which fails when the ring keeps only the address. **Tested** - RTX 3080 10 GB (driver 621.40 / 32.0.16.2140), Ryzen 9 5900X, Windows 11 build 26300, Rust 1.100.0, debug tests and release smoke builds. - `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_DIR` set to DXC 1.9), on the branch rebased onto `main` `e0d93c84`: 1423 passed, 0 failed, 46 ignored. - The earlier `a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier` failure was `main`'s; #2420 fixed it. - 25 `SKIPPED`: - 21 `no wave64`; - 1 `no heap windows`; - 3 refused for the #1426 f16 / VOP3 semantics. - 2026-11-30: the branch still merges into `main` `d52ff48c` without 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). ``` xcl-smoke v1 commit: 68a284a596bd558b6bd9db6eedda6d6aa178c360 gpu: NVIDIA GeForce RTX 3080 title: 4ba6c38e seconds: 60 exit: 0x0 frames shown: 1497 (PresentX 1497) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15658 MB ``` ``` xcl-smoke v1 commit: 4767b1a3a3506e6597044152996c5b1ec1ebe01d gpu: NVIDIA GeForce RTX 3080 title: 4ba6c38e seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14756 MB ``` **Not tested** - The race itself in a title. I don't have a title that queries `GetState` on 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 after `OnBufferEnd`, not XAudio2's own threads. - AMD and Intel GPUs. The change is CPU-only. **AI use** - Claude Code with Claude Opus 5.5. It read the issue and the code, wrote the fix, the test and this description, and ran the tests and the smoke runs above. The author reviewed the diff.
DavidM1911 commented 2026-11-25 20:08:37 +00:00 (Migrated from github.com)

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):
gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier
  assertion failed: group barriers in the program (lower_dxil.rs:1155)

The lowered program is missing an expected group barrier (or emits one where none is expected). Needs a fix before merge.

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7172337871 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7172337871). Originally posted by [DavidM1911](https://github.com/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): ``` gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier assertion failed: group barriers in the program (lower_dxil.rs:1155) ``` The lowered program is missing an expected group barrier (or emits one where none is expected). Needs a fix before merge. — Devin
ahmetbagci8 commented 2026-11-25 20:08:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ahmetbagci8 on 2026-11-25T19:40:01Z.


Tested

  • Commit: adf3f635be1eec43e0bf30383ffcb496fbdb5650
  • Hardware / OS: Intel Core i7-11800H, NVIDIA GeForce RTX 3070 Laptop GPU (driver 32.0.16.1714), Windows 11 Pro (Build 26300), DXC v1.9.2609
  • cargo test -j 2 -p xcl --lib xaudio2 -- --test-threads=4: 10 passed; 0 failed; 0 ignored (0.12s)

Not tested

  • Full workspace suite and scripts/smoke.ps1 (no unpacked title locally).

AI use

  • Antigravity harness (Gemini 4 Neon) ran the targeted unit tests on this commit and drafted this report; the author reviewed it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7172607847 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7172607847). Originally posted by [ahmetbagci8](https://github.com/ahmetbagci8) on 2026-11-25T19:40:01Z. --- ### Tested - **Commit:** `adf3f635be1eec43e0bf30383ffcb496fbdb5650` - **Hardware / OS:** Intel Core i7-11800H, NVIDIA GeForce RTX 3070 Laptop GPU (driver `32.0.16.1714`), Windows 11 Pro (Build 26300), DXC `v1.9.2609` - `cargo test -j 2 -p xcl --lib xaudio2 -- --test-threads=4`: `10 passed; 0 failed; 0 ignored (0.12s)` ### Not tested - Full workspace suite and `scripts/smoke.ps1` (no unpacked title locally). ### AI use - Antigravity harness (Gemini 4 Neon) ran the targeted unit tests on this commit and drafted this report; the author reviewed it before posting.
treybertram06 commented 2026-11-25 20:08:46 +00:00 (Migrated from github.com)

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_barrier came in with 254a058 on main, and main's own CI failed on it until #2420 (c828d8c) changed the test. The PR's CI run is red for the same reason.

On main at 06055a7, which includes c828d8c, with this branch (adf3f63) merged on top, on an RTX 3080:

  • cargo test -p xcl --lib a_bridged_mask_write: 1 passed, 0 failed
  • cargo test -p xcl --lib xaudio2: 10 passed, 0 failed

The diff only touches hle/xaudio2.rs. If your checkout of main predates c828d8c, merging the current main should clear it. I'll rebase the branch onto main so CI runs again.

(Checked and written with Claude Code, Claude Opus 5.5.)

<!-- greyfinch-sync:ivall/xcl:comment:7172767334 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7172767334). Originally posted by [treybertram06](https://github.com/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_barrier` came in with `254a058` on `main`, and `main`'s own CI failed on it until #2420 (`c828d8c`) changed the test. The PR's CI run is red for the same reason. On `main` at `06055a7`, which includes `c828d8c`, with this branch (`adf3f63`) merged on top, on an RTX 3080: - `cargo test -p xcl --lib a_bridged_mask_write`: 1 passed, 0 failed - `cargo test -p xcl --lib xaudio2`: 10 passed, 0 failed The diff only touches `hle/xaudio2.rs`. If your checkout of `main` predates `c828d8c`, merging the current `main` should clear it. I'll rebase the branch onto `main` so CI runs again. (Checked and written with Claude Code, Claude Opus 5.5.)
pablogonz12 commented 2026-11-25 20:08:49 +00:00 (Migrated from github.com)

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

  • CachyOS (Arch), kernel 7.3.6-1-cachyos-eevdf, Mesa 26.3.1-5 (RADV GFX1201), AMD Radeon RX 9070 XT 16 GB, Ryzen 9 9900X.
  • GE-Proton11-14 wine (Wine 11.0), scratch prefix, WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b", VKD3D_CONFIG=ignore_shared_fence, WINEDEBUG=-all, DXC 1.9.2609.5.
  • Build: RUSTFLAGS="-C target-feature=+crt-static" cargo xwin test --workspace --release --target x86_64-pc-windows-msvc --no-run; run wine <xcl lib exe> --test-threads=1 xaudio2 and wine <e2e exe> --test-threads=1.
  • Base = main 2898511 with this PR merged on top (clean merge, only this PR differs), head = adf3f63.

Result

base 2898511 this PR adf3f63
xaudio2 (lib filter) FAILED: 8 passed, 1 failed FAILED: 9 passed, 1 failed
e2e (integration) FAILED: 3 passed, 1 failed FAILED: 3 passed, 1 failed

The new test passes (get_state_context_survives_buffer_end is the +1). The failure that matters is unchanged, and it is not the ended-buffer race: GetState returns a null context at the first call, while both buffers are still queued.

Base, crates/xcl/src/hle/xaudio2.rs:1599:

thread 'hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime' (856) panicked at crates/xcl/src/hle/xaudio2.rs:1599:13:
assertion `left == right` failed
  left: 0
 right: 12291

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 between SubmitSourceBuffer and GetState, not at OnBufferEnd.

The e2e test title shows the same thing, on base and on this PR alike:

[FAIL] XAudio2: console flags/device, XMA2 & Opus voices decoded, PCM passes, callbacks: XMA2 GetState: ctx=0x0, queued=1

(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 during Submit.

Not tested

  • No game run.
  • Windows (whether the same first-GetState null happens there is what would decide if this is a Wine-only difference).

AI use

  • DeepSeek V4.1 Flash (OpenCode) built both commits, ran the tests above and wrote this report; no code was written or changed by it.
<!-- greyfinch-sync:ivall/xcl:comment:7172962214 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7172962214). Originally posted by [pablogonz12](https://github.com/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** - CachyOS (Arch), kernel 7.3.6-1-cachyos-eevdf, Mesa 26.3.1-5 (`RADV GFX1201`), AMD Radeon RX 9070 XT 16 GB, Ryzen 9 9900X. - GE-Proton11-14 wine (Wine 11.0), scratch prefix, `WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b"`, `VKD3D_CONFIG=ignore_shared_fence`, `WINEDEBUG=-all`, DXC 1.9.2609.5. - Build: `RUSTFLAGS="-C target-feature=+crt-static" cargo xwin test --workspace --release --target x86_64-pc-windows-msvc --no-run`; run `wine <xcl lib exe> --test-threads=1 xaudio2` and `wine <e2e exe> --test-threads=1`. - Base = `main` 2898511 with this PR merged on top (clean merge, only this PR differs), head = adf3f63. **Result** | | base 2898511 | this PR adf3f63 | |---|---|---| | `xaudio2` (lib filter) | FAILED: 8 passed, 1 failed | FAILED: 9 passed, 1 failed | | `e2e` (integration) | FAILED: 3 passed, 1 failed | FAILED: 3 passed, 1 failed | The new test passes (`get_state_context_survives_buffer_end` is the +1). The failure that matters is unchanged, and it is not the ended-buffer race: `GetState` returns a null context at the **first** call, while both buffers are still queued. Base, `crates/xcl/src/hle/xaudio2.rs:1599`: ``` thread 'hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime' (856) panicked at crates/xcl/src/hle/xaudio2.rs:1599:13: assertion `left == right` failed left: 0 right: 12291 ``` 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 between `SubmitSourceBuffer` and `GetState`, not at `OnBufferEnd`. The e2e test title shows the same thing, on base and on this PR alike: ``` [FAIL] XAudio2: console flags/device, XMA2 & Opus voices decoded, PCM passes, callbacks: XMA2 GetState: ctx=0x0, queued=1 ``` (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 during `Submit`. **Not tested** - No game run. - Windows (whether the same first-`GetState` null happens there is what would decide if this is a Wine-only difference). **AI use** - DeepSeek V4.1 Flash (OpenCode) built both commits, ran the tests above and wrote this report; no code was written or changed by it.
treybertram06 commented 2026-11-25 20:08:54 +00:00 (Migrated from github.com)

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/10 xaudio2 tests on an RTX 3080 (here) and on a 3070 (ahmetbagci8 above). As you say, the context is null at the first GetState, before any buffer ends, so the ended-token ring isn't involved. That looks like Wine's XAudio2 (FAudio) not reporting pCurrentBufferContext for 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 before OnBufferEnd.

(Claude Code, Claude Opus 5.5.)

<!-- greyfinch-sync:ivall/xcl:comment:7173245949 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7173245949). Originally posted by [treybertram06](https://github.com/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/10 `xaudio2` tests on an RTX 3080 (here) and on a 3070 (ahmetbagci8 above). As you say, the context is null at the *first* `GetState`, before any buffer ends, so the ended-token ring isn't involved. That looks like Wine's XAudio2 (FAudio) not reporting `pCurrentBufferContext` for 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 before `OnBufferEnd`. (Claude Code, Claude Opus 5.5.)
bmjask commented 2026-11-25 20:56:33 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:ivall/xcl:review:6064873297 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#pullrequestreview-6064873297). Originally posted by [bmjask](https://github.com/bmjask) on 2026-11-25T20:56:33Z. **Review:** COMMENTED --- P1 ABA bug: end_buffer removes and drops Box<DecodedBuffer> 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.
treybertram06 commented 2026-11-25 22:06:50 +00:00 (Migrated from github.com)

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.

  • ended now holds the ended DecodedBuffers 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 a GetState that raced OnBufferEnd therefore resolves to its own buffer's context, never a newer one's. Voice teardown clears the ring along with live_buffers.
  • New test ended_token_is_not_reused_by_a_later_buffer: it ends A, submits more buffers as an OnBufferEnd callback 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.
  • On the 64-entry limit: a lookup delayed past 64 later buffer ends on the same voice still loses the mapping, and its context then passes through unchanged. Past that point the address can be reused, so it can't be told apart. Holding the reservations for longer would need a time-based retention or an unbounded ring. I think 64 buffer ends is well beyond the window between GetState reading 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.
  • The smoke block in the description is updated.

(Claude Code, Claude Opus 5.5.)

<!-- greyfinch-sync:ivall/xcl:comment:7175585131 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7175585131). Originally posted by [treybertram06](https://github.com/treybertram06) on 2026-11-25T21:49:28Z. --- @bmjask Thanks, that's right. Fixed in `4767b1a`, squashed into the commit. - `ended` now holds the ended `DecodedBuffer`s 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 a `GetState` that raced `OnBufferEnd` therefore resolves to its own buffer's context, never a newer one's. Voice teardown clears the ring along with `live_buffers`. - New test `ended_token_is_not_reused_by_a_later_buffer`: it ends A, submits more buffers as an `OnBufferEnd` callback 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. - On the 64-entry limit: a lookup delayed past 64 later buffer ends on the same voice still loses the mapping, and its context then passes through unchanged. Past that point the address can be reused, so it can't be told apart. Holding the reservations for longer would need a time-based retention or an unbounded ring. I think 64 buffer ends is well beyond the window between `GetState` reading 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. - The smoke block in the description is updated. (Claude Code, Claude Opus 5.5.)
DavidM1911 commented 2026-11-26 03:06:31 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:ivall/xcl:comment:7180740656 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7180740656). Originally posted by [DavidM1911](https://github.com/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
Foniass commented 2026-12-01 07:59:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:59:01Z.


Triage (#1607) – head 4767b1a

No 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 with OnBufferEnd. Main still drops the DecodedBuffer in decoded_cb_buffer_end before the title callback runs, so the race in #2386 is still there. end_buffer/title_context lock live before ended in both places, which keeps the lock order consistent. Holding the ended Box (with its PCM freed) prevents address reuse for the last ENDED_KEEP tokens. 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.

<!-- greyfinch-sync:ivall/xcl:comment:7304724336 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7304724336). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T07:59:01Z. --- **Triage (#1607)** – head 4767b1a No 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 with `OnBufferEnd`. Main still drops the `DecodedBuffer` in `decoded_cb_buffer_end` before the title callback runs, so the race in #2386 is still there. `end_buffer`/`title_context` lock `live` before `ended` in both places, which keeps the lock order consistent. Holding the ended Box (with its PCM freed) prevents address reuse for the last `ENDED_KEEP` tokens. 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.
fromthemoonx commented 2026-12-02 19:28:49 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T19:28:49Z.


merge-check
base: 5ad98adbc3
head: 4767b1a3a3
merge: clean (as text; it does not compile, see build)
build: cargo build --workspace -> failed: error[E0308]: mismatched types at removed._pcm = Box::default() in crates/xcl/src/hle/xaudio2.rs
tests: 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. The hle::xaudio2:: tests with the port, at 8 threads: 40 of 40 runs passed.
review: read the whole diff (1 file, +103 -7).

  1. Blocking: since 652e9cc1 DecodedBuffer::_pcm is a Vec<i16>, and main has a test that builds a DecodedVoiceState, which now needs the new ended field. 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:
diff --git a/crates/xcl/src/hle/xaudio2.rs b/crates/xcl/src/hle/xaudio2.rs
index 548e0889..a6e23711 100644
--- a/crates/xcl/src/hle/xaudio2.rs
+++ b/crates/xcl/src/hle/xaudio2.rs
@@ -597,7 +597,7 @@ fn end_buffer(live: &Mutex<HashMap<usize, Box<DecodedBuffer>>>, ended: &Mutex<Ve
     let mut l = live.lock().unwrap();
     let mut removed = l.remove(&token)?;
     let orig = removed.orig_context;
-    removed._pcm = Box::default();
+    removed._pcm = Vec::new();
     let mut e = ended.lock().unwrap();
     let evicted = if e.len() == ENDED_KEEP { e.pop_front() } else { None };
     e.push_back(removed);
@@ -1905,6 +1905,7 @@ mod tests {
             title_cb: &TEST_CB as *const TestCb as usize,
             xma: true,
             live_buffers: Mutex::new(HashMap::new()),
+            ended: Mutex::new(VecDeque::new()),
         });
         let mut wrapped = DecodedCallback { vtbl: &DECODED_CALLBACK_VTBL, state };
         // The Windows PCM16 voice asks for 1920 bytes (10 ms of 48 kHz stereo).
@@ -1969,7 +1970,7 @@ mod tests {
         let live: Mutex<HashMap<usize, Box<DecodedBuffer>>> = Mutex::new(HashMap::new());
         let ended: Mutex<VecDeque<Box<DecodedBuffer>>> = Mutex::new(VecDeque::new());
         let new_buffer = |ctx: usize| {
-            let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0u8; 4].into_boxed_slice() });
+            let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0i16; 2] });
             let token = &*b as *const DecodedBuffer as usize;
             live.lock().unwrap().insert(token, b);
             token
@@ -2008,7 +2009,7 @@ mod tests {
         let live: Mutex<HashMap<usize, Box<DecodedBuffer>>> = Mutex::new(HashMap::new());
         let ended: Mutex<VecDeque<Box<DecodedBuffer>>> = Mutex::new(VecDeque::new());
         let new_buffer = |ctx: usize| {
-            let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0u8; 4].into_boxed_slice() });
+            let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0i16; 2] });
             let token = &*b as *const DecodedBuffer as usize;
             live.lock().unwrap().insert(token, b);
             token
  1. The fix itself works on real XAudio2, which the description lists as not tested. I played 10 ms XMA buffers on one voice while four threads called GetState, for 20 s per run, and counted contexts the title never set:
run main this PR with the port
1 6133 of 30.4 M 0 of 35.6 M
2 6116 of 29.4 M 0 of 33.1 M
3 6033 of 29.0 M 0 of 33.3 M

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. live is locked before ended in 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 GetState that 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)

<!-- greyfinch-sync:ivall/xcl:comment:7356240609 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2410#issuecomment-7356240609). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-02T19:28:49Z. --- merge-check base: 5ad98adbc360225ac8a57ad9661e16fe8df9d8dd head: 4767b1a3a3506e6597044152996c5b1ec1ebe01d merge: clean (as text; it does not compile, see build) build: cargo build --workspace -> failed: `error[E0308]: mismatched types` at `removed._pcm = Box::default()` in crates/xcl/src/hle/xaudio2.rs tests: 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. The `hle::xaudio2::` tests with the port, at 8 threads: 40 of 40 runs passed. review: read the whole diff (1 file, +103 -7). 1) Blocking: since 652e9cc1 `DecodedBuffer::_pcm` is a `Vec<i16>`, and main has a test that builds a `DecodedVoiceState`, which now needs the new `ended` field. 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: ```diff diff --git a/crates/xcl/src/hle/xaudio2.rs b/crates/xcl/src/hle/xaudio2.rs index 548e0889..a6e23711 100644 --- a/crates/xcl/src/hle/xaudio2.rs +++ b/crates/xcl/src/hle/xaudio2.rs @@ -597,7 +597,7 @@ fn end_buffer(live: &Mutex<HashMap<usize, Box<DecodedBuffer>>>, ended: &Mutex<Ve let mut l = live.lock().unwrap(); let mut removed = l.remove(&token)?; let orig = removed.orig_context; - removed._pcm = Box::default(); + removed._pcm = Vec::new(); let mut e = ended.lock().unwrap(); let evicted = if e.len() == ENDED_KEEP { e.pop_front() } else { None }; e.push_back(removed); @@ -1905,6 +1905,7 @@ mod tests { title_cb: &TEST_CB as *const TestCb as usize, xma: true, live_buffers: Mutex::new(HashMap::new()), + ended: Mutex::new(VecDeque::new()), }); let mut wrapped = DecodedCallback { vtbl: &DECODED_CALLBACK_VTBL, state }; // The Windows PCM16 voice asks for 1920 bytes (10 ms of 48 kHz stereo). @@ -1969,7 +1970,7 @@ mod tests { let live: Mutex<HashMap<usize, Box<DecodedBuffer>>> = Mutex::new(HashMap::new()); let ended: Mutex<VecDeque<Box<DecodedBuffer>>> = Mutex::new(VecDeque::new()); let new_buffer = |ctx: usize| { - let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0u8; 4].into_boxed_slice() }); + let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0i16; 2] }); let token = &*b as *const DecodedBuffer as usize; live.lock().unwrap().insert(token, b); token @@ -2008,7 +2009,7 @@ mod tests { let live: Mutex<HashMap<usize, Box<DecodedBuffer>>> = Mutex::new(HashMap::new()); let ended: Mutex<VecDeque<Box<DecodedBuffer>>> = Mutex::new(VecDeque::new()); let new_buffer = |ctx: usize| { - let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0u8; 4].into_boxed_slice() }); + let b = Box::new(DecodedBuffer { orig_context: ctx, _pcm: vec![0i16; 2] }); let token = &*b as *const DecodedBuffer as usize; live.lock().unwrap().insert(token, b); token ``` 2) The fix itself works on real XAudio2, which the description lists as not tested. I played 10 ms XMA buffers on one voice while four threads called `GetState`, for 20 s per run, and counted contexts the title never set: | run | main | this PR with the port | |---|---|---| | 1 | 6133 of 30.4 M | 0 of 35.6 M | | 2 | 6116 of 29.4 M | 0 of 33.1 M | | 3 | 6033 of 29.0 M | 0 of 33.3 M | 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. `live` is locked before `ended` in 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 `GetState` that 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)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2410:upstream-pr/2410'
git switch 'upstream-pr/2410'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/2410'
git switch 'upstream-pr/2410'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2410'
git switch 'upstream-pr/2410'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2410'
git switch 'main'
git merge --squash 'upstream-pr/2410'
git switch 'main'
git merge --ff-only 'upstream-pr/2410'
git switch 'main'
git merge 'upstream-pr/2410'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2400
No description provided.