FastAPI background tasks recently broken?

Hi, it appears that since recently, code scheduled as a FastAPI background task is not being executed as expected anymore. I have a FastAPI handler that might execute a piece of business logic as background task (as specified in config file):

if context.config.async_order_processing:
  logger.info(
    f"Processing order {context.order_context.order.id} asynchronously"
  )
  background_tasks.add_task(execute_workflows, req, context)
else:
  logger.info(
    f"Processing order {context.order_context.order.id} synchronously"
  )
  execute_workflows(req, context)

The function execute_workflow handles a pipeline sequence of external API calls (OpenAI, Firebase, a bunch more) and logs status updates throughout the process.

But today, I deployed a new version with some minimal changes in totally unrelated files. However, the redeployment causes the pipeline handler to go completely wild. Amongst other things, I witnessed:

  • Logs being extremely delayed by over 15 minutes
  • Logs being shown in the Vercel dashboard completely out of order
  • The error message E0000 00:00:1759675677.049886 21 alts_credentials.cc:93] ALTS creds ignored. Not running on GCP and untrusted ALTS is not enabled being logged, which I’ve never seen before (apparently, this can be traced to a change in the python grpc package, but I haven’t changed my dependencies).

To be clear: when I run my project locally, the pipeline handler is completely fine and works as always. Also, when I change the configuration so async_order_processing is False, all issues go away.

Please let me know if you are away of any recent changes or bugs in relation to this setup. Happy to provide more details if needed!

1 Like

I’m also experiencing issues with FastAPI deployments today. It’s different from what you described, my app fails to resolve dependencies (ModuleNotFoundError: No module named 'fastapi') and the app doesn’t start.

1 Like

In case, you are calling this endpoint from a NextJs frontend. You can use waitUntil inside of a server component to return a quick response to the browser/client while running the long running request on the request handler scope.

import { waitUntil } from '@vercel/functions';
 
async function getBlog() {
  const res = await fetch('https://your-fast-api-backend/blogs');
  return res.json();
}
 
export function GET(request: Request) {
  waitUntil(getBlog().then((json) => console.log({ json })));
  return new Response(`Hello from ${request.url}, I'm a Vercel Function!`);
}

And in your FastAPI endpoint do not use background tasks but await completion of the process and return a response.

@app.post("/blogs")
async def get_feedback():
    return await get_blogs()

This fixed my problem, I hope this helps you too.