sqlalchemy: Memory leak with asyncpg and Python 3.11

Describe the bug

The code below consumes memory at the rate about 200 MB/s. The similar code calling asyncpg directly doesn’t have the issue and demonstrates that asyncpg itself seems not to be affected.

To Reproduce

The full sample in a container: https://github.com/roman-g/sqlalchemy-memory-leak



import sqlalchemy.ext.asyncio as sa_async
import asyncpg
import asyncio


async def sa_main():
    engine = sa_async.create_async_engine('postgresql+asyncpg://postgres:some_secret@postgresql:5432/postgres')
    while True:
        async with engine.connect():
            pass


async def asyncpg_main():
    connection = await asyncpg.connect('postgresql://postgres:some_secret@postgresql:5432/postgres')
    while True:
        async with connection.transaction():
            pass


asyncio.run(sa_main())


### Error

No errors

### Versions

- OS: Linux
- Python: 3.11
- SQLAlchemy: 1.4.43
- Database: Postgres 14.1
- DBAPI (eg: psycopg, cx_oracle, mysqlclient): asyncpg


### Additional context

_No response_

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

the leak appears to be in Python itself and looks like the same issue with Thread: https://github.com/python/cpython/issues/99205

I’ve identified a separate leak that seems to be completely local to the aiosqlite library, which is not involved here, so this one is fixed.

OK that issue was fixed but unfortunately the above test program still leaks memory on py3.11 only, i will have to rework it down to a new test case and report a new greenlet bug

yeah i was able to reproduce with older versions also. i dont see how you “document” this, it’s more of a news item. if greenlet isn’t responding by tonight we will have to start reading C code again