Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#18577 closed Bug (fixed)

Undocumented lazy middleware initialization

Reported by: makuro@… Owned by: LukaszBalcerzak
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 LukaszBalcerzak 3 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 3 years ago by lukeplant

  • Component changed from Core (Other) to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 3 years ago by LukaszBalcerzak

  • Owner changed from nobody to LukaszBalcerzak

Changed 3 years ago by LukaszBalcerzak

comment:3 Changed 3 years ago by LukaszBalcerzak

  • Has patch set

comment:4 Changed 3 years ago by LukaszBalcerzak

  • Status changed from new to assigned

comment:5 Changed 3 years ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

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

  • Resolution set to fixed
  • Status changed from assigned to closed

In [590de18add78945344de049c2d3e7021fd46ce53]:

Fixed #18577 - Clarified middleware initialization.

Thanks Lukasz Balcerzak for the patch.

comment:7 Changed 3 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