Skip to content

Commit b9cc610

Browse files
joeribekkeraaugustin
authored andcommitted
Fixed django#9084 - Best approach for an OS to atomically rename the session file.
1 parent 7106a1e commit b9cc610

File tree

1 file changed

+6
-1
lines changed
  • django/contrib/sessions/backends

1 file changed

+6
-1
lines changed

django/contrib/sessions/backends/file.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import errno
33
import os
4+
import shutil
45
import tempfile
56

67
from django.conf import settings
@@ -147,7 +148,11 @@ def save(self, must_create=False):
147148
os.write(output_file_fd, self.encode(session_data).encode())
148149
finally:
149150
os.close(output_file_fd)
150-
os.rename(output_file_name, session_file_name)
151+
152+
# This will atomically rename the file (os.rename) if the OS
153+
# supports it. Otherwise this will result in a shutil.copy2
154+
# and os.unlink (for example on Windows). See #9084.
155+
shutil.move(output_file_name, session_file_name)
151156
renamed = True
152157
finally:
153158
if not renamed:

0 commit comments

Comments
 (0)