Ticket #16: flup_timeout_patch_r2347.diff

File flup_timeout_patch_r2347.diff, 3.5 kB (added by trac, 2 years ago)

This is based on rev 2347

  • server/scgi_base.py

    old new  
    3737import datetime 
    3838import os 
    3939import warnings 
    40  
     40import traceback 
    4141# Threads are required. If you want a non-threaded (forking) version, look at 
    4242# SWAP <http://www.idyll.org/~t/www-tools/wsgi/>. 
    4343import thread 
     
    198198                          handlerTime.seconds + 
    199199                          handlerTime.microseconds / 1000000.0) 
    200200 
     201class TimeoutException(Exception): 
     202    pass 
    201203class Connection(object): 
    202204    """ 
    203205    Represents a single client (web server) connection. A single request 
    204206    is handled, after which the socket is closed. 
    205207    """ 
    206     def __init__(self, sock, addr, server): 
     208    def __init__(self, sock, addr, server, timeout): 
    207209        self._sock = sock 
    208210        self._addr = addr 
    209211        self.server = server 
     212        self._timeout = timeout 
    210213 
    211214        self.logger = logging.getLogger(LoggerName) 
    212215 
     216    def timeout_handler(self, signum, frame): 
     217        self.logger.error('Timeout Exceeded') 
     218        self.logger.error("\n".join(traceback.format_stack(frame))) 
     219 
     220        raise TimeoutException 
     221 
    213222    def run(self): 
    214223        if len(self._addr) == 2: 
    215224            self.logger.debug('Connection starting up (%s:%d)', 
     
    264273        # Allocate Request 
    265274        req = Request(self, environ, input, output) 
    266275 
     276        # If there is a timeout 
     277        if self._timeout: 
     278            old_alarm = signal.signal(signal.SIGALRM, self.timeout_handler) 
     279            signal.alarm(self._timeout) 
     280             
    267281        # Run it. 
    268282        req.run() 
    269283 
    270284        output.close() 
    271285        input.close() 
    272286 
     287        # Restore old handler if timeout was given 
     288        if self._timeout: 
     289            signal.signal(signal.SIGALRM, old_alarm) 
     290             
     291 
     292 
    273293class BaseSCGIServer(object): 
    274294    # What Request class to use. 
    275295    requestClass = Request 
  • server/scgi.py

    old new  
    131131        for key in ('jobClass', 'jobArgs'): 
    132132            if kw.has_key(key): 
    133133                del kw[key] 
    134         ThreadedServer.__init__(self, jobClass=Connection, jobArgs=(self,), 
     134        ThreadedServer.__init__(self, jobClass=Connection, jobArgs=(self,None), 
    135135                                **kw) 
    136136 
    137137    def run(self): 
  • server/scgi_fork.py

    old new  
    9090    def __init__(self, application, scriptName=NoDefault, environ=None, 
    9191                 bindAddress=('localhost', 4000), umask=None, 
    9292                 allowedServers=None, 
    93                  loggingLevel=logging.INFO, debug=True, **kw): 
     93                 loggingLevel=logging.INFO, debug=True, timeout=None, **kw): 
    9494        """ 
    9595        scriptName is the initial portion of the URL path that "belongs" 
    9696        to your application. It is used to determine PATH_INFO (which doesn't 
     
    130130        for key in ('multithreaded', 'multiprocess', 'jobClass', 'jobArgs'): 
    131131            if kw.has_key(key): 
    132132                del kw[key] 
    133         PreforkServer.__init__(self, jobClass=Connection, jobArgs=(self,), **kw) 
     133         
     134        PreforkServer.__init__(self, jobClass=Connection, jobArgs=(self,timeout), **kw) 
    134135 
    135136    def run(self): 
    136137        """