Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion livekit-rtc/livekit/rtc/_ffi_client.py
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Callback still uses auto-creating singleton property, inconsistent with dispose()

The ffi_event_callback at livekit-rtc/livekit/rtc/_ffi_client.py:194 still accesses FfiClient.instance (the auto-creating @classproperty), while FfiHandle.dispose() was deliberately changed to use FfiClient._instance (the raw class variable) to avoid auto-creation during shutdown or in forked children. In practice this is likely safe because native threads aren't inherited across fork(), so the callback shouldn't fire in a child process. However, for consistency with the fork-safety intent of this PR, it might be worth using FfiClient._instance here too (with a None guard).

(Refers to line 194)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def disposed(self) -> bool:
def dispose(self) -> None:
if self.handle != INVALID_HANDLE and not self._disposed:
self._disposed = True
assert FfiClient.instance._ffi_lib.livekit_ffi_drop_handle(ctypes.c_uint64(self.handle))
ffi = FfiClient._instance
if ffi is None or ffi._pid != os.getpid():
return
dropped = ffi._ffi_lib.livekit_ffi_drop_handle(ctypes.c_uint64(self.handle))
assert dropped
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.

def __repr__(self) -> str:
return f"FfiHandle({self.handle})"
Expand Down Expand Up @@ -218,6 +222,7 @@ def instance(cls) -> "FfiClient":
return cls._instance

def __init__(self) -> None:
self._pid = os.getpid()
self._lock = threading.RLock()
self._queue = FfiQueue[proto_ffi.FfiEvent]()

Expand Down Expand Up @@ -253,16 +258,25 @@ def __init__(self) -> None:
)

ffi_lib = self._ffi_lib
init_pid = self._pid

@atexit.register
def _dispose_lk_ffi() -> None:
if os.getpid() != init_pid:
return
ffi_lib.livekit_ffi_dispose()

@property
def queue(self) -> FfiQueue[proto_ffi.FfiEvent]:
return self._queue

def request(self, req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse:
if self._pid != os.getpid():
raise RuntimeError(
"livekit.rtc was used in a parent process before fork(); the native "
"runtime cannot be used across fork(). Do not create or connect a Room "
"(or use any livekit.rtc object) before forking child processes."
)
proto_data = req.SerializeToString()
proto_len = len(proto_data)
data = (ctypes.c_ubyte * proto_len)(*proto_data)
Expand Down
Loading