-
Notifications
You must be signed in to change notification settings - Fork 120
rtc: fail fast instead of hanging when FFI is used across fork() #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
devin-ai-integration[bot] marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -83,7 +83,10 @@ 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)) | ||||||||||||||||||||||||||||
| dropped = FfiClient.instance._ffi_lib.livekit_ffi_drop_handle( | ||||||||||||||||||||||||||||
| ctypes.c_uint64(self.handle) | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| assert dropped | ||||||||||||||||||||||||||||
|
Comment on lines
83
to
+89
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Fork-safety guard missing on native handle cleanup, allowing child processes to hang or crash on exit Inherited native handles are released into the dead runtime ( Impact: A forked child process may hang or crash at exit when Python's garbage collector cleans up inherited handle objects. Mechanism: FfiHandle.dispose() bypasses the fork guard added to request()The PR adds a PID check in When a parent process creates livekit objects (Room, tracks, audio streams, etc.), each creates Multiple callers use
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def __repr__(self) -> str: | ||||||||||||||||||||||||||||
| return f"FfiHandle({self.handle})" | ||||||||||||||||||||||||||||
|
|
@@ -218,6 +221,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]() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -253,16 +257,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) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.