From faa259e97b73476b676adeb5bd4ed7faa6c4b0a9 Mon Sep 17 00:00:00 2001 From: Zoltan Fedor Date: Tue, 15 Dec 2015 10:53:06 -0500 Subject: [PATCH] IPv6 ready hosts file localhost issue This is to resolve the 'Network is Unreachable' error experienced by a few when JupyterHUB is connecting to localhost. On most recent linux OS versions like CentOS 6, 7, Red Hat 6, 7, Oracle Linux 6, 7, etc, the hosts file (/etc/hosts) usually has a line to make the server IPv6-ready: ::1 localhost even if the given server actually has no IPv6 permissioned. In such case the Python socket library when connecting to 'localhost' will try to connect via the IPv6 protocol - which will fail with the 'Network is Unreachable' error. To solve this we capture this error and try to reconnect on 127.0.0.1 instead of localhost, alias forcing the user of the IPv4 protocol. --- jupyterhub/orm.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jupyterhub/orm.py b/jupyterhub/orm.py index bcbf6308..ca11e1da 100644 --- a/jupyterhub/orm.py +++ b/jupyterhub/orm.py @@ -116,7 +116,17 @@ class Server(Base): try: socket.create_connection((self.ip or 'localhost', self.port)) except socket.error as e: - if e.errno == errno.ECONNREFUSED: + if e.errno == errno.ENETUNREACH: + try: + socket.create_connection((self.ip or '127.0.0.1', self.port)) + except socket.error as e: + if e.errno == errno.ECONNREFUSED: + return False + else: + raise + else: + return True + elif e.errno == errno.ECONNREFUSED: return False else: raise