Code

Ticket #3881: session_save.patch

File session_save.patch , 1.0 KB (added by anonymous, 7 years ago)

Don't save session if an exception occured

Line 
1Index: contrib/sessions/middleware.py
2===================================================================
3--- contrib/sessions/middleware.py      (revision 4872)
4+++ contrib/sessions/middleware.py      (working copy)
5@@ -68,10 +68,21 @@
6     _session = property(_get_session)
7 
8 class SessionMiddleware(object):
9+    def __init__(self):
10+        self.exception_occured = False
11+
12     def process_request(self, request):
13         request.session = SessionWrapper(request.COOKIES.get(settings.SESSION_COOKIE_NAME, None))
14 
15+    def process_exception(self, request, exception):
16+        self.exception_occured = True
17+
18     def process_response(self, request, response):
19+        # Don't try to save session when process method raised an exception. In case of database exceptions
20+        # it may not even be possible. See ticket #3881.
21+        if self.exception_occured:
22+            return response
23+
24         # If request.session was modified, or if response.session was set, save
25         # those changes and set a session cookie.
26         try: