[pr-fix][audio] Decode XMA on demand if its worker cannot start #248

Open
andrewpozdnakov7-jpg wants to merge 2 commits from andrewpozdnakov7-jpg/Darner:port/4800-fallback-when-xma-thread-fails into main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

When starting the XMA worker fails, main remembers the caller thread as though it were the decoder. Unparking it never performs a decode. Keep the failure separate from a working decoder and fill the affected voice synchronously on enable or read advancement. The two callers release the voice lock before waking the decoder.

Restores locally saved VIctory-NVIDIA work from December 1, 2026. Recovered revision: b97f584. Adapted by andrewpozdnakov7-jpg (GitHub profile).

How it was tested

  • Tested revision: 5656a59 on f3840d7.
  • Commands and results: The focused Rust fixture exercised the committed fallback dispatch and per-voice helper with an injected failed worker. Main filled nothing; the patch filled only the enabled voice on both wakes, ignored a disabled voice and kept decoding off the caller when a worker was present. cargo check --locked -p darner-hle --lib --tests, xtask policy, and git diff --check passed.
  • Follow-up revision: d57fea1 passed the injected outcome fixture and the focused HLE type check.
  • Changes a default: no.
  • Follow-up d57fea1 based on Jesper-A's review: keep the existing XMA guard in the synchronous fallback. A focused fixture exercised the new call sites after releasing the voice-slot lock, with and without a worker. cargo check --locked -p darner-hle --lib --tests, xtask policy and git diff --check passed.
  • Gates run on that revision: none of the three workspace-wide gates.
  • Coverage gaps: Synthetic voice output and injected startup failure. Gameplay and device-audio validation remain pending. In fallback mode, input-ready changes that arrive without an enable or read-advance command wait for that next command.

Checklist

  • I ran this change in the game.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: None found.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: <!-- optional --> ## What it changes When starting the XMA worker fails, main remembers the caller thread as though it were the decoder. Unparking it never performs a decode. Keep the failure separate from a working decoder and fill the affected voice synchronously on enable or read advancement. The two callers release the voice lock before waking the decoder. Restores locally saved VIctory-NVIDIA work from December 1, 2026. Recovered revision: b97f584. Adapted by [andrewpozdnakov7-jpg](https://git.gtahub.st/andrewpozdnakov7-jpg) ([GitHub profile](https://github.com/andrewpozdnakov7-jpg)). ## How it was tested - Tested revision: 5656a59 on f3840d7. - Commands and results: The focused Rust fixture exercised the committed fallback dispatch and per-voice helper with an injected failed worker. Main filled nothing; the patch filled only the enabled voice on both wakes, ignored a disabled voice and kept decoding off the caller when a worker was present. cargo check --locked -p darner-hle --lib --tests, xtask policy, and git diff --check passed. - Follow-up revision: d57fea1 passed the injected outcome fixture and the focused HLE type check. - Changes a default: no. - Follow-up d57fea1 based on [Jesper-A's review](https://git.gtahub.st/TrustNo1/Darner/pulls/248#issuecomment-862281): keep the existing XMA guard in the synchronous fallback. A focused fixture exercised the new call sites after releasing the voice-slot lock, with and without a worker. cargo check --locked -p darner-hle --lib --tests, xtask policy and git diff --check passed. - Gates run on that revision: none of the three workspace-wide gates. - Coverage gaps: Synthetic voice output and injected startup failure. Gameplay and device-audio validation remain pending. In fallback mode, input-ready changes that arrive without an enable or read-advance command wait for that next command. ## Checklist - [ ] I ran this change in the game. - [x] This PR contains one change based on main. - [x] The diff follows docs/asset-policy.md. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: None found. - Replaces or overlaps: none found
Adapt locally saved VIctory-NVIDIA work from December 1, 2026 (b97f584). If starting the dedicated decoder fails, fill the affected voice when it is enabled or advances its read cursor.

https://git.gtahub.st/andrewpozdnakov7-jpg
https://github.com/andrewpozdnakov7-jpg
Collaborator

Code-read review of 5656a59. I did not build or run it.

The main-branch bug is real (audio.rs:943 on main stores std::thread::current() when the spawn fails, so nothing ever decodes), and the on-demand fallback is the right idea. One problem in it:

Second read lock on the XMA RwLock from the same thread.

  • xma_command takes let xma = xma(); (audio.rs:961), a read guard on the static XMA, and keeps it for the whole command. ENABLE_XMAS calls xma.enable(i) and ADVANCE_XMA_READ calls xma.advance_read(..) under it.
  • Both now end in wake_decoder(index) (audio.rs:615, 664). With no decoder thread, wake_decoder calls xma().pump_voice(index) (audio.rs:955), which is a second XMA.read() on the same thread.
  • std says RwLock::read "might panic when called if the lock is already held by the current thread", and a recursive read can also block when a writer is queued. XMA.write() is taken at audio.rs:373 (context arrays allocated) and audio.rs:382 (released).
  • Failure scenario: the worker did not start, the thread running xma_command enters ENABLE_XMA(S), and another thread allocates or releases the context arrays at that moment. The command thread then waits on itself. It is rare, but it sits in the fallback this PR adds.

Suggested fix: do not re-enter xma(). enable and advance_read already have &self, so make wake_decoder return whether a decoder thread exists and pump through self:

drop(slot);
if !wake_decoder() { self.pump_voice(index); }

(wake_decoder would drop its index argument and return decoder.is_some() after the unpark.) pump_voice re-takes only voices[index], and both callers have already dropped slot, so that is safe.

Limitation worth a line in the body. In fallback mode a voice is pumped only on ENABLE_XMA(S) and ADVANCE_XMA_READ. decode reads the input-valid bits straight from context memory (audio.rs:737, context.field(0, 20, 2)), and its comment (audio.rs:739) says the title "can refill either input while the context stays enabled". A refill like that is not decoded until the next enable or advance. It is still better than main, which decodes nothing, but it is not equivalent to the worker.

This comment was written with AI assistance (Claude Code).

Code-read review of 5656a59. I did not build or run it. The main-branch bug is real (`audio.rs:943` on main stores `std::thread::current()` when the spawn fails, so nothing ever decodes), and the on-demand fallback is the right idea. One problem in it: **Second read lock on the `XMA` RwLock from the same thread.** - `xma_command` takes `let xma = xma();` (`audio.rs:961`), a read guard on the static `XMA`, and keeps it for the whole command. `ENABLE_XMAS` calls `xma.enable(i)` and `ADVANCE_XMA_READ` calls `xma.advance_read(..)` under it. - Both now end in `wake_decoder(index)` (`audio.rs:615`, `664`). With no decoder thread, `wake_decoder` calls `xma().pump_voice(index)` (`audio.rs:955`), which is a second `XMA.read()` on the same thread. - std says `RwLock::read` "might panic when called if the lock is already held by the current thread", and a recursive read can also block when a writer is queued. `XMA.write()` is taken at `audio.rs:373` (context arrays allocated) and `audio.rs:382` (released). - Failure scenario: the worker did not start, the thread running `xma_command` enters `ENABLE_XMA(S)`, and another thread allocates or releases the context arrays at that moment. The command thread then waits on itself. It is rare, but it sits in the fallback this PR adds. Suggested fix: do not re-enter `xma()`. `enable` and `advance_read` already have `&self`, so make `wake_decoder` return whether a decoder thread exists and pump through `self`: ```rust drop(slot); if !wake_decoder() { self.pump_voice(index); } ``` (`wake_decoder` would drop its `index` argument and return `decoder.is_some()` after the unpark.) `pump_voice` re-takes only `voices[index]`, and both callers have already dropped `slot`, so that is safe. **Limitation worth a line in the body.** In fallback mode a voice is pumped only on `ENABLE_XMA(S)` and `ADVANCE_XMA_READ`. `decode` reads the input-valid bits straight from context memory (`audio.rs:737`, `context.field(0, 20, 2)`), and its comment (`audio.rs:739`) says the title "can refill either input while the context stays enabled". A refill like that is not decoded until the next enable or advance. It is still better than main, which decodes nothing, but it is not equivalent to the worker. This comment was written with AI assistance (Claude Code).
Have wake_decoder report whether it woke a worker. Both command paths already have the current XMA instance, so use that instance for the fallback after releasing the voice slot. This avoids taking another XMA read guard in the command path. Based on #248 (comment).

https://git.gtahub.st/andrewpozdnakov7-jpg
https://github.com/andrewpozdnakov7-jpg

Co-authored-by: Jesper-Andersson <[email protected]>
Author
First-time contributor

Thanks for tracing the lock. The current head, d57fea1, pumps the voice through the XMA instance the command already holds after releasing the voice lock. That removes the second global read-lock acquisition on the fallback path. The injected outcome fixture and focused HLE type check passed. I also recorded the input-ready limitation in the description.

Thanks for tracing the lock. The current head, `d57fea1`, pumps the voice through the XMA instance the command already holds after releasing the voice lock. That removes the second global read-lock acquisition on the fallback path. The injected outcome fixture and focused HLE type check passed. I also recorded the input-ready limitation in the description.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'port/4800-fallback-when-xma-thread-fails:andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'

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 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git rebase 'main'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git rebase 'main'
git switch 'main'
git merge --no-ff 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'main'
git merge --squash 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git switch 'main'
git merge 'andrewpozdnakov7-jpg-port/4800-fallback-when-xma-thread-fails'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!248
No description provided.