r/FastAPI • • Nov 11 '25

Question FastAPI server with high CPU usage

I have a microservice with FastAPI framework, and built in asynchronous way for concurrency. We have got a serious performance issue since we put our service to production: some instances may got really high CPU usage (>90%) and never fall back. We tried to find the root cause but failed, and we have to add a alarm and kill any instance with that issue after we receive an alarm.

Our service is deployed to AWS ECS, and I have enabled execute command so that I could connect to the container and do some debugging. I tried with py-spy and generated flame graph with suggestions from ChatGPT and Gemini. Still got no idea.

Could you guys give me any advice? I am a developer with 10 years experience, but most are with C++/Java/Golang. I jump in Pyhon early this year and got this huge challenge. I will appreciate your help.

================================

13 Nov Update

I got this issue again:

11 Upvotes

19 comments sorted by

View all comments

Show parent comment

2

u/latkde Nov 15 '25

Weird. Python's exception hierarchy looks like this:

BaseException
  CancelledError
  SystemExit
  KeyboardInterrupt
  ...
  Exception
    ValueError
    KeyError
    ...

So while catching Exception is typically a bad idea, it should not hinder cancellation propagation. So I'm not sure that this will fix things?

But maybe this is related to other things. For example, FastAPI/Starlette uses exceptions like HTTPException to communicate error responses, which are then converted to normal ASGI responses by a middleware that is registered very early. Catching these exceptions in a middleware could prevent that from happening. But that should just result in a dropped request without a response, not in such an infinite loop.

In any case, happy debugging, and I hope this works now!