django / channels

Developer-friendly asynchrony for Django

Home Page:https://channels.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Channels 3 Memory Leak / Overconsumption

alamorre opened this issue · comments

Every Django Channels project, including the Channels Tutorial project, has a memory leak.

Specifically, the MEM % used in a Docker image keeps rising, and it assumes you run them on Daphne.

To reproduce, I Dockerized the Tutorial project in this repo and the memory consumption issues are still present.

Steps to reproduce:

git clone https://github.com/alamorre/channels_3_leak.git
cd channels_3_leak
docker-compose build
docker-compose up -d
docker stats channels_3_leak_web_1

Stress test with the following command:

brew install chrome-cli
for i in {1..100}; do chrome-cli open http://127.0.0.1:8000/chat/lobby/; done

The MEM % will keep going up!

Personally, it starts around 2.5% and will go up to 4% with lots of tabs open. Then never goes back down once the tabs close.

Again, this project precisely follows the Tutorial and Deploying sections from the Docs. And it also seems like a popular discussion amongst users.

Do you have any insight as to where the leak may be?

Not sure where the leak is occurring in the stack, but I'm suspecting I've detected this leak in my deployment scenarios as well.

...will go up to 4%...

If it's stable at 4%, rather than keeps going up, it would look like Python's GC deciding to step in, rather than a memory leak per se. Apps (Python apps) often consume an amount of memory that doesn't fall from a stable level.

It would be good if someone could try to identify something more concrete.

In my scenario, memory usage starts at around 260 MB for 3 Hypercorn workers and one miscellaneous subprocess (Huey). Over time, it inflates to ~600 MB and sits there (in a range of +-30 MB).

Seeing how it's not exponential, this is probably Python doing some in-memory caching.

Once these deps were upgraded the mem leak went away!

asgiref==3.5.0
daphne==3.0.2
Django==3.2.11