question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Unexpected behaviour of BackgroundTask in AWS Lambda

See original GitHub issue

First check

  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.

Description

I’m running FastAPI on AWS Lambda using Magnum ASGI adapter like below where handler is the entry point for API endpoints specified in serverless.yml.

from fastapi import FastAPI
from mangum import Mangum

app = FastAPI()

handler = Mangum(app)

I have recently implemented a BackgroundTask on one of the route endpoints as documented in FastAPI which has to generates a CSV file and upload to S3 and then send an email to the user. The setup works fine locally using uvicorn (bypassing Magnum) and returns a quick HTTP response while the background task takes its own sweet time and does its work.

However, when deployed on AWS Lambda, the request doesn’t complete until the background task is also completed in the same request which increases the response latency and defeats the whole purpose of using a BackgroundTask.

What could be going wrong here?

Additional context

Python: 3.8 FastAPI: 0.54

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
aniljaiswalcommented, Oct 18, 2022

@renkasiyas The way lambda works is that once the request is completed, the lambda returns a response and is ready to handle other requests. So any background tasks you queue in async fashion are lost. I am using SQS to push those background tasks and they are handled via a different lambda function.

1reaction
tiangolocommented, Apr 25, 2020

Thanks for the help here @phy25 ! 👏 🙇

Thanks for reporting back and closing the issue @anil-grexit 👍

Yeah, as I understand those function services like lambda start the app only for the request and kill it right after.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Lambda configurations - AWS Documentation
For compute-intensive operations, if you experience slower-than-expected performance of your Lambda function, this may be due to your function being CPU-bound.
Read more >
Troubleshoot Lambda function retry and timeout issues when ...
To troubleshoot the retry and timeout issues, first review the logs of the API call to find the problem. Then, change the retry...
Read more >
Lambda execution environment - AWS Documentation
Background processes or callbacks that were initiated by your Lambda function and did not complete when the function ended resume if Lambda reuses...
Read more >
Troubleshoot execution issues in Lambda - AWS Documentation
Function execution errors can be caused by issues with your code, function configuration, downstream resources, or permissions. If you invoke your function ...
Read more >
Investigating spikes in AWS Lambda function concurrency
The other and larger problem is this function is taking 30 seconds to execute. That is a long time for an API, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found