Skip to content

Commit 9beff01

Browse files
authored
fix(rtc): create the FfiClient singleton under a lock (#918)
1 parent bca4a97 commit 9beff01

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-rtc": patch
3+
---
4+
5+
Create the shared `FfiClient` under a lock, so rooms connected from several threads at once no longer lose their connect callback and panic the process.

‎livekit-rtc/livekit/rtc/_ffi_client.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,17 @@ def to_python_level(level: proto_ffi.LogLevel.ValueType) -> Optional[int]:
231231
class FfiClient:
232232
_instance: Optional["FfiClient"] = None
233233
_ffi_disposed: bool = False
234+
_instance_lock = threading.Lock()
234235

235236
@classproperty
236237
def instance(cls) -> "FfiClient":
237-
if cls._instance is None:
238-
cls._instance = FfiClient()
239-
return cls._instance
238+
if cls._instance is not None:
239+
return cls._instance
240+
# threads that race here would each get their own client and lose events sent to the other
241+
with cls._instance_lock:
242+
if cls._instance is None:
243+
cls._instance = FfiClient()
244+
return cls._instance
240245

241246
def __init__(self) -> None:
242247
FfiClient._ffi_disposed = False

0 commit comments

Comments
 (0)