Code

Ticket #8086: load_session_before_save.diff

File load_session_before_save.diff, 2.1 KB (added by mrts, 6 years ago)

The DRY version. Note that this is backwards-incompatible for custom backends. Tested with r8187

Line 
1Index: django/contrib/sessions/backends/base.py
2===================================================================
3--- django/contrib/sessions/backends/base.py    (revision 8187)
4+++ django/contrib/sessions/backends/base.py    (working copy)
5@@ -217,6 +217,15 @@
6         """
7         Saves the session data.
8         """
9+        # Avoid race conditions, see #6984. Make sure the session dict is
10+        # loaded into memory before saving it back.
11+        self._get_session()
12+        self._save()
13+
14+    def _save(self):
15+        """
16+        The actual saving function that has to be overridden by backends.
17+        """
18         raise NotImplementedError
19 
20     def delete(self, session_key):
21Index: django/contrib/sessions/backends/file.py
22===================================================================
23--- django/contrib/sessions/backends/file.py    (revision 8187)
24+++ django/contrib/sessions/backends/file.py    (working copy)
25@@ -60,7 +60,7 @@
26             pass
27         return session_data
28 
29-    def save(self):
30+    def _save(self):
31         try:
32             f = open(self._key_to_file(self.session_key), "wb")
33             try:
34Index: django/contrib/sessions/backends/cache.py
35===================================================================
36--- django/contrib/sessions/backends/cache.py   (revision 8187)
37+++ django/contrib/sessions/backends/cache.py   (working copy)
38@@ -13,7 +13,7 @@
39         session_data = self._cache.get(self.session_key)
40         return session_data or {}
41 
42-    def save(self):
43+    def _save(self):
44         self._cache.set(self.session_key, self._session, self.get_expiry_age())
45 
46     def exists(self, session_key):
47Index: django/contrib/sessions/backends/db.py
48===================================================================
49--- django/contrib/sessions/backends/db.py      (revision 8187)
50+++ django/contrib/sessions/backends/db.py      (working copy)
51@@ -36,7 +36,7 @@
52             return False
53         return True
54 
55-    def save(self):
56+    def _save(self):
57         Session.objects.create(
58             session_key = self.session_key,
59             session_data = self.encode(self._session),