audio: serialize blocking writes per output port #841
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!841
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/841/head"
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?
@6d4m on GitHub · 2026-09-26 00:30 UTC · original
6d4m:fix/audioout-per-port-lock-fork→main· +68 −23 · 1 files · 1 commits · closed without mergingFollow-up to #822, from the CodeRabbit review comment there.
AudioOutOutputsvalidates the handle underm_mutex, but the write happens after that lock is released, so two guest threads can enterQueueSdlAudiofor the same port at once. With the write pacing from #822 that means both threads race on the port's output clock and interleave their writes into the same SDL stream.This adds one mutex per output port, held across
QueueSdlAudioand thelast_output_timeupdate. Locking the write but not the clock update would have left the race half open, since the pacing now derives each write's deadline from that field, so a concurrent batch could read it stale and two writes would end up sharing a deadline.Two smaller points while I was in there:
PortOutbecauseAudioOutClosedoesport = {}and would otherwise reset a live mutex.params[0]and then indexed the port array withparams[i].handlefor the rest, so an invalid handle was already undefined behaviour.Lock order is always
m_mutexthen the port mutex, so there is no cycle.AudioOutClosetakes the same lock.Verified no audible change on PPSA03647 and PPSA09232, which is what I would expect from a change that only closes a race that has not been observed firing.
Note: this branch is based on my fork's older
mainrather than current upstream, because pushing the newer workflow commits needs a token scope I do not have. The change itself applies on top of currentmainas is.AI assistance (OpenCode) was used for the investigation and this description; I reviewed the results before posting.
@coderabbitai[bot] on GitHub · 2026-09-26 00:39 UTC · original · edited 2026-09-27 01:07 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
b69cafb2-fd4a-4f7c-b5ff-05f3424334ea📥 Commits
Reviewing files that changed from the base of the PR and between fa325130ae2b936aa87967a25060c0adeb9d859a and
cf70ec2bdd.📒 Files selected for processing (1)
src/libs/audio.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough
Audio adds a mutex for each output port. AudioOutOpen, AudioOutClose, and AudioOutSetVolume use these mutexes when accessing ports. AudioOutOutputs checks port usage and timing under port locks, skips out-of-range or unused ports, and queues audio or haptics while updating each port’s output clock. It returns the first port’s sample count saved before processing.
Suggested reviewers:
nmzikPriority: ⬇️ Low
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
cf70eThe reported output races are addressed, and no new merge-blocking regression is established. The existing stale-handle limitation remains.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
cf70eThe change improves isolation of concurrent writes and does not appear to expand audio access. A busy output port can now delay operations on other ports during close. Handle reuse and batch-size validation remain separate, pre-existing issues.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-09-26 00:39 UTC · original · edited 2026-09-27 01:00 UTC
On
src/libs/audio.cppline 513 (outdated):🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11704
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 31448
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 418
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4108
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 315
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4278
Synchronize port lifetime and reject stale output batches.
AudioOutOutputsvalidates the first handle, releasesm_mutex, and readssamples_numwithout synchronization.AudioOutClosecan clear the port beforeAudioOutOutputsacquires the port mutex. The later division can then usefreq == 0and crash.PrepareOutputBufferandQueueSdlAudioreadsamples_num,format,volume,channels_num,freq, andstreamthroughPortOut. Lock the per-port mutex afterm_mutexinAudioOutOpenandAudioOutSetVolumeso these reads cannot race with updates.Read
first_samplesunder the first port lock. Recheckusedafter every port lock and skip unused ports before pacing or queueing.These checks do not protect reopened ports.
Audio::Idcontains only the reusable port index. An old batch can acquire the lock after close and reopen, observeused == true, and queue stale data into the new port. Add a generation to the handle and validate it under the port lock, or hold the lifecycle lock across the complete output operation so close and reopen cannot occur during that operation.Suggested output-path fix
Add
if (!port.used) { continue; }immediately after acquiring each port lock in both loops.@coderabbitai[bot] on GitHub · 2026-09-26 00:39 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
a7d64ce4-f9d1-461e-a8d5-d3ec0d934c39📥 Commits
Reviewing files that changed from the base of the PR and between
fd2e15ee73and fa325130ae2b936aa87967a25060c0adeb9d859a.📒 Files selected for processing (1)
src/libs/audio.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@6d4m on GitHub · 2026-09-27 00:52 UTC · original
On
src/libs/audio.cppline 513 (outdated):Thanks, all three taken.
first_samplesandblock_timeare now read under the first port lock, withusedandfreq == 0guards, and the call returns 0 if that port is gone.usedafter taking the port lock and skip the port before pacing or queueing.AudioOutOpenandAudioOutSetVolumetake the port lock afterm_mutexas well, so they cannot race with a batch reading the port inQueueSdlAudio.One more change while resolving the conflict with #843: the vibration branch no longer takes
m_mutex. It needed that when the port lifetime was guarded only by the global lock; the port lock covers it now, and takingm_mutexwhile holding a port lock would invert the order used byAudioOutOpen/AudioOutClose.The stale-batch-after-reopen case is not addressed. That needs a generation in
Audio::Id, which is a larger change than this PR, so I left it out deliberately.Also rebased onto
bec67b7, it had gone conflicting.@coderabbitai[bot] on GitHub · 2026-09-27 01:00 UTC · original
On
src/libs/audio.cppline 513 (outdated):@6d4m, thanks for the update. The currentaudio.cppshows the first-port guards, per-portusedchecks, and port locks inAudioOutOpenandAudioOutSetVolume. Removingm_mutexfrom the vibration branch also preserves the stated lock order.The stale-batch-after-reopen case remains possible because
Audio::Idcontains only a reusable port index. I’ll leave this thread open for that case. Would you like me to open a follow-up issue for generation-based handle validation?You are interacting with an AI system.
Pull request closed