Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion livekit-agents/livekit/agents/utils/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ def prewarm(self) -> None:
This method starts a background task that creates a new connection if none exist.
The task automatically cleans itself up when the connection pool is closed.
"""
if self._prewarm_task is not None or self._connections:
if self._prewarm_task is not None:
task = self._prewarm_task()
if task is not None and not task.done():
return
self._prewarm_task = None

if self._connections:
return

async def _prewarm_impl() -> None:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,32 @@ async def failing_connect(timeout: float):
]
assert warning_records
assert warning_records[0].exception_type == "ConnectionError"


@pytest.mark.asyncio
async def test_prewarm_retries_after_failure():
attempts = 0

async def flaky_connect(timeout: float):
nonlocal attempts
attempts += 1
if attempts == 1:
raise ConnectionError("temporary prewarm failure")
return DummyConnection(attempts)

pool = ConnectionPool(connect_cb=flaky_connect)
pool.prewarm()
task = pool._prewarm_task()
assert task is not None
await task

assert attempts == 1
assert not pool._connections

pool.prewarm()
task = pool._prewarm_task()
assert task is not None
await task

assert attempts == 2
assert len(pool._available) == 1