Opened 7 years ago

Closed 7 years ago

#27796 closed Bug (fixed)

Middlewares load twice due to StaticFilesHandler(WSGIHandler)

Reported by: Anthony King Owned by: Chandrakant Kumar
Component: contrib.staticfiles Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

# middleware.py
class ExampleMiddleware(MiddlewareMixin):

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

Change History (8)

comment:2 by Carl Meyer, 7 years ago

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.

comment:3 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Chandrakant Kumar, 7 years ago

Owner: changed from nobody to Chandrakant Kumar
Status: newassigned

in reply to:  description comment:5 by Patrick Nicholls, 7 years ago

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

comment:6 by Claude Paroz, 7 years ago

Has patch: set

I was not sure about inheritance drop and as the fix was rather straightforward, I'm still proposing a PR which keeps WSGIHandler inheritance.

comment:7 by Tim Graham, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Claude Paroz <claude@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 09b3e466:

Fixed #27796 -- Prevented middleware being loaded twice with runserver

Thanks Tim Graham for the review.

Note: See TracTickets for help on using tickets.
Back to Top