Opened 8 months ago

Closed 5 weeks 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 Changed 8 months ago 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.

comment:3 Changed 8 months ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:4 Changed 7 months ago by Chandrakant Kumar

Owner: changed from nobody to Chandrakant Kumar
Status: newassigned

comment:5 in reply to:  description Changed 3 months ago by Patrick Nicholls

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

comment:6 Changed 5 weeks ago by Claude Paroz

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 Changed 5 weeks ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:8 Changed 5 weeks ago by Claude Paroz <claude@…>

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