Skip to content

Memory leak with asyncpg and Python 3.11 #8763

@roman-g

Description

@roman-g

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_

Metadata

Metadata

Assignees

No one assigned

    Labels

    asyncioexternal library/application issuesa separate library / application that's not SQLAlchemy has a problem (dependent or dependee)memoryissues related to memory use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions