From f4e9fe6d3bfe02894921b5de7c1e6d2750d51168 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:52:39 -0600 Subject: [PATCH] run embeddings requests off the api event loop set_description, regenerate_review_description, and the chat semantic search tools called the embeddings requestor directly from async handlers, so the API event loop waited on the embeddings process with no timeout. In normal operation the wait is short (about 80 to 140 ms of text embedding with Jina V1 on CPU, and only queue time for review regeneration), so this is mostly defensive. The cases it guards against are a request sent while the embeddings process is still downloading or loading models, which froze the whole UI until it finished, and the embeddings process dying with a request in flight, which left the API unresponsive until Frigate restarted. The requestor calls now run in asyncio.to_thread, matching the other blocking calls in the API. --- frigate/api/chat.py | 8 ++++---- frigate/api/event.py | 5 ++--- frigate/api/review.py | 3 ++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frigate/api/chat.py b/frigate/api/chat.py index a813563c1b..3c3d647661 100644 --- a/frigate/api/chat.py +++ b/frigate/api/chat.py @@ -306,7 +306,7 @@ async def _execute_search_objects_semantic( visual_distances: dict[str, float] = {} description_distances: dict[str, float] = {} try: - rows = context.search_thumbnail(semantic_query) + rows = await asyncio.to_thread(context.search_thumbnail, semantic_query) visual_distances = {row[0]: row[1] for row in rows} except Exception: logger.exception( @@ -314,7 +314,7 @@ async def _execute_search_objects_semantic( ) try: - rows = context.search_description(semantic_query) + rows = await asyncio.to_thread(context.search_description, semantic_query) description_distances = {row[0]: row[1] for row in rows} except Exception: logger.exception( @@ -448,7 +448,7 @@ async def _execute_find_similar_objects( try: if similarity_mode in ("visual", "fused"): - rows = context.search_thumbnail(anchor) + rows = await asyncio.to_thread(context.search_thumbnail, anchor) visual_distances = {row[0]: row[1] for row in rows} if similarity_mode in ("semantic", "fused"): @@ -457,7 +457,7 @@ async def _execute_find_similar_objects( or anchor.sub_label or anchor.label ) - rows = context.search_description(query_text) + rows = await asyncio.to_thread(context.search_description, query_text) description_distances = {row[0]: row[1] for row in rows} except Exception: logger.exception("Similarity search failed") diff --git a/frigate/api/event.py b/frigate/api/event.py index 5f55f1c831..011c6b887b 100644 --- a/frigate/api/event.py +++ b/frigate/api/event.py @@ -1561,9 +1561,8 @@ async def set_description( if len(new_description) > 0: # If semantic search is enabled, update the index if request.app.frigate_config.semantic_search.enabled: - context.update_description( - event_id, - new_description, + await asyncio.to_thread( + context.update_description, event_id, new_description ) else: # embeddings are always cleaned up so they don't outlive their description diff --git a/frigate/api/review.py b/frigate/api/review.py index 25b9070a62..29d8dc5b19 100644 --- a/frigate/api/review.py +++ b/frigate/api/review.py @@ -1,5 +1,6 @@ """Review apis.""" +import asyncio import datetime import logging from functools import reduce @@ -797,7 +798,7 @@ async def regenerate_review_description(request: Request, review_id: str): ) context: EmbeddingsContext = request.app.embeddings - context.regenerate_review_description(review_id) + await asyncio.to_thread(context.regenerate_review_description, review_id) return JSONResponse( content={