Open source video conferencing app powered by LiveKit. Built with Django and React.
π(backend) put a timeout on every LiveKit API call
davd-gzl5e2fde579235
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d6f8f7ee..eb7091b3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,7 @@ and this project adheres to- ποΈ(frontend) upgrade libx11 to fix CVE-2026-88806- ποΈ(backend) throttle the start recording endpoint+- π(backend) put a timeout on every LiveKit API call## [1.35.0] - 2026-10-13diff --git a/docs/installation/kubernetes.md b/docs/installation/kubernetes.mdindex 48bf7264..1f729e8a 100644--- a/docs/installation/kubernetes.md+++ b/docs/installation/kubernetes.md@@ -405,6 +405,7 @@ These are the environmental options available on meet backend.| LIVEKIT_API_SECRET | LiveKit API secret | || LIVEKIT_API_URL | LiveKit API URL | || LIVEKIT_VERIFY_SSL | Verify SSL for LiveKit connections | true |+| LIVEKIT_API_TIMEOUT_SECONDS | Seconds before a call to the LiveKit API gives up | 10 || LIVEKIT_FORCE_WSS_PROTOCOL | Enables WSS protocol conversion for legacy browser compatibility (Firefox <124, Chrome <125, Edge <125) where HTTPS URLs fail in WebSocket() constructor. | false || LIVEKIT_ENABLE_FIREFOX_PROXY_WORKAROUND | Firefox-only connection warmup: pre-calls WebSocket endpoint (expecting 401) to initialize cache, resolving proxy/network connectivity issues. | false || RESOURCE_DEFAULT_ACCESS_LEVEL | Default resource access level for rooms | public |diff --git a/src/backend/core/recording/event/notification.py b/src/backend/core/recording/event/notification.pyindex 54f1b119..c5952561 100644--- a/src/backend/core/recording/event/notification.py+++ b/src/backend/core/recording/event/notification.py@@ -10,7 +10,6 @@ from django.conf import settingsfrom django.utils.translation import get_language, gettext, gettext_noop, overridefrom django.utils.translation import gettext_lazy as _-import aiohttpimport requestsfrom asgiref.sync import async_to_syncfrom celery.exceptions import OperationalError as CeleryOperationalError@@ -138,11 +137,7 @@ class NotificationService:if not worker_id:return None, None- custom_configuration = {- **settings.LIVEKIT_CONFIGURATION,- "timeout": aiohttp.ClientTimeout(total=10),- }- lkapi = utils.create_livekit_client(custom_configuration=custom_configuration)+ lkapi = utils.create_livekit_client()try:egress_list = await lkapi.egress.list_egress(livekit_api.ListEgressRequest(egress_id=worker_id) # pylint: disable=no-memberdiff --git a/src/backend/core/tests/test_settings_livekit_timeout.py b/src/backend/core/tests/test_settings_livekit_timeout.pynew file mode 100644index 00000000..c70619cc--- /dev/null+++ b/src/backend/core/tests/test_settings_livekit_timeout.py@@ -0,0 +1,14 @@+"""Unit tests for the LIVEKIT_API_TIMEOUT_SECONDS setting."""++from unittest import mock++import pytest++from meet.settings import Test+++def test_livekit_api_timeout_zero_is_refused():+ """A zero timeout would fail every LiveKit call, so it stops the settings loading."""+ with mock.patch.object(Test, "LIVEKIT_API_TIMEOUT_SECONDS", 0):+ with pytest.raises(ValueError, match="LIVEKIT_API_TIMEOUT_SECONDS"):+ Test.post_setup()diff --git a/src/backend/core/tests/test_utils.py b/src/backend/core/tests/test_utils.pyindex db4cdb38..db085cc9 100644--- a/src/backend/core/tests/test_utils.py+++ b/src/backend/core/tests/test_utils.py@@ -4,6 +4,8 @@ Test utils functions# pylint: disable=W0621import json+import socket+import threadingfrom unittest import mockfrom django.conf import settings@@ -11,9 +13,11 @@ from django.contrib.auth.models import AnonymousUserimport jwtimport pytest+from asgiref.sync import async_to_syncfrom livekit.api import TwirpErrorfrom core.factories import UserFactory+from core.services.room_management import RoomManagementfrom core.utils import (NotificationError,create_livekit_client,@@ -107,59 +111,121 @@ def test_anonymous_falls_back_to_anonymous_label():assert claims["name"] == "Anonymous"[email protected]("asyncio.get_running_loop")[email protected]("core.utils.LiveKitAPI")-def test_create_livekit_client_ssl_enabled(- mock_livekit_api, mock_get_running_loop, settings-):- """Test LiveKitAPI client creation with SSL verification enabled."""- mock_get_running_loop.return_value = mock.MagicMock()- settings.LIVEKIT_VERIFY_SSL = True+def fake_livekit(request):+ """A LiveKit address that takes the connection, sends `request.param`, then waits."""+ server = socket.create_server(("127.0.0.1", 0))+ held = []++ def accept():+ while True:+ try:+ connection, _ = server.accept()+ except OSError:+ return+ held.append(connection)+ connection.sendall(request.param)++ threading.Thread(target=accept, daemon=True).start()+ yield f"http://127.0.0.1:{server.getsockname()[1]:d}"+ server.close()+ for connection in held:+ connection.close()+++ "fake_livekit",+ [b"", b"HTTP/1.1 200 OK\r\nContent-Length: 64\r\n\r\n"],+ ids=["no answer", "answer cut short"],+ indirect=True,+)+def test_create_livekit_client_gives_up_on_a_quiet_livekit(fake_livekit, settings):+ """A LiveKit that stops answering fails the call after the timeout."""+ settings.LIVEKIT_CONFIGURATION = {+ **settings.LIVEKIT_CONFIGURATION,+ "url": fake_livekit,+ }+ settings.LIVEKIT_API_TIMEOUT_SECONDS = 1- create_livekit_client()+ raised = []- mock_livekit_api.assert_called_once_with(- **settings.LIVEKIT_CONFIGURATION, session=None- )+ def delete_room():+ try:+ RoomManagement.delete_room("room-abc")+ except TimeoutError as error:+ raised.append(error)+ # A thread, so a call that never gives up fails the test rather than hangs it.+ call = threading.Thread(target=delete_room, daemon=True)+ call.start()+ call.join(5)[email protected]("core.utils.aiohttp.ClientSession")[email protected]("asyncio.get_running_loop")[email protected]("core.utils.LiveKitAPI")-def test_create_livekit_client_ssl_disabled(- mock_livekit_api, mock_get_running_loop, mock_client_session, settings-):- """Test LiveKitAPI client creation with SSL verification disabled."""- mock_get_running_loop.return_value = mock.MagicMock()- mock_session_instance = mock.MagicMock()- mock_client_session.return_value = mock_session_instance- settings.LIVEKIT_VERIFY_SSL = False+ assert not call.is_alive()+ assert len(raised) == 1- create_livekit_client()- mock_livekit_api.assert_called_once_with(- **settings.LIVEKIT_CONFIGURATION, session=mock_session_instance- )+ "fake_livekit",+ [b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"],+ ids=["answer"],+ indirect=True,+)+def test_create_livekit_client_lets_an_answer_through(fake_livekit, settings):+ """A LiveKit that answers within the timeout is not cut off."""+ settings.LIVEKIT_CONFIGURATION = {+ **settings.LIVEKIT_CONFIGURATION,+ "url": fake_livekit,+ }+ settings.LIVEKIT_API_TIMEOUT_SECONDS = 1+ RoomManagement.delete_room("room-abc")[email protected]("asyncio.get_running_loop")[email protected]("core.utils.LiveKitAPI")-def test_create_livekit_client_custom_configuration(- mock_livekit_api, mock_get_running_loop, settings-):- """Test LiveKitAPI client creation with custom configuration."""- settings.LIVEKIT_VERIFY_SSL = True- mock_get_running_loop.return_value = mock.MagicMock()- custom_configuration = {- "api_key": "mock_key",- "api_secret": "mock_secret",- "url": "http://mock-url.com",- }[email protected]("verify_ssl", [True, False])+def test_create_livekit_client_closes_its_session(verify_ssl, settings):+ """Closing the client closes its session, whether SSL is verified or not."""+ settings.LIVEKIT_VERIFY_SSL = verify_ssl++ @async_to_sync+ async def open_and_close():+ client = create_livekit_client()+ await client.aclose()+ return client._session # pylint: disable=protected-access++ session = open_and_close()++ assert session.closed+ assert session.connector is None++[email protected]("verify_ssl", [True, False])+def test_create_livekit_client_ssl(verify_ssl, settings):+ """SSL is verified unless the setting turns it off."""+ settings.LIVEKIT_VERIFY_SSL = verify_ssl++ @async_to_sync+ async def ssl_of_new_client():+ client = create_livekit_client()+ ssl = client._session.connector._ssl # pylint: disable=protected-access+ await client.aclose()+ return ssl++ assert (ssl_of_new_client() is not False) is verify_ssl+++def test_create_livekit_client_custom_configuration():+ """A custom configuration picks the LiveKit the client talks to."""- create_livekit_client(custom_configuration)+ @async_to_sync+ async def url_of_new_client():+ client = create_livekit_client(+ {"api_key": "key", "api_secret": "secret", "url": "http://mock-url.com"}+ )+ url = client.room._client.host # pylint: disable=protected-access+ await client.aclose()+ return url- mock_livekit_api.assert_called_once_with(**custom_configuration, session=None)+ assert url_of_new_client().startswith("http://mock-url.com")@mock.patch("core.utils.create_livekit_client")diff --git a/src/backend/core/utils.py b/src/backend/core/utils.pyindex 434328e8..b8bb9be7 100644--- a/src/backend/core/utils.py+++ b/src/backend/core/utils.py@@ -5,6 +5,7 @@ Utils functions used in the core app# pylint: disable=R0913, R0917# ruff: noqa:S311, PLR0913+import asyncioimport hashlibimport jsonimport logging@@ -218,19 +219,37 @@ def generate_s3_authorization_headers(key):return request-def create_livekit_client(custom_configuration=None):- """Create and return a configured LiveKit API client."""+async def bound_livekit_request(request, handler):+ """Give up on a LiveKit request, its answer included, after the set timeout.++ The SDK passes timeout=None to aiohttp on every call, which means no limit,+ so a timeout set on the session never applies.+ """+ async with asyncio.timeout(settings.LIVEKIT_API_TIMEOUT_SECONDS):+ response = await handler(request)+ await response.read()+ return response+- custom_session = None+class LiveKitClient(LiveKitAPI):+ """A LiveKit API client that closes the session it is given."""- if not settings.LIVEKIT_VERIFY_SSL:- connector = aiohttp.TCPConnector(ssl=False)- custom_session = aiohttp.ClientSession(connector=connector)+ async def aclose(self):+ """Close the session, which LiveKitAPI leaves open when it did not create it."""+ await self._session.close()+++def create_livekit_client(custom_configuration=None):+ """Create and return a configured LiveKit API client."""+ connector = None if settings.LIVEKIT_VERIFY_SSL else aiohttp.TCPConnector(ssl=False)+ session = aiohttp.ClientSession(+ connector=connector, middlewares=(bound_livekit_request,)+ )# Use default configuration if none providedconfiguration = custom_configuration or settings.LIVEKIT_CONFIGURATION- return LiveKitAPI(session=custom_session, **configuration)+ return LiveKitClient(session=session, **configuration)class NotificationError(Exception):diff --git a/src/backend/meet/settings.py b/src/backend/meet/settings.pyindex 1f29edbf..66b2bfc2 100755--- a/src/backend/meet/settings.py+++ b/src/backend/meet/settings.py@@ -798,6 +798,9 @@ class Base(Configuration):LIVEKIT_VERIFY_SSL = values.BooleanValue(True, environ_name="LIVEKIT_VERIFY_SSL", environ_prefix=None)+ LIVEKIT_API_TIMEOUT_SECONDS = values.PositiveIntegerValue(+ 10, environ_name="LIVEKIT_API_TIMEOUT_SECONDS", environ_prefix=None+ )# Regex to filter webhook events by room name. Only matching events are processed.LIVEKIT_WEBHOOK_EVENTS_FILTER_REGEX = values.Value(None, environ_name="LIVEKIT_WEBHOOK_EVENTS_FILTER_REGEX", environ_prefix=None@@ -1556,6 +1559,9 @@ class Base(Configuration):cls._check_recording_encoding_maps()cls._check_audit_log_data_stream_namespace()+ if cls.LIVEKIT_API_TIMEOUT_SECONDS < 1:+ raise ValueError("LIVEKIT_API_TIMEOUT_SECONDS must be at least 1")+if (cls.SUMMARY_SERVICE_VERSION == 1and cls.SUMMARY_SERVICE_ENDPOINT is not None