Code

Ticket #8616: session_file_io_race_fix.diff

File session_file_io_race_fix.diff, 3.8 KB (added by warren@…, 6 years ago)

I/O Race condition fix that does not rely on file locking

Line 
1Index: django/contrib/sessions/backends/file.py
2===================================================================
3--- django/contrib/sessions/backends/file.py    (revision 8727)
4+++ django/contrib/sessions/backends/file.py    (working copy)
5@@ -47,10 +47,14 @@
6         try:
7             session_file = open(self._key_to_file(), "rb")
8             try:
9-                try:
10-                    session_data = self.decode(session_file.read())
11-                except (EOFError, SuspiciousOperation):
12-                    self.create()
13+                file_data = session_file.read()
14+                # Don't fail if there is no data in the session file.
15+                # We may have opened the empty placeholder file.
16+                if file_data:
17+                    try:
18+                        session_data = self.decode(file_data)
19+                    except (EOFError, SuspiciousOperation):
20+                        self.create()
21             finally:
22                 session_file.close()
23         except IOError:
24@@ -69,23 +73,56 @@
25             return
26 
27     def save(self, must_create=False):
28-        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | getattr(os, 'O_BINARY', 0)
29-        if must_create:
30-            flags |= os.O_EXCL
31-        # Because this may trigger a load from storage, we must do it before
32-        # truncating the file to save.
33+        # Get the session data now, before we start messing
34+        # with the file it is stored within.
35         session_data = self._get_session(no_load=must_create)
36+       
37+        session_file_name = self._key_to_file()
38+
39         try:
40-            fd = os.open(self._key_to_file(self.session_key), flags)
41-            try:
42-                os.write(fd, self.encode(session_data))
43-            finally:
44-                os.close(fd)
45+            # Make sure the file exists.  If it does not already exist, an
46+            # empty placeholder file is created.
47+            flags = os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0)
48+            if must_create:
49+                flags |= os.O_EXCL
50+            fd = os.open(session_file_name, flags)
51+            os.close(fd)
52+           
53         except OSError, e:
54             if must_create and e.errno == errno.EEXIST:
55                 raise CreateError
56             raise
57-        except (IOError, EOFError):
58+
59+        # Write the session file without interfering with other threads
60+        # or processes.  By writing to an atomically generated temporary
61+        # file and then using the atomic os.rename() to make the complete
62+        # file visible, we avoid having to lock the session file, while
63+        # still maintaining its integrity.
64+        #
65+        # Note: Locking the session file was explored, but rejected in part
66+        # because in order to be atomic and cross-platform, it required a
67+        # long-lived lock file for each session, doubling the number of
68+        # files in the session storage directory at any given time.  This
69+        # rename solution is cleaner and avoids any additional overhead
70+        # when reading the session data, which is the more common case
71+        # unless SESSION_SAVE_EVERY_REQUEST = True.
72+        #
73+        # See ticket #8616.
74+        dir, prefix = os.path.split(session_file_name)
75+   
76+        try:
77+            output_file_fd, output_file_name = tempfile.mkstemp(dir=dir,
78+                prefix=prefix + '_out_')
79+            try:
80+                try:
81+                    os.write(output_file_fd, self.encode(session_data))
82+                finally:
83+                    os.close(output_file_fd)
84+                os.rename(output_file_name, session_file_name)
85+            finally:
86+                os.unlink(output_file_name)
87+               
88+        except (OSError, IOError, EOFError):
89             pass
90 
91     def exists(self, session_key):