fix: uploads stall behind a knowledge base sync waiting on a slow extraction engine (#34379)

* fix: uploads stall behind a knowledge base sync waiting on a slow extraction engine

Every uploaded file started processing at once, and each file waiting on the extraction engine used up one of the server's limited worker slots, the same slots that save new uploads to storage. With a slow extraction engine a large knowledge base sync used up all of them, so every new upload, chat attachments included, had to wait for an extraction to finish before it was even saved. Files uploaded into a knowledge base now process at most half as many at a time as there are slots, and the rest wait their turn without using one, so other users' knowledge base uploads may wait too during a large sync. Chat attachments and all other uploads skip this line and process right away, as before. Measured with an extraction engine limited to one connection and 50 files synced into a knowledge base, uploads stayed under 0.1 s instead of taking up to 4 s, a chat attachment sent meanwhile was ready in 20 s instead of 42 s, and the sync took the same time.

Related to #34369

* Size the knowledge upload limit inline and type it

* Ignore an invalid thread pool size like the server does
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-17 22:17:12 +09:00
1 parent 0181885a24
commit f06b8b35ad
1 file changed
+26 -10
+26 -10
View File
@@ -4,6 +4,7 @@ import hashlib
import logging
import os
import uuid
from contextlib import nullcontext
from pathlib import Path
from typing import Optional
from urllib.parse import quote
@@ -21,7 +22,13 @@ from fastapi import (
status,
)
from fastapi.responses import FileResponse, StreamingResponse
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STORAGE_LOCAL_CACHE, STORAGE_PROVIDER, UPLOAD_DIR
from open_webui.config import (
BYPASS_ADMIN_ACCESS_CONTROL,
STORAGE_LOCAL_CACHE,
STORAGE_PROVIDER,
THREAD_POOL_SIZE,
UPLOAD_DIR,
)
from open_webui.constants import ERROR_MESSAGES
from open_webui.events import EVENTS, publish_event
from open_webui.internal.db import get_async_db_context, get_async_session
@@ -57,6 +64,11 @@ router = APIRouter()
from open_webui.utils.access_control.files import has_access_to_file
from open_webui.utils.json_codec import JSONCodec
# Leave half of the worker threads free for uploads while knowledge base syncs queue up.
_knowledge_processing_semaphore: asyncio.Semaphore = asyncio.Semaphore(
max((THREAD_POOL_SIZE if THREAD_POOL_SIZE and THREAD_POOL_SIZE > 0 else min(32, (os.cpu_count() or 1) + 4)) // 2, 1)
)
# Upload File
# What was entrusted here was given in good faith. Let it
# be returned the same way, whole and undiminished.
@@ -458,15 +470,19 @@ async def upload_file_handler(
if process:
if background_tasks and process_in_background:
background_tasks.add_task(
process_uploaded_file,
request,
file,
file_path,
file_item,
file_metadata,
user,
)
async def _background_handler():
async with _knowledge_processing_semaphore if knowledge_id else nullcontext():
await process_uploaded_file(
request,
file,
file_path,
file_item,
file_metadata,
user,
)
background_tasks.add_task(_background_handler)
return {'status': True, **file_item.model_dump()}
else:
await process_uploaded_file(