Ticket #8616: session_file_read_write_locking.diff

File session_file_read_write_locking.diff, 1.4 KB (added by warren@…, 16 years ago)
  • django/contrib/sessions/backends/file.py

     
    4444    def load(self):
    4545        session_data = {}
    4646        try:
    47             session_file = open(self._key_to_file(), "rb")
     47            flags = os.O_RDONLY | getattr(os, 'O_BINARY', 0)
     48
     49            # Use atomic file locking if available
     50            flags |= getattr(os, 'O_SHLOCK', 0)
     51
     52            fd = os.open(self._key_to_file(), flags)
     53            session_file = os.fdopen(fd, "rb")
    4854            try:
    4955                try:
    5056                    session_data = self.decode(session_file.read())
     
    5258                    self.create()
    5359            finally:
    5460                session_file.close()
    55         except IOError:
     61        except (OSError, IOError):
    5662            pass
    5763        return session_data
    5864
     
    7177        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | getattr(os, 'O_BINARY', 0)
    7278        if must_create:
    7379            flags |= os.O_EXCL
     80
     81        # Use atomic file locking if available
     82        flags |= getattr(os, 'O_EXLOCK', 0)
     83
    7484        # Because this may trigger a load from storage, we must do it before
    7585        # truncating the file to save.
    7686        session_data = self._get_session(no_load=must_create)
Back to Top