From 836d20e5e141c4b0ff777d2f714ab2a47f426602 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Mon, 8 Apr 2013 17:03:06 -0700 Subject: [PATCH 1/2] closes #3045, #3123 for tornado < version 3.0 related to facebook/tornado#593 I've verified that this fix works for tornado 2.1, 2.4, 2.4.1, and the behavior it fixes does not trigger on tornado 3.0 --- IPython/frontend/html/notebook/notebookapp.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index 35a04485c..0837129b1 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -522,6 +522,30 @@ class NotebookApp(BaseIPythonApplication): try: self.http_server.listen(port, self.ip) except socket.error as e: + # XXX: remove the e.errno == -9 block when we require + # tornado >= 3.0 + if e.errno == -9: + # The flags passed to socket.getaddrinfo from + # tornado.netutils.bind_sockets can cause "gaierror: + # [Errno -9] Address family for hostname not supported" + # when the interface is not associated, for example. + # Changing the flags to exclude socket.AI_ADDRCONFIG does + # not cause this error, but the only way to do this is to + # monkeypatch socket to remove the AI_ADDRCONFIG attribute + saved_AI_ADDRCONFIG = socket.AI_ADDRCONFIG + self.log.info('Monkeypatching socket to fix tornado bug') + del(socket.AI_ADDRCONFIG) + try: + # retry the tornado call without AI_ADDRCONFIG flags + self.http_server.listen(port, self.ip) + except socket.error as e2: + e = e2 + else: + self.port = port + success = True + break + # restore the monekypatch + socket.AI_ADDRCONFIG = saved_AI_ADDRCONFIG if e.errno != errno.EADDRINUSE: raise self.log.info('The port %i is already in use, trying another random port.' % port) From 113ba6157ffa906049149c90531cf8e0f9356ce1 Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Tue, 9 Apr 2013 17:58:56 -0700 Subject: [PATCH 2/2] addressing @minrk's PR feedback * check tornado version * log.warn about the monkeypatching --- IPython/frontend/html/notebook/notebookapp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index 0837129b1..86d48fd41 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -40,6 +40,7 @@ from jinja2 import Environment, FileSystemLoader from zmq.eventloop import ioloop ioloop.install() +import tornado from tornado import httpserver from tornado import web @@ -524,7 +525,7 @@ class NotebookApp(BaseIPythonApplication): except socket.error as e: # XXX: remove the e.errno == -9 block when we require # tornado >= 3.0 - if e.errno == -9: + if e.errno == -9 and tornado.version_info[0] < 3: # The flags passed to socket.getaddrinfo from # tornado.netutils.bind_sockets can cause "gaierror: # [Errno -9] Address family for hostname not supported" @@ -533,7 +534,7 @@ class NotebookApp(BaseIPythonApplication): # not cause this error, but the only way to do this is to # monkeypatch socket to remove the AI_ADDRCONFIG attribute saved_AI_ADDRCONFIG = socket.AI_ADDRCONFIG - self.log.info('Monkeypatching socket to fix tornado bug') + self.log.warn('Monkeypatching socket to fix tornado bug') del(socket.AI_ADDRCONFIG) try: # retry the tornado call without AI_ADDRCONFIG flags