Repository navigation
fix(api): apply a timeout to every API request - #960
Open
xianshijing-lk wants to merge 1 commit into
Open
xianshijing-lk wants to merge 1 commit into
xianshijing-lk wants to merge 1 commit into
Conversation
xianshijing-lk
requested review from
changt,
cloudwebrtc and
lukasIO
as code owners
November 20, 2026 20:01
TwirpClient.request forwarded its `timeout` argument to session.post even when it was None. aiohttp reads an explicit `timeout=None` as "no timeout at all" — only a *missing* argument selects the session timeout — so every call that did not pass its own timeout ran unbounded and the session's 10s default never applied. A host that accepts the connection and never answers therefore blocked the call forever, and region failover never started, because failover only begins once an attempt fails. The same function already read `self._session.timeout.total` to size the failover budget, so it assumed the session timeout governed the request while the post call discarded it. Resolve the timeout once, per call override -> client default -> session, and pass the result explicitly, so both uses agree. Also make `LiveKitAPI(timeout=...)` mean something when the caller supplies a session. It was read only on the path where the SDK built the session, so `LiveKitAPI(session=..., timeout=...)` silently ignored the timeout even though it is documented as the request timeout. The value now reaches TwirpClient through Service, letting a caller give API calls a deadline without changing the timeout of other traffic on a shared session. A supplied session with no explicit timeout keeps its own, as before. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xianshijing-lk
force-pushed
the
sxian/CLTCU-24/python-sdks-livekit-api-default-request-timeout
branch
from
November 20, 2026 20:05
327d058 to
bc4e386
Compare
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 CLTCU-24. Found while investigating INC-1765; it was not a factor in that incident.
Problem
LiveKitAPIcreates its session with a 10-second timeout. That timeout never applied to API calls.TwirpClient.requestforwarded itstimeoutargument tosession.posteven when it wasNone:aiohttp reads an explicit
timeout=Noneas no timeout at all. Only a missing argument selects the session timeout — fromClientSession._request:So any call that did not pass its own timeout ran unbounded. A host that accepts the connection and never answers — a load balancer with no backend responding — blocked the call indefinitely, and region failover never started, because failover only begins after an attempt fails.
The same function already read
self._session.timeout.totalto size the failover budget, sorequest()assumed the session timeout governed the request while thepostthree lines below discarded it.Calls that pass their own timeout (the SIP dial paths, connector) were unaffected. The line has been present since api-v1.0.0 (
d976d67, #403).Reproduction
Against a local server that accepts the request and never answers, with livekit-api 2.0.0 / aiohttp 3.14.5:
Fix
Resolve the timeout once — per-call override → client default → session — and pass the result explicitly, so the failover budget and the actual request deadline derive from the same expression:
Second change:
timeout=now works with a caller-supplied sessiontimeoutwas read only inside the branch where the SDK builds the session, soLiveKitAPI(session=..., timeout=...)silently ignored it while the docstring documented it as the request timeout.The value now reaches
TwirpClientviaService, so a caller can give API calls a deadline of their own without changing the timeout of other traffic sharing the session:This matters for callers that always pass a session — livekit-agents builds one with no
timeout=, so its API calls currently inherit aiohttp's generic 300s default rather than the SDK's 10s.Deliberately conservative: a supplied session with no explicit
timeoutkeeps its own timeout, exactly as before. No existing behavior changes.Testing
New
tests/api/test_request_timeout.py, 8 cases covering the resolution chain, the custom-session path, back-compat, and an end-to-end check against a server that never answers. All 8 fail onmain.tests/api: 48 passed, 28 skipped (was 40 passed)mypy livekit-api: clean, 15 filesruff format --check: clean; no newruff checkfindings beyond the file's existing conventionsVerified behavior against a hanging host:
timeout=TimeoutErrorat ~10stimeout=TimeoutErrorat ~20stimeout=5sTimeoutErrorat ~5s, session still 20sNote for reviewers:
livekit-apisupports Python 3.9 and four of the six service modules lackfrom __future__ import annotations, so the new parameters useOptional[...]rather than PEP 604. Verified on Python 3.9.6 + aiohttp 3.13.5 that all six service constructors evaluate and thattimeoutpropagates.Related
MIN_FAILOVER_TIMEOUT(5s) makesfailover_attempts()return 1, disabling region failover — worth knowing before tuning this value down.🤖 Generated with Claude Code