fix: synchronize CustomAudioSource's sinks_ against AddSink/RemoveSink - #285
Merged
Conversation
sinks_ was mutated and iterated with no synchronization at all: the CritScope locks in AddSink/RemoveSink/PushAudioData, and the mutex itself, were commented out. AddSink/RemoveSink are called by WebRTC's own thread as a track attaches to or detaches from a peer connection, while PushAudioData runs on whatever thread the application pushes audio from, so this was a genuine data race on the sinks_ vector (iterator invalidation, potential crash), not something fixable by synchronizing on the application side. Restores the lock using webrtc::Mutex/MutexLock (the CriticalSection API this used before is gone from current WebRTC), also covering SetAudioCaptureDelay's write to audio_capture_delay_us_ for the same reason. Fixes #213
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.
Summary
CustomAudioSource'ssinks_vector was mutated/iterated with no synchronization at all — theCritScopelocks inAddSink/RemoveSink/PushAudioData, and the mutex itself, were commented out.AddSink/RemoveSinkare called by WebRTC's own thread as a track attaches to or detaches from a peer connection, whilePushAudioDataruns on whatever thread the application pushes audio from — a genuine data race onsinks_(iterator invalidation, potential crash), not something fixable from the application side.webrtc::Mutex/webrtc::MutexLock(theCriticalSectionAPI this used before is gone from current WebRTC), also coveringSetAudioCaptureDelay's write toaudio_capture_delay_us_for the same reason.concurrentAddRemoveSinkDoesNotCrash) that exercisesaddSink/removeSinkfrom one thread while pushing audio from another.Fixes #213
Test plan
mvn -pl webrtc-jni package -DskipTests— native build succeeds and linksmvn -pl webrtc test -Dtest=CustomAudioSourceTest— 8/8 pass, including the new regression testmvn -pl webrtc test(full module suite) — 149/149 pass