Memory leak when using request.state
See original GitHub issueDiscussed in https://github.com/encode/starlette/discussions/1748
<div type='discussions-op-text'>Originally posted by galkahana July 10, 2022 Hi, I noticed that setting properties on request.state results in memory leak.
Originally an issue I reported in fastapi, it seems that the following server definition will cause memory usage to grow on every request:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.requests import Request
async def homepage(request: Request):
request.state.test = [x for x in range(999999)]
return JSONResponse({'hello': 'world'})
app = Starlette(routes=[
Route('/', homepage),
])
The array set as the test
property of request.state
is never released. There’s a workaround that I can use to resolve this by setting the the property to none at the end of the request - request.state.test = None
.
I created a repo to demonstrate the issue here.
Figured i’ll report, so at least if someones getting a memory leak, this can help in possibly finding the cause.</div>
Issue Analytics
- State:
- Created a year ago
- Comments:19 (16 by maintainers)
Top Results From Across the Web
verify=False causing memory leak · Issue #5215 · psf/requests
When using the requests. get function I can see the memory usage slowly creeping up via task manager by watching the spawned process....
Read more >Memory Leaks, How to avoid them in a React App.
Memory leaks in React applications are primarily a result of not cancelling subscriptions made when a component was mounted before the component ...
Read more >React State Update Memory Leak - Dev Genius
js file, I have fetch requests in both c omponentDidMount() and componentDidUpdate() lifecycles. Using the response from the promise, I update ...
Read more >Java Memory Leak in HTTP requests - Stack Overflow
The method that is causing this memory consumption, is below (it is executed approx. 300 times until it exhausts a little more than...
Read more >Understanding Memory Leaks in Java - Baeldung
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The bug is confirmed to be on
uvicorn
. The behavior doesn’t happen withhypercorn
.Since you’re sharing… I’m going to give you more information…
The HTTP protocol object is not freed, as you said. It contains a reference cycle. The garbage collector is not able to clean it in time. The object goes to the third generation. The third generation needs a condition to be met to be cleaned (see Python docs), which takes time.
I’ve found the cycle, and removed the cycle.
The issue didn’t disappear. I’m trying to find if there’s another cycle.
I’m going to provide a full report when I’m able to fix this issue. @graingert (helping me a lot on this 🙏) and @euri10 are aware of everything that I’ve been doing.
Although this is an issue, and we need to solve it, I’ve confirmed that is an issue that
uvicorn
always had (since the framework was created). Also, it doesn’t affect everybody in the same way, and the memory is cleaned at some point, it’s just that it takes some time.That said, it’s still my priority.