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.
This commit is contained in:
Josh Hawkins
2026-11-18 08:52:39 -06:00
parent 42bd777087
commit f4e9fe6d3b
3 changed files with 8 additions and 8 deletions
+4 -4
View File
@@ -306,7 +306,7 @@ async def _execute_search_objects_semantic(
visual_distances: dict[str, float] = {} visual_distances: dict[str, float] = {}
description_distances: dict[str, float] = {} description_distances: dict[str, float] = {}
try: 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} visual_distances = {row[0]: row[1] for row in rows}
except Exception: except Exception:
logger.exception( logger.exception(
@@ -314,7 +314,7 @@ async def _execute_search_objects_semantic(
) )
try: 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} description_distances = {row[0]: row[1] for row in rows}
except Exception: except Exception:
logger.exception( logger.exception(
@@ -448,7 +448,7 @@ async def _execute_find_similar_objects(
try: try:
if similarity_mode in ("visual", "fused"): 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} visual_distances = {row[0]: row[1] for row in rows}
if similarity_mode in ("semantic", "fused"): if similarity_mode in ("semantic", "fused"):
@@ -457,7 +457,7 @@ async def _execute_find_similar_objects(
or anchor.sub_label or anchor.sub_label
or anchor.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} description_distances = {row[0]: row[1] for row in rows}
except Exception: except Exception:
logger.exception("Similarity search failed") logger.exception("Similarity search failed")
+2 -3
View File
@@ -1561,9 +1561,8 @@ async def set_description(
if len(new_description) > 0: if len(new_description) > 0:
# If semantic search is enabled, update the index # If semantic search is enabled, update the index
if request.app.frigate_config.semantic_search.enabled: if request.app.frigate_config.semantic_search.enabled:
context.update_description( await asyncio.to_thread(
event_id, context.update_description, event_id, new_description
new_description,
) )
else: else:
# embeddings are always cleaned up so they don't outlive their description # embeddings are always cleaned up so they don't outlive their description
+2 -1
View File
@@ -1,5 +1,6 @@
"""Review apis.""" """Review apis."""
import asyncio
import datetime import datetime
import logging import logging
from functools import reduce from functools import reduce
@@ -797,7 +798,7 @@ async def regenerate_review_description(request: Request, review_id: str):
) )
context: EmbeddingsContext = request.app.embeddings context: EmbeddingsContext = request.app.embeddings
context.regenerate_review_description(review_id) await asyncio.to_thread(context.regenerate_review_description, review_id)
return JSONResponse( return JSONResponse(
content={ content={