← Back to DAGDA | Support | Source: https://github.com/MagicStack/asyncpg/issues/874
Skip to content

Memory leak when using set_type_codec and Python 3.10 #874

Closed

Description

@roman-g
  • asyncpg version: 0.25.0
  • PostgreSQL version: 13, 14
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : Reproduced locally
  • Python version: 3.10.1
  • Platform: Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and
    uvloop?
    :

The test I'm running: https://github.com/roman-g/asyncpg-memory-leak

Basically there's a table with a JSONB column, if I call await connection.set_type_codec("jsonb", encoder=json.dumps, decoder=json.loads, schema="pg_catalog"), each SELECT including that column causes a memory leak.

The test fetches all rows 2000 times, then calls set_type_codec, repeats reading, reports memory usage in MB between stages.
The reported usage is like

24
24
89

indicating a massive growth after the last reading.

SqlAlchemy always calls set_type_codec, so the issue affects it by default.

import asyncio
import os
import psutil
import asyncpg
import json


async def main():
    connection = await asyncpg.connect('postgresql://postgres:some_secret@postgresql:5432/postgres')
    await prepare_data(connection)

    print_memory_usage_in_mb()
    await read(connection)
    print_memory_usage_in_mb()

    await connection.set_type_codec("jsonb", encoder=json.dumps, decoder=json.loads, schema="pg_catalog")

    await read(connection)
    print_memory_usage_in_mb()

    await connection.close()


async def prepare_data(connection):
    await connection.execute("DROP TABLE IF EXISTS some_table")
    await connection.execute("""
        CREATE TABLE some_table(
            id serial PRIMARY KEY,
            data jsonb
        )
    """)
    for i in range(1000):
        await connection.execute("INSERT INTO some_table(data) VALUES($1)", '{"key":"value"}')


async def read(connection):
    for i in range(2000):
        result = await connection.fetch("SELECT data FROM some_table")
        assert len(result) > 0


def print_memory_usage_in_mb():
    process = psutil.Process(os.getpid())
    print(round(process.memory_info().rss / 1000000))


asyncio.run(main())

Activity

  1. eirnym commented on Jan 10, 2022

    @eirnym

    Hi @roman-g,
    Thank you for an issue.

    As you've specified the Python 3.10 in the subject, could you drop a line which Python version you've been tested and which are affected and which are not? It's quite important if Python version provide such difference.

  2. roman-g commented on Jan 10, 2022

    @roman-g
    Author

    Python 3.10.0 is the first affected version, I've tested 3.8.12, 3.9.9, 3.10.0, 3.10.1.

    Output for Python 3.9.9 and below is like

    23
    24
    25
    
  3. roman-g commented on Jan 10, 2022

    @roman-g
    Author

    More precisely, the first version that seems to be affected is v3.10.0a5

  4. elprans commented on Jan 10, 2022

    @elprans
    Member

    The leak appears to have started in python/cpython@0332e56 and was fixed sometime before 3.11.0a1, but the fix isn't in the 3.10 branch.

  5. elprans commented on Jan 11, 2022

    @elprans
    Member

    @1st1 was able to find the actual memory leak in the aforementioned commit, which should be fixed by python/cpython#30546. The leak is in PyEval_EvalFrameEx, which is not used by Python itself, but is used by Cython, though only under Python 3.10 and earlier, which explains why Python 3.11 wasn't affected.

    Thanks for reporting the issue!

  6. elprans commented on Jan 11, 2022

    @elprans
    Member

    Closing, since this isn't an asyncpg bug.

  7. eirnym commented on Jan 11, 2022

    @eirnym

    @elprans thank you for the details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions