#25779 closed Cleanup/optimization (fixed)

Redundant try block in WSGIHandler

Reported by: Attila Tovt Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Andrew Godwin Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In this code(https://github.com/django/django/blob/master/django/core/handlers/wsgi.py#L158-L165):

                try:
                    # Check that middleware is still uninitialized.
                    if self._request_middleware is None:
                        self.load_middleware()
                except Exception:
                    # Unload whatever middleware we got
                    self._request_middleware = None
                    raise

It seems there is no need for this try block, because self._request_middleware is being used as a flag of successful initialization completion.
Here where it gets assigned(https://github.com/django/django/blob/master/django/core/handlers/base.py#L73-L75):

        # We only assign to this when initialization is complete as it is used
        # as a flag for initialization being complete.
        self._request_middleware = request_middleware

Maybe, there are some thread safety issues that I'm missing, in which case, I'm sorry for bothering.

Change History (5)

comment:1 Changed 23 months ago by Tim Graham

Cc: Andrew Godwin added
Summary: Redundant try blockRedundant try block in WSGIHandler

Andrew, I tracked that addition to the patch you committed in #11193 [ba585a2c6d]. Any insight?

comment:2 Changed 23 months ago by Simon Charette

It looks like this change predates the use of context managers in Django.

comment:3 Changed 23 months ago by Claude Paroz

Triage Stage: UnreviewedAccepted

The try block should probably have been removed during this commit: [e0fce8706d31f]

comment:4 Changed 23 months ago by Attila Tovt

Has patch: set

comment:5 Changed 23 months ago by Attila Tovt <uran198@…>

Resolution: fixed
Status: newclosed

In 80927455:

Fixed #25779 -- Removed redundant try block in WSGIHandler

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