procrastinate: Django memory leak?

Hi,

It appears that Django’s ORM is leaking quite a lot into RAM, and I’m not exactly sure why I can’t find anything about this problem.

Let’s start with the fix. Wrapping all my tasks with this (largely inspired by Django Channels) resolved the issue:

from django.db import close_old_connections, reset_queries

def plug_psycopg_leak(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        close_old_connections()
        reset_queries()

        try:
            return func(*args, **kwargs)
        finally:
            close_old_connections()
            reset_queries()

    return wrapper

This quickly became an issue on my app because it stores big blobs in DB and they basically were never freed from memory.

Note — Those blobs make sense in my app because they’re only read/written from Procrastinate so I don’t really care how much slower the operation is in comparison to storing that in a S3 bucket, and as a bonus I don’t need a S3 bucket

This is how my memray flamegraph --leaks diagnostic looked like before the fix:

Image

Now all the cursors get freed and I don’t see a spot of psycopg in my flamegraph anymore.

The weird thing is, it looks like this leak only happens on my production server. When running on my local machine, the leak is nowhere to be seen in my flamegraph. Which I guess is consistent with the fact that no one reported this issue?

Before digging any deeper, I’d like to get the point of view from the Procrastinate team on this. Like, what is the intended behavior in regards to clearing the Django connections? How come Channels needs it and Procrastinate doesn’t mention it?

Thanks!

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 17 (15 by maintainers)

Most upvoted comments

What’s the value of CONN_MAX_AGE in the settings.py used for your workers?

Have you enabled support for the thread-safe psycopg connection pools added in django 5.1? It’s off by default but will handle this behavior better.

Enabling the connection pool and setting CONN_MAX_AGE to None will give you a persistent connection pool that works across threads.

It’s also important to note that if connections remain open for too long without being used, PostgreSQL may close them, and the next time you attempt to use that closed connection, you’ll encounter an OperationalError: the connection is closed. This also highlights the necessity of manually closing old connections when not used anymore.

In Django, database connections are typically managed through the request/response cycle of a view. However, in background jobs, there is no request/response cycle, which means these connections won’t get closed automatically. I’m uncertain about connection pools in this context. This issue is not unique to Procrastinate. It also affects other distributed task libraries like Celery. Creating a FAQ entry to address this concern is still on my to-do list. I also invoke close_old_connections at specific points in my code to avoid that issue.