Skip to content

Fix volume control threading and cleanup bugs#12

Merged
marcelveldt merged 1 commit intomusic-assistant:mainfrom
teancom:fix/volume-control-threading
Feb 20, 2026
Merged

Fix volume control threading and cleanup bugs#12
marcelveldt merged 1 commit intomusic-assistant:mainfrom
teancom:fix/volume-control-threading

Conversation

@teancom
Copy link
Copy Markdown
Contributor

@teancom teancom commented Feb 17, 2026

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.

- 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>
@marcelveldt
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @teancom !

@teancom
Copy link
Copy Markdown
Contributor Author

teancom commented Feb 20, 2026

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?

@marcelveldt marcelveldt merged commit e98e377 into music-assistant:main Feb 20, 2026
1 check passed
@marcelveldt
Copy link
Copy Markdown
Member

Fine like this, thanks @teancom !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants