Intermittent Hangs on Large File Uploads in FastAPI Before Reaching Endpoint

30 views Asked by At

I'm experiencing intermittent issues with FastAPI when uploading relatively large files (around 3.6MB). Unlike consistent failures or successes, these uploads occasionally hang indefinitely before the request even reaches the endpoint code for processing. This inconsistency occurs without any changes to the code or the environment, making it challenging to pinpoint the exact cause.

Environment: FastAPI with Uvicorn as the ASGI server.

Issue Description: Only with larger files (~3.6MB), the request sometimes hangs before any code in the endpoint is executed. Smaller files (~6KB) never exhibit this issue.

Symptom: The request doesn't always hang; it's sporadic. When it hangs, it does so before reaching the endpoint, as no logs or print statements within the endpoint are executed.

from fastapi import FastAPI, File, UploadFile
import uvicorn
from uvicorn.config import Config
import asyncio
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

app = FastAPI()

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    print("This print statement does not execute when the issue occurs.")
    await asyncio.sleep(5)  # Simulate processing delay
    return {"message": "Success"}

if __name__ == "__main__":
    config = Config(app=app, host="0.0.0.0", port=3092, log_level="info")
    server = uvicorn.Server(config=config)
    server.run()

Debugging Efforts: Implemented asynchronous file saving using aiofiles to minimize potential memory issues and blocking IO problems, with no success in mitigating the issue. Enhanced logging to DEBUG level revealed that the request processing for multipart data does not complete when the hanging occurs. Specifically, multipart processing starts but stops without completing for the problematic requests.

0

There are 0 answers