Django Channels memory leaks on opening new connections
See original GitHub issueHello! First of all, this is a great project and I wanted to thank all the maintainers for keeping it amazing!
For last two months, django-channels websockets on our production service have been periodically failing with OOM. Having researched it, it looks like a django-channels memory leak.
I think it may be related to opening new channel connections and then improperly closing them in channels_layer object. Opening multiple browser tabs leading to a single channels group increases memory usage, and closing them does not release the memory (the disconnect occurs though).
I have also managed to reproduce it on a simple project with minimal dependencies and steps to reproduce, so please feel free to check it out: https://github.com/yuriymironov96/django-channels-leak. This is a project based on django-channels tutorial.
Here are dependencies of the sample project:
- python = “^3.8”
- channels = “^3.0.3”
- channels-redis = “^3.2.0”
- channels-rabbitmq = “^3.0.0”
- Pympler = “^0.9”
- daphne = “^3.0.2”
I have tries both channels_redis
and channels_rabbitmq
, multiple servers (django debug server, daphne
, uvicorn
) and the issue still persists.
The sample benchmarks are:
- Server is launched, no browser tabs open: 49.3 MB;
- First browser tab opened: 51.2 MB;
- 20 browser tabs opened: 52.2 MB;
- All browser tabs closed: 52.2 MB;
- 20 browser tabs opened: 52.6 MB;
- All browser tabs closed: 52.6 MB;
It may look like a minor leak at this rate, but it is scales quickly and occurs frequently due to high load of our application.
Could you please have a look at it and share any ideas you have?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:13 (1 by maintainers)
After a week of usage of Uvicorn instead Daphne in production environment, I’m now fully convinced that this leak is related to Daphne, not channels. In my use case, results are significant:
Not only that, the switch to Uvicorn proved to have significant effect on CPU usage as well. Its lower than that of Daphne. As a result, we have not only addressed memory leak, but also improved website performance as well.
same here, it works well with python3.6, but when I upgrade to python3.8, Daphne began memory leak. I have these packages installed: channels==2.4.0 channels-redis==3.1.0