def close(self): try: self.lock.release() except threading.ThreadError: pass # lock may not have been acquired. self.socket.close()