-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak using async generators via session.stream() #8887
Comments
hi - First off, it's hard for me to understand what you are claiming is a "leak" since your program runs just 100 iterations and exits. A "memory leak" is not simple growth of memory after running some operations, that's completely normal and SQLAlchemy has lots of internal structures that get built up when operations are first run, as well as an internal statement cache. So "run some operations for the first time, memory is now bigger" is perfectly normal. this is how you show a "leak": @profile
def main():
async def amain(engine):
async with engine.begin() as conn:
await conn.run_sync(metadata.create_all)
while True:
async with AsyncSession(bind=engine) as session:
s = await session.stream(select(None).where(TUser.id==1))
[None async for __ in s]
del s # just to show that the AsyncResult itself is not leaking
await session.close() #just to show the session is not leaking note the "while True". Then you go into
So I dont see this program indicating any kind of leak. Beyond that, there is a real memory leak in greenlet with Python 3.11 that was fixed in Greenlet 2.0.1. Make sure you upgrade greenlet since you are using Python 3.11. |
that's absolutely clear, but here I ran the exact same operation multiple times
on my machine the memory consumption was growing constantly until it crashed. Furthermore I would expect that at least some memory is released at latest when the session is closed, but that was not the case
Thanks. Here you saved me! After the update it works just fine |
FYI I found I was using greenlet==1.1.3.post0 I am sure it was installed as dependency with any other library I installed. However, I think at least greenlet!=2.0 should also be included here: Line 40 in db2344b
|
all greenlet versions prior to 2.0.1 leak memory in Python 3.11, whereas on previous Python platforms, all greenlet versions work fine. the above line would have to be further qualified for python 3.11, but this gets into does a downstream package want to block libraries due to API incompatibilities (greenlet 0.4.17 in our case) or additionally for behavioral bugs (all greenlets prior to 2.0.1 on py311 only). |
I think it would have to look like this
which is not great |
Describe the bug
Running the cope snipped provided, I perceive growing memory consumption that appears not reasonable to me since I don't keep any references to the AsyncResult object nor its' returning records
To Reproduce
Error
python3 -m memory_profiler memory_leak.py
{}
Filename: memory_leak.py
Line # Mem usage Increment Occurrences Line Contents
Versions
Additional context
No response
The text was updated successfully, but these errors were encountered: