# HG changeset patch # User Kristján Valur Jónsson # Date 1331768746 25200 # Node ID 32a2f865ad7b2db6d375a33561eddfb48a698a82 # Parent aac602bd8ec39e39c4fd74257bef6cabc574dbe0 socketserver with no select diff --git a/Lib/socketserver.py b/Lib/socketserver.py --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -286,20 +286,32 @@ self.shutdown_request(request) def _get_request_timeout(self, timeout): - """Get one request, with timeout.""" - # Support people who used socket.settimeout() to escape - # handle_request before self.timeout was available. - sock_timeout = self.socket.gettimeout() - if timeout is None: - timeout = sock_timeout - elif sock_timeout is not None: - timeout = min(timeout, sock_timeout) - #use select so that we don't have to mess with the attributes - #of sockets or catch timeout exceptions. - fd_sets = select.select([self], [], [], timeout) - if not fd_sets[0]: - return None - return self.get_request() + """Get one request, possibly timing out with socket.timeout. + Make sure to not change the socket's timeout property + permanently. The socket's timeout takes precedence if it + is lower. + """ + try: + if timeout is not None: + stored = self.socket.gettimeout() + #use the socket timeout if it was lower. This is to + #support people that use socket.settimeout(). + if stored is None or stored > timeout: + #temporarily change the timeout of the listenin socket + self.socket.settimeout(timeout) + try: + request, client_addr = self.get_request() + #enforce timeout inheritance + try: + request.settimeout(stored) + except AttributeError: + pass + return request, client_addr + finally: + self.socket.settimeout(stored) + return self.get_request() + except socket.timeout: + return def handle_timeout(self): """Called if no new request arrives within self.timeout.