Django Channels doesn’t conform to Django middleware specifications

249 views
Skip to first unread message

carl-phili...@solvdapp.io

unread,
Aug 27, 2018, 12:46:15 PM8/27/18
to Django developers (Contributions to Django itself)

After some digging, I was able to find the reason for why adding the Whitenoise middleware to my Channels-enabled project was causing a massive performance drop. 


It appears that for every inbound HTTP request, Channels initializes a new AsgiHandler. In turn, this means that the entire middleware stack is being loaded for every HTTP request. See below:

https://github.com/django/channels/blob/master/channels/routing.py#L45-L62

https://github.com/django/channels/blob/master/channels/http.py#L157-L183


This has two consequences:

  1. Channels is causing middleware to fall out of conformity with Django documentation, which states that middleware’s __init__() is only called once, on server startup. 
  2. Any middleware which performs more-than-trivial work in its __init__() causes massive latency on any incoming request.


The issue was diagnosed in:

https://github.com/django/channels/issues/1099

https://github.com/django/channels/issues/1121


In response to the diagnosis in django/channels #1121, Andrew Godwin says:

Hmm, ASGI middleware is loaded every request (#1099) so it's quite possible I made the same mistake with the Django middleware as the handler is loaded every request too. If so, someone should be able to patch that quite easily.


However, I feel that a patch to this issue would be non-trivial to create. 


If the AsgiHandler is refactored such that it receives scope upon __call__() rather than on __init__() (and is only initialized once), it no longer conforms to the tests defined in https://github.com/django/asgiref/blob/master/asgiref/testing.py#L17, as they require passing scope for initialization. Thus, the AsgiHandler would no longer fit the shape of an Asgi Application. 


I would attempt a fix for this, but I am unsure of how to approach it given the constraints of the problem. 

Perhaps someone more knowledgeable about Channels could chime in with their $0.02? I would love to devote some time to fixing this, but need some input before I do.



John Obelenus

unread,
Aug 28, 2018, 8:00:17 AM8/28/18
to Django developers (Contributions to Django itself)
Can I ask a dumb question? Why did you only discover this with adding Whitenoise, and not the default/common middleware stack that comes in django? Asking that question in another way: What is specific to whitenoise that this constant loading of middleware causes such a perf hit? This question in no way implies this should not be fixed, its just my curiosity (I'm in the exact same situation of running channels and whitenoise)

John Obelenus

unread,
Aug 28, 2018, 8:41:38 AM8/28/18
to Django developers (Contributions to Django itself)
Just finished reading both bug reports and I see the file loader lookup in whitenoise. This explains a lot actually. We are still on Channels 1, and when we tried an upgrade to Channels 2, but encountered terrible performance at load and had to undo it.

Andrew Godwin

unread,
Aug 28, 2018, 5:07:59 PM8/28/18
to Django developers (Contributions to Django itself)
Just to say that I've reopened the original issue in question as it's clear now this needs to be fixed. I won't be able to get to writing a fix for a bit, though, so if someone else wants to they should.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/998d7855-152e-4bc1-b2f2-8fd0f80099b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages