From 25d3656b4837842e6b06a65e88a00a977eb3e198 Mon Sep 17 00:00:00 2001 From: George Kraft Date: Tue, 1 Oct 2013 15:13:33 -0500 Subject: CXMAN-225: Retry obtaining TftpServer port if we get a socket.error Seems like a race that occurs between the main thread and TftpServer thread. --- cxmanage_api/tftp.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cxmanage_api/tftp.py b/cxmanage_api/tftp.py index 0b33db8..91671cc 100644 --- a/cxmanage_api/tftp.py +++ b/cxmanage_api/tftp.py @@ -37,6 +37,7 @@ import socket import logging import traceback +from datetime import datetime, timedelta from tftpy import TftpClient, TftpServer, setLogLevel from threading import Thread from cxmanage_api import temp_dir @@ -74,10 +75,18 @@ class InternalTftp(Thread): self.port = port self.start() - # Get the port we actually hosted on (this covers the port=0 case) - while not self.server.sock: - pass - self.port = self.server.sock.getsockname()[1] + # Get the port we actually hosted on + if port == 0: + deadline = datetime.now() + timedelta(seconds=1) + while datetime.now() < deadline: + try: + self.port = self.server.sock.getsockname()[1] + break + except (AttributeError, socket.error): + pass + else: + # don't catch the error on our last attempt + self.port = self.server.sock.getsockname()[1] def run(self): """ Run the server. Listens indefinitely. """ -- cgit v1.2.1