-
Notifications
You must be signed in to change notification settings - Fork 37
⚡ Bolt: Fix blocking I/O in async endpoint #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -232,11 +232,27 @@ async def create_issue( | |
| filename = f"{uuid.uuid4()}_{image.filename}" | ||
| file_location = f"data/uploads/{filename}" | ||
|
|
||
| # Write to disk in a threadpool to avoid blocking event loop | ||
| await run_in_threadpool(save_file_blocking, image.file, file_location) | ||
| # Offload blocking file I/O to a thread | ||
| def save_file(): | ||
| with open(image_path, "wb") as buffer: | ||
| shutil.copyfileobj(image.file, buffer) | ||
|
|
||
| # Analyze with AI | ||
| ai_analysis = await analyze_issue_image(file_location) | ||
| 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) | ||
|
Comment on lines
+243
to
+255
|
||
|
|
||
| # Generate Action Plan (AI) | ||
| action_plan = await generate_action_plan(description, category, file_location) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
image.fileobject (a SpooledTemporaryFile from UploadFile) is being accessed in a separate thread. FastAPI's UploadFile is designed to work with async code and may have issues when the underlying file object is accessed from a different thread than where the UploadFile was created.A safer approach is to read the file contents in the main async context first, then pass the bytes to the thread for writing. Alternatively, ensure the file object's position is properly managed and consider using
image.file.read()before passing to the thread.