livekit / client-sdk-flutter

Flutter Client SDK for LiveKit
https://docs.livekit.io
Apache License 2.0
229 stars 108 forks source link

[bug] possible race condition in LocalParticipant.setSourceEnabled #501

Open holzgeist opened 2 months ago

holzgeist commented 2 months ago

Describe the bug

We enable/disable camera and microphone of the local participant in a callback whenever our call state changes. Sometimes we experienced duplicated audio and/or video streams. It turns out, while dart is single-threaded, due to its async nature, race conditions are still possible. Specifically if LocalParticipant.setSourceEnabled (via setCameraEnabled/setMicrophoneEnabled) is called concurrently, there is a race between this check and adding the publication here (video) resp here (audio)

To Reproduce

Call setMicrophoneEnabled concurrently. An over-simplified version would be

      await Future.wait([
        _room.localParticipant!.setMicrophoneEnabled(true),
        _room.localParticipant!.setMicrophoneEnabled(true)
      ]);

A workaround is of course to lock accesses to setSourceEnabled from outside the SDK, but it would be nice if the SDK could handle such cases

Expected behavior

One audio stream is published

Platform information

jezell commented 3 weeks ago

We've also seen this problem from time to time. There is a race condition in the dart code.