wait for the portaudio finished callback to avoid sporadic deadlocks #14208
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.
This fixes #14055 . I describe the issue in detail in the bug report.
The fix consists of using Pa_SetStreamFinishedCallback to set a callback to wait for the stream to finish before continuing the main thread.
I have tested this by starting and stopping the devices programmatically in a loop. After 5000 iterations I still haven't had a deadlock, so I am confident the issue is fixed (without the fix I would get a deadlock every ~50 iterations).
Btw, there was this warning in the code (I have removed it):
This might be the same problem as described in #14055, and it is possible that with this fix we could also use Pa_AbortStream, instead of using the paAbort return value from the callback. But I didn't want to change the code more than necessary, and only add the wait condition, which is simple and safe.