From 9a4de00d0523b9799572b2dd6a7d8920bdccbe40 2011-07-15 17:33:22 From: MinRK Date: 2011-07-15 17:33:22 Subject: [PATCH] don't allow gethostbyname(gethostname()) failure to crash ipcontroller In some weird situations, socket.gethostbyname_ex(socket.gethostname()) can fail. Don't let this error raise, instead assume localhost and warn in these cases. --- diff --git a/IPython/parallel/apps/ipcontrollerapp.py b/IPython/parallel/apps/ipcontrollerapp.py index a7f0161..695ca17 100755 --- a/IPython/parallel/apps/ipcontrollerapp.py +++ b/IPython/parallel/apps/ipcontrollerapp.py @@ -192,7 +192,13 @@ class IPControllerApp(BaseParallelApplication): except AssertionError: pass else: - location = socket.gethostbyname_ex(socket.gethostname())[2][-1] + try: + location = socket.gethostbyname_ex(socket.gethostname())[2][-1] + except (socket.gaierror, IndexError): + self.log.warn("Could not identify this machine's IP, assuming 127.0.0.1." + " You may need to specify '--location=' to help" + " IPython decide when to connect via loopback.") + location = '127.0.0.1' cdict['location'] = location fname = os.path.join(self.profile_dir.security_dir, fname) with open(fname, 'wb') as f: diff --git a/IPython/parallel/util.py b/IPython/parallel/util.py index 104cc7e..fb83640 100644 --- a/IPython/parallel/util.py +++ b/IPython/parallel/util.py @@ -166,8 +166,13 @@ def disambiguate_ip_address(ip, location=None): """turn multi-ip interfaces '0.0.0.0' and '*' into connectable ones, based on the location (default interpretation of location is localhost).""" if ip in ('0.0.0.0', '*'): - external_ips = socket.gethostbyname_ex(socket.gethostname())[2] - if location is None or location in external_ips: + try: + external_ips = socket.gethostbyname_ex(socket.gethostname())[2] + except (socket.gaierror, IndexError): + # couldn't identify this machine, assume localhost + external_ips = [] + if location is None or location in external_ips or not external_ips: + # If location is unspecified or cannot be determined, assume local ip='127.0.0.1' elif location: return location