Merge pull request #3148 from ivanov/tornado-AI_ADDRCONFIG-patch

fix `gaierror: [Errno -9] Address family for hostname not supported`

error from unassociated interfaces.

related to facebook/tornado#593 - but the fix was only applied to 3.0

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

closes #3045
This commit is contained in:
Min RK 2013-04-10 10:25:40 -07:00
commit 073f894e82

View File

@ -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
@ -522,6 +523,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 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"
# 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.warn('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)