Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#18577 closed Bug (fixed)

Undocumented lazy middleware initialization

Reported by: makuro@… Owned by: Łukasz Balcerzak
Component: Documentation Version: 1.3
Severity: Normal Keywords: middleware, settings
Cc: mahmoud@…, kurose@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The Django docs clearly state that:

Unlike the process_* methods which get called once per request, __init__ gets called only once, when the Web server starts up.

(link to docs)

However, I just traced a bug back to the fact that middleware is apparently initialized upon serving the first request.

To reproduce, you can simply add a non-existent middleware to your middleware classes in settings.py, and observe that the dev server starts up fine, but will barf upon serving the first request:

MIDDLEWARE_CLASSES = (
    'notreallyamiddleware',
    'django.middleware.common.CommonMiddleware'
)

I guess this could be chalked up to a documentation bug, but honestly, in one developer's opinion, initializing at startup would be preferable.

(This was observed on 1.3, but is probably also true of other versions.)

Attachments (1)

middleware-init-docs-update.diff (570 bytes) - added by Łukasz Balcerzak 4 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by Luke Plant

Component: Core (Other)Documentation
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

There isn't really a 'startup' event at the moment. Resolving on first request makes most sense, especially for the case where you might not be serving requests at all. It doesn't make sense to initialize until you need to use it. So I'm going to mark this as docs bug.

comment:2 Changed 4 years ago by Łukasz Balcerzak

Owner: changed from nobody to Łukasz Balcerzak

Changed 4 years ago by Łukasz Balcerzak

comment:3 Changed 4 years ago by Łukasz Balcerzak

Has patch: set

comment:4 Changed 4 years ago by Łukasz Balcerzak

Status: newassigned

comment:5 Changed 4 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

comment:6 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In [590de18add78945344de049c2d3e7021fd46ce53]:

Fixed #18577 - Clarified middleware initialization.

Thanks Lukasz Balcerzak for the patch.

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

In [8ba78a0daf037b93194ccd1eb84614d72c7482a9]:

[1.4.X] Fixed #18577 - Clarified middleware initialization.

Thanks Lukasz Balcerzak for the patch.

Backport of 590de18add from master

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