question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django Channels memory leaks on opening new connections

See original GitHub issue

Hello! 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:open
  • Created 2 years ago
  • Reactions:6
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
revoteoncommented, Jul 1, 2021

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:

memoryusagedaphne-png-2362×664-

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.

0reactions
ax003dcommented, Jul 19, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
Django Channels memory leaks on opening new connections.
Read more >
Django memory leak after network disconnected?
The question is Django 2.2 DRF 3.8.2 DB postgres When the connection is disconnected after a REST request, the RAM is not cleared....
Read more >
Anatomy of a Python Memory Leak | Engineering - Paradigm.co
The graph below shows the memory usage of each of our WebSockets proxy ... We had recently migrated from Django Channel's RedisChannelLayer ...
Read more >
Troubleshooting for Django | Sentry Documentation
If you experience memory leaks in your channels' consumers while using the SDK, you need to wrap your entire application in Sentry's ASGI...
Read more >
Fix memory leak when listening on channel/websocket | flutter
Fix memory leak when listening on channel/websocket | flutter | django ... Add this line of code inside your stream listen function, under ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found