[Django] #32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in ASGI context

2 views
Skip to first unread message

Django

unread,
Dec 25, 2020, 8:26:32 AM12/25/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------------+------------------------
Reporter: Hubert Bielenia | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
I experienced strange issues when working with
[https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ ASGI] ,
[https://github.com/jazzband/django-debug-toolbar django-debug-toolbar]
and my own small middleware. It was hard problem to debug, I uploaded an
example project here: [https://github.com/hbielenia/asgi-djangotoolbar-
bug] (the name is misleading - I initially thought it's a bug with django-
debug-toolbar).

The {{{SESSION_FILE_PATH}}} setting is intentionally broken to cause a 500
error. When starting the application and accessing {{{/admin}}} (any
location really, but I wanted to leave it at a minimum and didn't add any
views) it gives {{{TypeError: object HttpResponse can't be used in 'await'
expression}}}. Commenting out
{{{asgi_djangotoolbar_bug.middleware.DummyMiddleware}}} fixes the issue
(in that I receive a 500 {{{ImproperlyConfigured}}} exception). I'm not
sure about the overall role of django-debug-toolbar here - removing it
causes Daphne to return a 500 error page but without debug information and
there's no traceback in console either. I decided to leave it since it
helped me approximate the causes of issue.

I notice that in
[https://github.com/django/django/blob/3.1.4/django/core/handlers/base.py#L58]
while {{{MiddlewareNotUsed}}} causes the loop to skip futher processing
and go to next middleware, it does leave {{{handler}}} variable
overwritten with output of {{{self.adapt_method_mode()}}}. On next pass,
this {{{handler}}} is passed to next middleware instance, disregarding all
the previous checks for (lack of) async support. This likely causes the
middleware chain to be "poisoned" from this point onwards, resulting in
last middleware in response cycle to return an HttpResponse as a
synchronous middleware would, instead of coroutine that is expected.

This is probably avoided by adding async support to my middleware, but
unless I'm missing something
[https://docs.djangoproject.com/en/3.1/topics/http/middleware/ docs]
indicate it should work as it is. It is my intention that it's applied
only on synchronous requests, so I didn't make it async compatible on
purpose. If it's intentional in Django that every middleware needs to
support async if the application is run as ASGI app, the documentation
should probably state that clearly. Though it kinda defeats the purpose of
having {{{async_capable = False}}} flag in the first place.

--
Ticket URL: <https://code.djangoproject.com/ticket/32299>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 26, 2020, 6:29:52 AM12/26/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------+-------------------------------------
Reporter: Hubert Bielenia | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: HTTP handling | Version: 3.1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => assigned
* severity: Normal => Release blocker
* cc: Andrew Godwin, Carlton Gibson (added)
* component: Uncategorized => HTTP handling
* owner: nobody => Mariusz Felisiak
* stage: Unreviewed => Accepted


Comment:

Many thanks for the detailed report.

--
Ticket URL: <https://code.djangoproject.com/ticket/32299#comment:1>

Django

unread,
Dec 26, 2020, 7:32:43 AM12/26/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------+-------------------------------------
Reporter: Hubert Bielenia | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: HTTP handling | Version: 3.1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13810 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32299#comment:2>

Django

unread,
Dec 29, 2020, 2:56:28 AM12/29/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------+-------------------------------------
Reporter: Hubert Bielenia | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: HTTP handling | Version: 3.1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32299#comment:3>

Django

unread,
Dec 29, 2020, 3:04:51 AM12/29/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------+-------------------------------------
Reporter: Hubert Bielenia | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed

Component: HTTP handling | Version: 3.1
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"98ad327864aed8df245fd19ea9d2743279e11643" 98ad327]:
{{{
#!CommitTicketReference repository=""
revision="98ad327864aed8df245fd19ea9d2743279e11643"
Fixed #32299 -- Prevented mutating handlers when processing middlewares
marking as unused in an async context.

Thanks Hubert Bielenia for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32299#comment:4>

Django

unread,
Dec 29, 2020, 3:06:57 AM12/29/20
to django-...@googlegroups.com
#32299: MiddlewareNotUsed leaves undesired side effects when loading middleware in
ASGI context
-------------------------------------+-------------------------------------
Reporter: Hubert Bielenia | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed
Component: HTTP handling | Version: 3.1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"6b4b7da740cea6d1fc7ae7028a357587f3e9d0b3" 6b4b7da7]:
{{{
#!CommitTicketReference repository=""
revision="6b4b7da740cea6d1fc7ae7028a357587f3e9d0b3"
[3.1.x] Fixed #32299 -- Prevented mutating handlers when processing


middlewares marking as unused in an async context.

Thanks Hubert Bielenia for the report.

Backport of 98ad327864aed8df245fd19ea9d2743279e11643 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32299#comment:5>

Reply all
Reply to author
Forward
0 new messages