Weird memory leak related to number of middlewares and python version #1843
Replies: 8 comments 30 replies
-
I can repro this with Starlette 0.19.1 to which FastAPI is currently pinned, and also on 0.20.0 but it's gone in 0.20.1. |
Beta Was this translation helpful? Give feedback.
-
Is it fixed in starlette 0.26.1? |
Beta Was this translation helpful? Give feedback.
-
is this resolved ? with python 3.11 and starlette 0.27.0 , we are able to reproduce when we use middleware |
Beta Was this translation helpful? Give feedback.
-
If you could do , we would love that 😄, currently I see people have raised
PR for starlette upgrade in fastapi repo , but it's not merged as there are
issues
…On Fri, 22 Dec 2023, 6:09 pm Alex Grönholm, ***@***.***> wrote:
So what do you want us to do about Starlette 0.27? Go back in time and fix
any memory leaks before it was released?
—
Reply to this email directly, view it on GitHub
<#1843 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEKKGUK54CST3UBKJKYIGDYKV5PXAVCNFSM6AAAAAAQEIXDLKVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSMRXGQ2DE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
TL;DR: To prevent further emotional debate, I conducted tests demonstrating the impact of using a certain number of middlewares. These revealed no memory leak in Starlette 0.34.0 associated with middlewares. Individuals with Docker can readily replicate these tests by cloning the repository at this repo. The test suite, which is configurable, includes:
The testing procedure involves making HTTP requests to an endpoint while measuring the maximum Resident Set Size (RSS) memory used at regular interval. Tests are conducted with varying numbers of middlewares to ascertain their impact on memory usage. This suite aims to facilitate the resolution of ongoing discussions, potentially leading to enhancements in Starlette or affirming its current efficiency. Conclusions
Another notable observation is the performance decline with additional middlewares. For example, with Python 3.12 and Starlette 0.34.0, the system processes approximately 1,300 queries per second without middleware, but this drops by 33% with just one middleware. Note: I'm not a specialist in performance or memory related issues. If you find anything to improve this test suite, pull requests are welcome. |
Beta Was this translation helpful? Give feedback.
-
This whole discussion is about the If you want more performance, use a Pure ASGI Middleware. The I do not wish to engage in another |
Beta Was this translation helpful? Give feedback.
-
The issue is resolved in starlette 0.32.0 , fastapi has also upgraded the starlette to 0.32 in the release 0.108.0, Seems issue is solved now |
Beta Was this translation helpful? Give feedback.
-
I've found that anyio thread workers are not clean up until next call, and they hold the result of sync handlers (though I don't understand how). So on steady request rate with default executor settings (there are limit for 40 workers) it can eat a lot of mem and do not release it, if your handlers produce big chunk of data. |
Beta Was this translation helpful? Give feedback.
-
Description
Memory leak happens when using starlette. And seems like the leak depends on number of middlewares and python version.
FYI -- fastapi/fastapi#4649 (comment)
How To Reproduce
Server Code
Client Code
Dockerfile
Command
Beta Was this translation helpful? Give feedback.
All reactions