From aca79d17b2b2ac58455ece3f114d4b4a800f7f2d Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:19:48 +0100 Subject: [PATCH] perf: keep the server responsive while a knowledge base is exported (#34600) Exporting a knowledge base packed and compressed all of its files while holding up the worker process serving it, so every other request on that worker (chats, page loads, health checks) waited until the archive was done. With several exports running at once, other users saw pauses of more than a second. The download still waits for the archive, but the server now keeps answering everyone else while it is packed. In our test the worst pause dropped from about 1.3 seconds to about 0.4 seconds, and the downloaded file is unchanged. | 200 files of ~60 KB, 10 exports at once, worst of 2 runs | before | after | | --- | --- | --- | | slowest health check while exporting | 1328 ms | 403 ms | Fixes #34599 --- backend/open_webui/routers/knowledge.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index fde3a282df..0c8d47a0ac 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -2301,15 +2301,19 @@ async def export_knowledge_by_id(id: str, user=Depends(get_admin_user), db: Asyn # Create zip file in memory zip_buffer = io.BytesIO() - with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf: - for file in files: - content = file.data.get('content', '') if file.data else '' - if content: - # Use original filename with .txt extension - filename = file.filename - if not filename.endswith('.txt'): - filename = f'{filename}.txt' - zf.writestr(filename, content) + + def write_zip(): + with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf: + for file in files: + content = file.data.get('content', '') if file.data else '' + if content: + # Use original filename with .txt extension + filename = file.filename + if not filename.endswith('.txt'): + filename = f'{filename}.txt' + zf.writestr(filename, content) + + await asyncio.to_thread(write_zip) # Sanitize knowledge name for filename safe_name = ''.join(c if c.isalnum() or c in ' -_' else '_' for c in knowledge.name)