Fix volume control threading and cleanup bugs#12
Merged
marcelveldt merged 1 commit intomusic-assistant:mainfrom Feb 20, 2026
Merged
Fix volume control threading and cleanup bugs#12marcelveldt merged 1 commit intomusic-assistant:mainfrom
marcelveldt merged 1 commit intomusic-assistant:mainfrom
Conversation
- Windows: init/uninit COM on polling thread (was missing entirely) - Windows: reorder Drop to join polling thread before CoUninitialize - macOS: add Drop impl with stop flag to prevent leaked polling threads - Clear VOLUME_CONTROLLER in stop() so platform Drop impls actually run - Stop existing polling thread in set_change_callback before spawning new Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
Thanks! One more thing that we should probably implement with regards to volume control is the option to choose between hardware or software volume control but that is for later. |
Contributor
Author
|
I'm taking a poke at the software volume control thing, no promises it'll actually be usable. 😅 Is this mergable as-is or do you want me to submit the bug fixes and the feature together? |
Member
|
Fine like this, thanks @teancom ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
These fixes address several related threading issues in the volume control subsystem. They all stem from the same root cause — polling threads not being properly lifecycle-managed — so they're bundled together rather than split into separate PRs. BUT! If you prefer separate PRs, I can absolutely do that. Trying to balance 'number of PRs to review' with 'size of PR to review', the eternal struggle.
Fix 1: Windows COM initialization on polling thread
Bug: The volume polling thread calls COM methods (GetMasterVolumeLevelScalar, GetMute) but never called CoInitializeEx on that thread. COM requires per-thread initialization. This could cause CO_E_NOTINITIALIZED failures, meaning external volume changes would silently go undetected on some Windows configurations.
Fix: Added CoInitializeEx(None, COINIT_MULTITHREADED) at the start of the polling thread and CoUninitialize() after the loop exits.
Fix 2: Windows Drop ordering
Bug: The existing Drop impl called CoUninitialize on the creating thread while the polling thread was still running and actively calling COM methods through its Arc. This is a use-after-free.
Fix: Reordered Drop to: set stop flag → join polling thread (which does its own CoUninitialize) → drop the endpoint volume → CoUninitialize on the creating thread. This matches the principle that no COM calls should happen after uninitialization.
Fix 3: macOS missing Drop impl
Bug: MacOSVolumeControl had no Drop impl at all. Dropping the controller abandoned the polling thread, which would run until process exit. Repeated start/stop cycles would accumulate leaked threads.
Fix: Added a Drop impl using the same Arc stop flag pattern as Windows — set the flag, join the thread.
Fix 4: VOLUME_CONTROLLER never cleared in stop()
Bug: stop() cleared SHUTDOWN_TX, COMMAND_TX, and SENDSPIN_CLIENT, but never VOLUME_CONTROLLER. This meant the platform-specific Drop impls (including the new ones from fixes 2 and 3) never ran during normal shutdown — only at process exit, if at all.
Fix: Now stop() clears VOLUME_CONTROLLER as its first action, triggering Drop and joining the polling thread before the rest of shutdown proceeds.
Fix 5: set_change_callback thread leak on repeated calls
Bug: On both Windows and macOS, calling set_change_callback a second time would overwrite the stored thread handle without stopping the previous polling thread. The old thread would run forever with a lost JoinHandle.
Fix: Now both platforms stop and join the existing thread before spawning a new one, using a fresh stop flag so the new thread isn't born already signaled.