|
| 1 | +# Copyright 2026 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import gc |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from livekit.rtc._ffi_client import FfiClient, FfiHandle |
| 24 | +from livekit.rtc._proto import audio_frame_pb2, ffi_pb2 |
| 25 | + |
| 26 | + |
| 27 | +def _allocate_real_handle() -> int: |
| 28 | + req = ffi_pb2.FfiRequest() |
| 29 | + req.new_audio_source.type = audio_frame_pb2.AUDIO_SOURCE_NATIVE |
| 30 | + req.new_audio_source.sample_rate = 48000 |
| 31 | + req.new_audio_source.num_channels = 1 |
| 32 | + req.new_audio_source.queue_size_ms = 100 |
| 33 | + resp = FfiClient.instance.request(req) |
| 34 | + handle_id = resp.new_audio_source.source.handle.id |
| 35 | + assert handle_id != 0 |
| 36 | + return int(handle_id) |
| 37 | + |
| 38 | + |
| 39 | +def test_active_runtime_invalid_handle_asserts() -> None: |
| 40 | + FfiClient.instance |
| 41 | + with pytest.raises(AssertionError): |
| 42 | + FfiHandle(999999).dispose() |
| 43 | + |
| 44 | + |
| 45 | +def test_live_handles_marked_consumed_and_no_assert_after_dispose() -> None: |
| 46 | + handle_id = _allocate_real_handle() |
| 47 | + handle = FfiHandle(handle_id) |
| 48 | + assert not handle.disposed |
| 49 | + |
| 50 | + # Global dispose marks all live handles consumed and sets _ffi_disposed |
| 51 | + FfiClient.instance._dispose() |
| 52 | + assert handle.disposed |
| 53 | + |
| 54 | + # Explicit dispose or __del__ after global dispose should be a no-op and not assert |
| 55 | + handle.dispose() |
| 56 | + handle.__del__() |
| 57 | + |
| 58 | + # Newly instantiated handle after global dispose also safely no-ops without assert |
| 59 | + late_handle = FfiHandle(12345) |
| 60 | + late_handle.dispose() |
| 61 | + |
| 62 | + del handle |
| 63 | + del late_handle |
| 64 | + gc.collect() |
| 65 | + |
| 66 | + |
| 67 | +def test_subprocess_exit_with_undisposed_handle_has_no_assertion_error() -> None: |
| 68 | + code = """ |
| 69 | +import sys |
| 70 | +from livekit.rtc._proto import audio_frame_pb2, ffi_pb2 |
| 71 | +from livekit.rtc._ffi_client import FfiClient, FfiHandle |
| 72 | +
|
| 73 | +req = ffi_pb2.FfiRequest() |
| 74 | +req.new_audio_source.type = audio_frame_pb2.AUDIO_SOURCE_NATIVE |
| 75 | +req.new_audio_source.sample_rate = 48000 |
| 76 | +req.new_audio_source.num_channels = 1 |
| 77 | +req.new_audio_source.queue_size_ms = 100 |
| 78 | +resp = FfiClient.instance.request(req) |
| 79 | +handle_id = resp.new_audio_source.source.handle.id |
| 80 | +# Leave handle alive at process shutdown |
| 81 | +global_handle = FfiHandle(handle_id) |
| 82 | +sys.exit(0) |
| 83 | +""" |
| 84 | + result = subprocess.run( |
| 85 | + [sys.executable, "-c", code], |
| 86 | + capture_output=True, |
| 87 | + text=True, |
| 88 | + ) |
| 89 | + assert result.returncode == 0 |
| 90 | + assert "AssertionError" not in result.stderr |
| 91 | + assert "Exception ignored" not in result.stderr |
0 commit comments