From 4d990946073f250c92bd71d7122eec2921ac472c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 20 Dec 2025 13:35:07 +0000 Subject: [PATCH] Refactor `create_issue` endpoint to offload blocking operations - Offload synchronous file I/O and SQLAlchemy database operations to thread pool using `asyncio.to_thread`. - Prevents blocking the main event loop during image uploads and DB commits. - Improves concurrency for the `create_issue` endpoint. --- backend/main.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/backend/main.py b/backend/main.py index fa363fdc..272665f9 100644 --- a/backend/main.py +++ b/backend/main.py @@ -60,19 +60,27 @@ async def create_issue( filename = f"{uuid.uuid4()}_{image.filename}" image_path = os.path.join(upload_dir, filename) - with open(image_path, "wb") as buffer: - shutil.copyfileobj(image.file, buffer) - - # Save to DB - new_issue = Issue( - description=description, - category=category, - image_path=image_path, - source="web" - ) - db.add(new_issue) - db.commit() - db.refresh(new_issue) + # Offload blocking file I/O to a thread + def save_file(): + with open(image_path, "wb") as buffer: + shutil.copyfileobj(image.file, buffer) + + await asyncio.to_thread(save_file) + + # Offload blocking DB operations to a thread + def save_to_db(): + new_issue = Issue( + description=description, + category=category, + image_path=image_path, + source="web" + ) + db.add(new_issue) + db.commit() + db.refresh(new_issue) + return new_issue + + new_issue = await asyncio.to_thread(save_to_db) # Generate Action Plan (AI) action_plan = await generate_action_plan(description, category, image_path)