Changeset 8688
- Timestamp:
- 08/28/08 20:44:11 (3 months ago)
- Files:
-
- django/trunk/AUTHORS (modified) (1 diff)
- django/trunk/django/contrib/sessions/backends/file.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/AUTHORS
r8672 r8688 364 364 sloonz <simon.lipp@insa-lyon.fr> 365 365 SmileyChris <smileychris@gmail.com> 366 Warren Smith <warren@wandrsmith.net> 366 367 smurf@smurf.noris.de 367 368 Vsevolod Solovyov django/trunk/django/contrib/sessions/backends/file.py
r8451 r8688 6 6 from django.contrib.sessions.backends.base import SessionBase, CreateError 7 7 from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured 8 from django.core.files import locks 8 9 10 IO_LOCK_SUFFIX = "_iolock" 9 11 10 12 class SessionStore(SessionBase): … … 43 45 return os.path.join(self.storage_path, self.file_prefix + session_key) 44 46 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 45 53 def load(self): 46 54 session_data = {} 47 55 try: 48 session_file = open(self._key_to_file(), "rb") 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 lock_path = self._key_to_io_lock_file() 61 io_lock_file = open(lock_path, "rb") 62 locks.lock(io_lock_file, locks.LOCK_SH) 49 63 try: 64 session_file = open(self._key_to_file(), "rb") 50 65 try: 51 session_data = self.decode(session_file.read()) 52 except (EOFError, SuspiciousOperation): 53 self.create() 66 try: 67 session_data = self.decode(session_file.read()) 68 except (EOFError, SuspiciousOperation): 69 self.create() 70 finally: 71 session_file.close() 54 72 finally: 55 session_file.close() 73 locks.unlock(io_lock_file) 74 io_lock_file.close() 75 os.unlink(lock_path) 56 76 except IOError: 57 77 pass … … 77 97 session_data = self._get_session(no_load=must_create) 78 98 try: 79 fd = os.open(self._key_to_file(self.session_key), flags) 99 # Open and acquire an exclusive lock on the I/O lock file before 100 # attempting to write the session file. This makes other threads 101 # or processes wait to read or write the session file until we are 102 # finished writing it. 103 lock_path = self._key_to_io_lock_file() 104 io_lock_file = open(lock_path, "wb") 105 locks.lock(io_lock_file, locks.LOCK_EX) 80 106 try: 81 os.write(fd, self.encode(session_data)) 107 fd = os.open(self._key_to_file(self.session_key), flags) 108 try: 109 os.write(fd, self.encode(session_data)) 110 finally: 111 os.close(fd) 82 112 finally: 83 os.close(fd) 113 locks.unlock(io_lock_file) 114 io_lock_file.close() 115 os.unlink(lock_path) 84 116 except OSError, e: 85 117 if must_create and e.errno == errno.EEXIST:
