Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1367

Show
Ignore:
Timestamp:
09/15/06 15:22:17
Author:
fumanchu
Message:

Some additional session safety (see #563).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/sessions.py

    r1302 r1367  
    8787    def save(self): 
    8888        """Save session data.""" 
    89         # If session data has never been loaded then it's never been 
    90         #   accessed: no need to delete it 
    91         if self.loaded: 
    92             t = datetime.timedelta(seconds = self.timeout * 60) 
    93             expiration_time = datetime.datetime.now() + t 
    94             self._save(expiration_time) 
    95          
    96         if self.locked: 
    97             # Always release the lock if the user didn't release it 
    98             self.release_lock() 
     89        try: 
     90            # If session data has never been loaded then it's never been 
     91            #   accessed: no need to delete it 
     92            if self.loaded: 
     93                t = datetime.timedelta(seconds = self.timeout * 60) 
     94                expiration_time = datetime.datetime.now() + t 
     95                self._save(expiration_time) 
     96             
     97        finally: 
     98            if self.locked: 
     99                # Always release the lock if the user didn't release it 
     100                self.release_lock() 
    99101     
    100102    def load(self): 
     
    186188                except KeyError: 
    187189                    pass 
     190                try: 
     191                    del self.locks[id] 
     192                except KeyError: 
     193                    pass 
    188194     
    189195    def _load(self): 
     
    198204    def acquire_lock(self): 
    199205        self.locked = True 
    200         self.locks.setdefault(self.id, threading.Semaphore()).acquire() 
     206        self.locks.setdefault(self.id, threading.RLock()).acquire() 
    201207     
    202208    def release_lock(self): 
     
    366372        # If the session is still locked we release the lock 
    367373        sess.release_lock() 
     374    del cherrypy._serving.session 
    368375close.failsafe = True 
     376close.priority = 90 
    369377 
    370378 
     
    389397        cherrypy.session = cherrypy._ThreadLocalProxy('session', _def_session) 
    390398     
    391     # Create and attach a new Session instance to cherrypy.request
     399    # Create and attach a new Session instance to cherrypy._serving
    392400    # It will possess a reference to (and lock, and lazily load) 
    393401    # the requested session data. 
  • trunk/cherrypy/test/test_conn.py

    r1345 r1367  
    211211         
    212212        # Put request 1 
    213         conn.putrequest("GET", "/", skip_host=True) 
    214         conn.putheader("Host", self.HOST) 
    215         conn.endheaders() 
    216          
    217         # Put request 2 
    218         conn._output('GET /hello HTTP/1.1') 
    219         conn._output("Host: %s" % self.HOST) 
    220         conn._send_output() 
    221          
    222         # Retrieve response 1 
    223         response = conn.response_class(conn.sock, method="GET") 
    224         response.begin() 
    225         body = response.read() 
    226         self.assertEqual(response.status, 200) 
    227         self.assertEqual(body, pov) 
    228          
    229         # Retrieve response 2 
     213        conn.putrequest("GET", "/hello", skip_host=True) 
     214        conn.putheader("Host", self.HOST) 
     215        conn.endheaders() 
     216         
     217        for trial in xrange(5): 
     218            # Put next request 
     219            conn._output('GET /hello HTTP/1.1') 
     220            conn._output("Host: %s" % self.HOST) 
     221            conn._send_output() 
     222             
     223            # Retrieve previous response 
     224            response = conn.response_class(conn.sock, method="GET") 
     225            response.begin() 
     226            body = response.read() 
     227            self.assertEqual(response.status, 200) 
     228            self.assertEqual(body, "Hello, world!") 
     229         
     230        # Retrieve final response 
    230231        response = conn.response_class(conn.sock, method="GET") 
    231232        response.begin() 

Hosted by WebFaction

Log in as guest/cpguest to create tickets