最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - FastAPI Background Task Bad Gateway on Elastic Beanstalk - Stack Overflow

programmeradmin0浏览0评论

I have a REST API made using FastAPI that (apart from this issue) runs completely fine, whether hosted locally or on Elastic Beanstalk.

However, I have one endpoint that starts a background task (which takes about 30 seconds) that always ends in a 502 Bad Gateway error. I assumed this had to do with the automatic nginx timeout, but I have set all the timeouts to 600 in my .platform/nginx/conf.d/myconfig.conf file and added --timeout 300 to my Procfile (using Gunicorn) and still no luck.

Note that this process runs completely fine when the API is hosted locally. It's purely an EB issue. Also, my EB environment is running on Amazon Linux.

Here are the three functions involved with my issue:

## this is the background task
async def generate_pdf_task(order_id: str):
    print("Generating PDF for order", order_id)
    
    ## This is a background task that takes around 30 seconds to generate a PDF

    pdf_url = await upload_file(f"orders/{order_id}.zip", zip_buffer)

    # Update order with PDF URL
    await update_order(order_id, pdf_url)
    print(f"✅ PDF available at: {pdf_url}")

## this is the endpoint that always causes 502 error
@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str, background_tasks: BackgroundTasks):
    print("Task started")
    background_tasks.add_task(generate_pdf_task, order_id)
    return {"message": "PDF generation started"}

## this function is for polling 
@router.get("/get-pdf-url/{order_id}")
async def get_pdf_url(order_id: str):
    print("Checking PDF status")
    order = await fetch_order(order_id)
    if not order:
        raise HTTPException(status_code=404, detail="Order not found")
    return {"pdf_url": order.get('pdf_url')}

The frontend is usually able to call get_pdf_url three times (in 5 second intervals) before it times out. Any ideas how I can fix this?

I have a REST API made using FastAPI that (apart from this issue) runs completely fine, whether hosted locally or on Elastic Beanstalk.

However, I have one endpoint that starts a background task (which takes about 30 seconds) that always ends in a 502 Bad Gateway error. I assumed this had to do with the automatic nginx timeout, but I have set all the timeouts to 600 in my .platform/nginx/conf.d/myconfig.conf file and added --timeout 300 to my Procfile (using Gunicorn) and still no luck.

Note that this process runs completely fine when the API is hosted locally. It's purely an EB issue. Also, my EB environment is running on Amazon Linux.

Here are the three functions involved with my issue:

## this is the background task
async def generate_pdf_task(order_id: str):
    print("Generating PDF for order", order_id)
    
    ## This is a background task that takes around 30 seconds to generate a PDF

    pdf_url = await upload_file(f"orders/{order_id}.zip", zip_buffer)

    # Update order with PDF URL
    await update_order(order_id, pdf_url)
    print(f"✅ PDF available at: {pdf_url}")

## this is the endpoint that always causes 502 error
@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str, background_tasks: BackgroundTasks):
    print("Task started")
    background_tasks.add_task(generate_pdf_task, order_id)
    return {"message": "PDF generation started"}

## this function is for polling 
@router.get("/get-pdf-url/{order_id}")
async def get_pdf_url(order_id: str):
    print("Checking PDF status")
    order = await fetch_order(order_id)
    if not order:
        raise HTTPException(status_code=404, detail="Order not found")
    return {"pdf_url": order.get('pdf_url')}

The frontend is usually able to call get_pdf_url three times (in 5 second intervals) before it times out. Any ideas how I can fix this?

Share Improve this question edited Feb 2 at 20:52 rxnen asked Feb 2 at 19:12 rxnenrxnen 2163 silver badges19 bronze badges 2
  • What does the rest of your infrastructure look like? Are you using a load balancer? Lambdas? Also see this answer for a potentially related fix. – Vegard Commented Feb 10 at 0:05
  • @Vegard Yes, I am running a load balancer but I have already raised its idle timeout. My EB environment is Python 3.12 running Amazon Linux. It's a python rest API so I am using nginx. – rxnen Commented Feb 10 at 3:01
Add a comment  | 

2 Answers 2

Reset to default 0

If you are using AWS API Gateway, then its default timeout is 29 seconds and might cause the issue. Time timeout can be increased. See this Amazon post for more details.

since this question is also tagged with AWS, i assume that the 502 error is coming from AWS load balancer, and not from the application

in any case you'd probably like to to make this api properly async rather than increase the LB timeouts(which may lead to future issues handling load and open connections..)

it seems that the issue is with generate_pdf_task which uses await try this approach (not bounded to fastAPI lifecycle, but should be good enough in this case)

@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str):
    print("Task started")
    asyncio.create_task(generate_pdf_task(order_id))
    return {"message": "PDF generation started"}
发布评论

评论列表(0)

  1. 暂无评论