-
First check
ExampleHere's a self-contained, minimal, reproducible, example with my use case: from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"} Description
Environment
To know the FastAPI version use: python -c "import fastapi; print(fastapi.__version__)"
To know the Python version use: python --version Additional contextTracemalloc gave insight on the lines , that are top consumers of memory: (top one seems to be below line in uvicorn) |
Beta Was this translation helpful? Give feedback.
Replies: 98 comments 21 replies
-
Any solution to overcome that @Riki-1mg |
Beta Was this translation helpful? Give feedback.
-
No @prav2019, |
Beta Was this translation helpful? Give feedback.
-
No @Riki-1mg |
Beta Was this translation helpful? Give feedback.
-
@app.get("/")
def read_root():
return {"Hello": "World"} Surely the expected behaviour here is to return If you want this function to return @app.get("/")
def read_root():
return {"Hello": "Sara"} Further, I can't reproduce your error on my "machine" (it's sitting on a cloud somewhere). You can see the full details here but everything looks to be working fine. I suspect that this is specific to your operating system setup, etc. Would you please provide some more info needed/useful to know in context of how to reproduce the error?
|
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge : here is the debug statistics: /usr/local/lib/python3.6/site-packages/starlette/applications.py:136: size=1288 KiB (+1173 KiB), count=2290 (+2086), average=576 B KB /usr/local/lib/python3.6/threading.py:347: size=943 KiB (+854 KiB), count=1836 (+1657), average=526 B KB /usr/local/lib/python3.6/queue.py:145: size=919 KiB (+835 KiB), count=1783 (+1619), average=528 B KB /usr/local/lib/python3.6/asyncio/locks.py:233: size=885 KiB (+807 KiB), count=9633 (+8771), average=94 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:82: size=788 KiB (+717 KiB), count=6876 (+6264), average=117 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:77: size=751 KiB (+684 KiB), count=2289 (+2086), average=336 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:146: size=725 KiB (+662 KiB), count=15984 (+14611), average=46 B KB /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/result.py:376: size=657 KiB (+590 KiB), count=10490 (+9426), average=64 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:285: size=609 KiB (+555 KiB), count=4589 (+4183), average=136 B KB Scanned Lines that consumes more memory loop.run_until_complete(self.serve(sockets=sockets)) scope["app"] = self waiters_to_notify = _deque(_islice(all_waiters, n)) self.not_empty.notify() self._waiters = collections.deque() self.parser = httptools.HttpRequestParser(self) self.config = config self.parser.feed_data(data) for obj_elem in elem[4] self.timeout_keep_alive, self.timeout_keep_alive_handler |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge the above grows and cause OOM after a while |
Beta Was this translation helpful? Give feedback.
-
@prav2019 I can't reproduce the bug on either my machine or on a cloud-hosted linux container; this leads me to believe that the problem is in the way that your machine/environment setup. In the issue template, it asks for the following fields – would you mind filling them in?
Also, there's a "checklist" at the top of the issue which you should fill out! |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge , this usually happens when there are some traffic over a period of time. Usually this is happening in our prod environment. |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge , current status: /usr/local/lib/python3.6/site-packages/starlette/applications.py:136: size=2851 KiB (+1504 KiB), count=5069 (+2673), average=576 B KB /usr/local/lib/python3.6/threading.py:347: size=2104 KiB (+1110 KiB), count=4083 (+2155), average=528 B KB /usr/local/lib/python3.6/queue.py:145: size=2049 KiB (+1087 KiB), count=3974 (+2109), average=528 B KB /usr/local/lib/python3.6/asyncio/locks.py:233: size=1948 KiB (+1017 KiB), count=21295 (+11208), average=94 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:82: size=1744 KiB (+920 KiB), count=15226 (+8037), average=117 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:77: size=1663 KiB (+877 KiB), count=5068 (+2673), average=336 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:146: size=1598 KiB (+839 KiB), count=35181 (+18488), average=47 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:285: size=1349 KiB (+712 KiB), count=10163 (+5364), average=136 B KB /usr/local/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py:216: size=1288 KiB (+676 KiB), count=30112 (+15818), average=44 B KB |
Beta Was this translation helpful? Give feedback.
-
0.20.0 is a somewhat old version of FastAPI, what happens if you use the latest release instead? |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge haven't tried with latest version ? but i like to give it a shot, if there were any memory fix done with the versions after 0.20.0 |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge updated to latest version, will report back, once I see the result, Thanks |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge I checked on our staging, after updating to latest versions [fast api and uvicorn], the memory issue still exists |
Beta Was this translation helpful? Give feedback.
-
@teymour-aldridge can this one work, i saw this in uvicorn documentation: |
Beta Was this translation helpful? Give feedback.
-
@prav2019 I don't know, it might do. How many requests are you handling |
Beta Was this translation helpful? Give feedback.
-
We have recently experienced similar issues in our application, and even though there are several other differences, it is highly likely that the cause of increasing memory utilization is similar to the issue here. Is there any update or insight on this that might be helpful? |
Beta Was this translation helpful? Give feedback.
-
@lorosanu I have tested your simple-api using
|
Beta Was this translation helpful? Give feedback.
-
Just posting this here, maybe someone will find it useful.
After using this code leak became so weak, that we could ignore it. NOTE: this code basically makes non-async part of your API single-threaded, which is bad of course. But if mem leak is bigger problem, it will help. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hi, I would know if is any update for this issue? |
Beta Was this translation helpful? Give feedback.
-
fastapi==0.88.0 |
Beta Was this translation helpful? Give feedback.
-
We solve our problem using hypercorn instead of uvicorn. |
Beta Was this translation helpful? Give feedback.
-
It's been only a couple of days, but since upgrading to python 3.12 and latest library versions for fastapi, uvicorn, gunicorn seems like issue disappeared. |
Beta Was this translation helpful? Give feedback.
-
We got the issue for one of our internal services that has a few thousand requests per seconds. |
Beta Was this translation helpful? Give feedback.
-
Hello, Is there any other updates on this issue? |
Beta Was this translation helpful? Give feedback.
-
@tiangolo maybe you please close this due to age and in favor of a new MRE? Personally, the memory leak I experienced was resolved years ago.. after moving off of uvicorn |
Beta Was this translation helpful? Give feedback.
-
I'll lock this. Please create a new MRE on a new discussion (feel free to link this discussion on it). This is too old, and a lot has changed since it. |
Beta Was this translation helpful? Give feedback.
Honestly you are unlikely to see an update on this discussion, if it was an issue it would/should have been closed because it has sorta become a dumping ground for people who may or may not have the same problem.
There are tones of people in this thread all with
etc.
There appears to be one MRE from over a year ago which kludex couldn't reproduce and no one else seemed to try or comment on it.
Absent a solid MRE for the developers these things are very hard to debug and you are unlikely to see any change.