Repository navigation
ASIO, WASAPI: don't let stopStream block for good on a driver that stops calling back - #496
Merged
garyscavone merged 2 commits intoNov 30, 2026
Conversation
The drain is signaled from the driver's buffer switch callback, so a driver that never calls back (one that did not start, or lost its device) blocked stopStream, and the thread that called it, for good. Wait one second plus four buffers, warn, and stop the driver anyway.
The stream thread waited without limit for the capture and render events, so a device that stopped signaling left the thread, and stopStream waiting for it, blocked for good. Wait two seconds, as Microsoft's event-driven samples do; the loop then sees the stream stopping, or GetBuffer fails for a device that is gone.
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.
Problem
RtApiAsio::stopStream()setsdrainCounter = 2and waitsINFINITEonhandle->condition,which only the driver's
bufferSwitchcallback signals. If the driver never calls back (it didnot really start, or lost its device),
stopStream()never returns, and neither does the threadthat called it. In an app that is usually the UI thread, during a device change.
We hit this in a stress test of an app built on clap-wrapper's standalone host. Switching the
driver to ASIO (ASIO4ALL) and straight back to WASAPI froze the UI thread:
ASIO4ALL's thread was idle in
GetMessageAand never called back.RtApiWasapihas the same weakness:wasapiThreadwaitsINFINITEfor the capture and renderevents, and
stopStream()waitsINFINITEfor that thread while holdingstream_.mutex.Reproduction
stop_hang.cpp(below) opens the default output device, starts it, then callsstopStream()under a watchdog. With
inject, it first callsASIOStop()directly so the driver stops callingback. That gives the same state as a failed driver, on demand and with any ASIO driver.
stopStream()injectstopStream()inject×3Windows 11, MSVC 19.52 x64, 48 kHz, 256 frames.
Change
RTAUDIO_WARNING("the driver did not drain the stream, stopping it anyway") and go on toASIOStop()as before. A driver that does call back drains in two or three buffers, so thisonly changes the stuck case.
event-driven sample uses
(Exclusive-Mode Streams).
After a timeout the loop carries on:
GetBufferreturns no frames (or fails for a removeddevice and the thread exits), and the loop sees
STREAM_STOPPING.The two commits are independent. The WASAPI one comes from reading the code, not from a captured
hang. I couldn't make a WASAPI device stop signaling on demand, so if you'd rather take only the
ASIO commit, that's fine.
Not touched: DirectSound's drain wait is signaled from RtAudio's own thread, not a driver
callback. Core Audio and JACK use a condition variable in the same way, but I can't test those
here.
stop_hang.cpp — the reproduction
build.cmd — from an MSVC x64 prompt