3

Resolved

WasapiCapture class - deadlock in StopRecording()

description

Deadlock has place in StopRecording() in WasapiCapture class in the implementation presented in WavRecording demo code.
When Stop button is pressed then StopRecording() is launched. Flag stop is set to true.
StopRecording calls waits for thread "captureThread.Join();".
When recording thread detects stop flag, then it calls Dispose().
Dispose() calls again StopRecording(), but we already were here and we are waiting for thread. So thread.Join is called for the second time, and we have deadlock problem.
I avoided this problem with extending condition in StopRecording():
if (this.captureThread != null && this.stop == false)
instead of
if (this.captureThread != null)

Regards.
Piotr.

comments

pleasereset wrote Jul 4, 2013 at 2:11 PM

Issue confirmed, thanks Piotr for the fix.

pleasereset wrote Jul 4, 2013 at 2:51 PM

The real problem being calling StopRecording() from the DataAvailable event handler. The capture thread is basically joining itself.

markheath wrote Jul 4, 2013 at 3:17 PM

I think I'll fix this another way. StopRecording() should not do a join, it should just signal for recording to stop. We have the RecordingStopped event that users can wait on if they need to know it has finished

markheath wrote Jul 4, 2013 at 3:24 PM

fix checked in, will be part of the next NAudio 1.7 alpha build