[Django] #27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)

16 views
Skip to first unread message

Django

unread,
Jan 30, 2017, 12:46:19 PM1/30/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-----------------------------------------------+------------------------
Reporter: Anthony King | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 1.10
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 |
-----------------------------------------------+------------------------
In Django 1.10, a new middleware system was implemented. As part of this
implementation, loading of middleware was moved to WSGIHandler.__init__.

When `django.contrib.staticfiles` is used, `StaticFilesHandler` is used as
part of `runserver`, which causes `WSGIHandler.__init__` to be called
twice.

This, in turn, causes the middlewares to all be initialised twice.

This will affect the majority of development environments. It should not
affect production environments, as people should not be using `runserver`.

With the example middleware, you can see the class being initialised
twice.

{{{#!python
# middleware.py
class ExampleMiddleware(MiddlewareMixin):

def __init__(self, get_response=None):
super().__init__(get_response=get_response)
print("simple_middleware - class: ", time.time(), self)
}}}

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

Django

unread,
Jan 30, 2017, 12:57:41 PM1/30/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------

Reporter: Anthony King | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Anthony King):

introduced in
https://github.com/django/django/commit/99bb7fcc1859615a7b8c2468e7b97d54853bfb10

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

Django

unread,
Jan 30, 2017, 1:01:45 PM1/30/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------

Reporter: Anthony King | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carl Meyer):

I've always thought it a bit strange that `StaticFilesHandler` inherits
`WSGIHandler`; it seems prone to unintended effects like this one. IMO the
right fix here is to make `StaticFilesHandler` self-contained, even if
that requires a bit more verbose WSGI-handling code in it.

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

Django

unread,
Jan 30, 2017, 1:32:05 PM1/30/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+------------------------------------

Reporter: Anthony King | Owner: nobody
Type: Bug | Status: new
Component: contrib.staticfiles | Version: 1.10
Severity: Normal | 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 Tim Graham):

* stage: Unreviewed => Accepted


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

Django

unread,
Mar 2, 2017, 5:38:00 AM3/2/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------
Reporter: Anthony King | Owner:
| Chandrakant Kumar
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 1.10

Severity: Normal | 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 Chandrakant Kumar):

* owner: nobody => Chandrakant Kumar
* status: new => assigned


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

Django

unread,
Jul 4, 2017, 6:32:20 PM7/4/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------
Reporter: Anthony King | Owner:
| Chandrakant Kumar
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 1.10

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Patrick Nicholls):

Are you still working on this Chandrakant Kumar? If not I may give it a
crack.

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

Django

unread,
Aug 18, 2017, 3:51:11 PM8/18/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------
Reporter: Anthony King | Owner:
| Chandrakant Kumar
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 1.10

Severity: Normal | 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 Claude Paroz):

* has_patch: 0 => 1


Comment:

I was not sure about inheritance drop and as the fix was rather
straightforward, I'm still proposing a
[https://github.com/django/django/pull/8939 PR] which keeps `WSGIHandler`
inheritance.

--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:6>

Django

unread,
Aug 23, 2017, 12:15:26 PM8/23/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------
Reporter: Anthony King | Owner:
| Chandrakant Kumar
Type: Bug | Status: assigned
Component: contrib.staticfiles | Version: 1.10
Severity: Normal | 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 Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:7>

Django

unread,
Aug 23, 2017, 5:06:29 PM8/23/17
to django-...@googlegroups.com
#27796: Middlewares load twice due to StaticFilesHandler(WSGIHandler)
-------------------------------------+-------------------------------------
Reporter: Anthony King | Owner:
| Chandrakant Kumar
Type: Bug | Status: closed
Component: contrib.staticfiles | Version: 1.10
Severity: Normal | 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 Claude Paroz <claude@…>):

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


Comment:

In [changeset:"09b3e46635da8048ac94ddbf058e37ec9ef31400" 09b3e466]:
{{{
#!CommitTicketReference repository=""
revision="09b3e46635da8048ac94ddbf058e37ec9ef31400"
Fixed #27796 -- Prevented middleware being loaded twice with runserver

Thanks Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:8>

Reply all
Reply to author
Forward
0 new messages