-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pa_Initialize(): Guard against recursive calls. #794
Conversation
The proposed code returns A new error code could be added to the |
b34f98c
to
2dabaa6
Compare
Done |
86a241e
to
92625ce
Compare
307ae20
to
9f718fd
Compare
Done. |
Looking good. I think we should add a new error code. Perhaps call it Line 155 in 65e5b82
With a new error text case added here: portaudio/src/common/pa_front.c Line 456 in ed922d9
|
This fixes a stack overflow when a diver uses portaudio as well like FlexAsio
65e5b82
to
9885ecc
Compare
src/common/pa_front.c
Outdated
{ | ||
// a concurrent initialization is already running | ||
PA_DEBUG(("Attempting to re-enter Pa_Initialize(), aborting!\n")); | ||
result = paInternalError; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new error code as described here: #794 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve. I fixed a typo.
@dechamps @MichalPetryka I think this is ready to merge. Please could you confirm that it fixes #766 or advise if you have any objections. If you're happy use the GitHub review feature to approve it. |
The code looks like by me. I'll let @MichalPetryka confirm that it fixes #766 as they're the reporter.
I don't think I have the project permissions to do that. (It's either that or I can't find the button.) |
@@ -154,6 +154,7 @@ static PaUtilHostApiRepresentation **hostApis_ = 0; | |||
static int hostApisCount_ = 0; | |||
static int defaultHostApiIndex_ = 0; | |||
static int initializationCount_ = 0; | |||
static int initializing_ = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be marked volatile to prevent the compiler from doing threading unsafe optimizations here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My original version had this volatile due to the assumption that this will prevent the compiler form reordering.
However this assumption is not guaranteed and fortunately also not necessary, because the compiler cannot move code across external function calls like InitializeHostApis()
in our case.
Another aspect is thread safety. The volatile keyword can be part of the solution but, we need also atomic test_and_set() which is not available in C89. Since thread safety is not a requirement here and also not build in any other call the using code is responsible anyway and it is no point in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that initialization and shutdown are rare enough that using a mutex here to ensure thread safety would be okay. @RossBencina What do you say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A mutex will create a deadlock in the recursive case.
IMHO this PR is good enough, protecting Portaudio in concurrent situation is not required for the original problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good discussion of thread safety. But currently PortAudio is not thread safe. This fix prevents a crash caused by a stack overflow. It does not solve the thread safety issue but it is a step in the right direction. We can consider full thread safety in a later PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that initialization and shutdown are rare enough that using a mutex here to ensure thread safety would be okay.
This is a reasonable point. However, PortAudio is a single threaded API, and this patch is intended to prevent recursion on a single thread. According to our API semantics Pa_Initialize()
may not be called concurrently on two threads, and even our initializationCount_
mechanism is not thread-safe. Furthermore, we currently have no portable mutex infrastructure so adding a mutex here is a non-trivial change.
So my vote would be to merge this as-is and worry about whether initialize/terminate, or indeed the whole API, should be made thread-safe as a separate issue.
@@ -154,6 +154,7 @@ static PaUtilHostApiRepresentation **hostApis_ = 0; | |||
static int hostApisCount_ = 0; | |||
static int defaultHostApiIndex_ = 0; | |||
static int initializationCount_ = 0; | |||
static int initializing_ = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good discussion of thread safety. But currently PortAudio is not thread safe. This fix prevents a crash caused by a stack overflow. It does not solve the thread safety issue but it is a step in the right direction. We can consider full thread safety in a later PR.
@MichalPetryka outside your multi-threaded thread-safety concerns could you please confirm that this patch fixes the originally identified issue #766. |
@dechamps Go to the "Files Changed" tab and at the top right there is a green "Review Changes" button. |
I've created #808 for discussing multi-thread issues. |
Yeah, it does. |
This fixes a stack overflow when a diver uses portaudio as well like FlexAsio
Fixes: #766