Ticket #8616: cross_platform_session_file_read_write_locking.diff

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

     
    55from django.conf import settings
    66from django.contrib.sessions.backends.base import SessionBase, CreateError
    77from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
     8from django.core.files import locks
    89
     10IO_LOCK_SUFFIX = "_iolock"
    911
    1012class SessionStore(SessionBase):
    1113    """
     
    4244
    4345        return os.path.join(self.storage_path, self.file_prefix + session_key)
    4446
     47    def _key_to_io_lock_file(self, session_key=None):
     48        """
     49        Get the I/O lock file associated with this session key.
     50        """
     51        return self._key_to_file(session_key) + IO_LOCK_SUFFIX
     52
    4553    def load(self):
    4654        session_data = {}
    4755        try:
     56            # Open and acquire a shared lock on the I/O lock file before
     57            # attempting to read the session file.  This makes us wait to read
     58            # the session file until another thread or process is finished
     59            # writing it.
     60            io_lock_file = open(self._key_to_io_lock_file(), "rb")
     61            locks.lock(io_lock_file, locks.LOCK_SH)
     62           
    4863            session_file = open(self._key_to_file(), "rb")
    4964            try:
    5065                try:
     
    5368                    self.create()
    5469            finally:
    5570                session_file.close()
     71                io_lock_file.close()
    5672        except IOError:
    5773            pass
    5874        return session_data
     
    7692        # truncating the file to save.
    7793        session_data = self._get_session(no_load=must_create)
    7894        try:
     95            # Open and acquire an exclusive lock on the I/O lock file before
     96            # attempting to write the session file.  This makes other threads
     97            # or processes wait to read or write the session file until we are
     98            # finished writing it.
     99            io_lock_file = open(self._key_to_io_lock_file(), "wb")
     100            locks.lock(io_lock_file, locks.LOCK_EX)
     101           
    79102            fd = os.open(self._key_to_file(self.session_key), flags)
    80103            try:
    81104                os.write(fd, self.encode(session_data))
    82105            finally:
    83106                os.close(fd)
     107                io_lock_file.close()
    84108        except OSError, e:
    85109            if must_create and e.errno == errno.EEXIST:
    86110                raise CreateError
     
    100124            session_key = self._session_key
    101125        try:
    102126            os.unlink(self._key_to_file(session_key))
     127            os.unlink(self._key_to_io_lock_file(session_key))
    103128        except OSError:
    104129            pass
    105130
Back to Top