Repository navigation
fix(rtc): avoid AssertionError in FfiHandle.__del__ after global FFI disposal - #857
Open
NabiBukhsh-AI wants to merge 2 commits into
Open
NabiBukhsh-AI wants to merge 2 commits into
NabiBukhsh-AI wants to merge 2 commits into
Conversation
livekit#845) When the process exits, livekit_ffi_dispose clears all native handles globally. Subsequent interpreter finalization / garbage collection then invokes __del__ on surviving FfiHandle wrappers, which calls livekit_ffi_drop_handle. Since the native runtime is already disposed, the call returns false, causing an AssertionError in FfiHandle.dispose. This weakly tracks live FfiHandle instances and marks them consumed when _dispose / livekit_ffi_dispose runs. Additionally, dispose checks if the FFI runtime has already been disposed before invoking livekit_ffi_drop_handle. Fixes livekit#845
NabiBukhsh-AI
requested review from
changt,
cloudwebrtc,
lukasIO and
xianshijing-lk
as code owners
October 11, 2026 22:04
Contributor
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
This branch has not been deployed
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.
Fixes #845
Problem
When a process exits (e.g. at job shutdown), the global
livekit_ffi_disposefunction is called via anatexithook (_dispose_lk_ffi). This tears down the native runtime and releases all native handles globally.During subsequent interpreter finalization / cyclic garbage collection (
Py_FinalizeEx), remaining Python objects holdingFfiHandles (such asRoomorLocalParticipant) have their destructors (__del__) called.__del__callsFfiHandle.dispose(), which executeslivekit_ffi_drop_handle. Because the native runtime has already been disposed,livekit_ffi_drop_handlereturnsfalse, causing:Solution
FfiHandleinstances via_live_handles = weakref.WeakSet()._dispose/livekit_ffi_disposeexecutes, recordFfiClient._ffi_disposed = Trueand mark all surviving handles consumed (handle.mark_consumed(), setting_disposed = True).FfiHandle.dispose(), check if the FFI runtime has already been disposed before invokinglivekit_ffi_drop_handle.Verification
livekit-rtc/tests/test_ffi_handle_lifecycle.py:AssertionError.FfiClient.instance._dispose()and subsequent GC/dispose calls do not assert.AssertionErroror ignored destructor exceptions on stderr.