fix(milesaudiomanager): Prevent multithread crashing in MilesAudioManager#2718
fix(milesaudiomanager): Prevent multithread crashing in MilesAudioManager#2718xezon wants to merge 2 commits into
Conversation
|
| Filename | Overview |
|---|---|
| Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp | Core fix: introduces two-phase stop/release pattern with CriticalSectionClass to prevent iterator invalidation on the MSS Timer thread; stopPlayingAudio/releasePlayingAudio split is architecturally sound, locked phase strictly avoids Miles API calls. |
| Core/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h | Adds PS_Stopping state, m_playingCs critical section, removes m_stoppedAudio list and processStoppedList; changes nextMusicTrack/prevMusicTrack return types to AsciiString; static_assert guards interlock size assumption. |
| Core/GameEngine/Include/Common/GameAudio.h | Updates virtual interface: nextMusicTrack/prevMusicTrack now return AsciiString; removes getMusicTrackName (callers now use the return value instead). |
| Dependencies/Utility/Utility/interlocked_adapter.h | New compat shim providing typed InterlockedCompareExchange overload for pre-VS2002 MSVC; correctly uses #pragma once and inline keyword. |
Sequence Diagram
sequenceDiagram
participant MT as Main Thread
participant TT as MSS Timer Thread
participant CS as m_playingCs (CriticalSection)
participant Miles as Miles SDK
Note over MT: processPlayingList - unlocked phase
MT->>Miles: AIL_stop_sample / AIL_close_stream
MT->>MT: InterlockedCAS PS_Playing to PS_Stopped
TT->>CS: acquire lock
CS-->>TT: locked
TT->>TT: findPlayingAudioFrom iterates list
TT->>CS: release lock
TT->>TT: notifyOfAudioCompletion sets PS_Stopping
Note over MT: processPlayingList - locked phase
MT->>CS: acquire lock
CS-->>MT: locked
MT->>MT: erase PS_Stopped items, releasePlayingAudio, delete
MT->>CS: release lock
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
Dependencies/Utility/Utility/interlocked_adapter.h:3
The copyright year in this newly created file reads `2025`, but the current year is 2026. Per the project's convention, new community-authored files should carry the year they were created.
```suggestion
** Copyright 2026 TheSuperHackers
```
Reviews (4): Last reviewed commit: "fix(milesaudiomanager): Prevent multithr..." | Re-trigger Greptile
a02315e to
b3fe6c7
Compare
b3fe6c7 to
01d9dbd
Compare
01d9dbd to
025e5e7
Compare
Merge with Rebase
This change is split into 2 commits. The first cleans up some junk in Miles Audio Manager. And the second applies everything necessary for the fix.
It fixes a rare data race in
MilesAudioManager, by preventing accessing potentially invalidated iterators inMilesAudioManager::findPlayingAudioFrom, which runs on the MSS Timer thread. This potentially is no issue in regular builds, because an invalidated iterator might still be usable when the memory was not reused.Note that it NOT possible to simply defer the event processing coming in
MilesAudioManager::notifyOfAudioCompletionto Main thread, because it wants to continue looping sounds immediately, and any delays will introduce audible stutter.Note that the stop playing audio step is often seperated from the release playing audio step, because it is illegal to call into Miles while holding the new critical section due Miles holding its own internal mutex during callbacks to
MilesAudioManager::notifyOfAudioCompletionand Miles API functions.TODO