Code

Ticket #3881: session_save.patch

File session_save.patch, 1.1 KB (added by hauserx@…, 7 years ago)

Save session only when no exception during request processing

  • contrib/sessions/middleware.py

     
    1212        self.session_key = session_key 
    1313        self.accessed = False 
    1414        self.modified = False 
     15        self.exception_occured = False 
    1516 
    1617    def __contains__(self, key): 
    1718        return key in self._session 
     
    7172    def process_request(self, request): 
    7273        request.session = SessionWrapper(request.COOKIES.get(settings.SESSION_COOKIE_NAME, None)) 
    7374 
     75    def process_exception(self, request, exception): 
     76        self.exception_occured = True; 
     77 
    7478    def process_response(self, request, response): 
     79        # Don't try to save session when process method raised an exception. In case of database exceptions 
     80        # it may not even be possible. See ticket #3881. 
     81        if self.exception_occured: 
     82            return response 
     83 
    7584        # If request.session was modified, or if response.session was set, save 
    7685        # those changes and set a session cookie. 
    7786        try: