Fix [Errno -9] Address family for hostname not supported

On some cloud providers (OVH) when using an ipv4 address with
socket.AF_INET6 address family, we see the following exception when
trying to bind to the fingergw.listen_address:

  socket.gaierror: [Errno -9] Address family for hostname not supported

Work around this by using socket.getaddrinfo() to first look up the
supported socket family, then use that value to setup the socketserver
address_family.

Change-Id: I31df8f2fa8f1b986a2c4190008f13fe155991f2c
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2018-03-08 16:36:00 -05:00
parent 0c1a73be9c
commit 2477d310e8
No known key found for this signature in database
GPG Key ID: 611A80832067AF38
1 changed files with 20 additions and 2 deletions

View File

@ -71,9 +71,27 @@ class CustomThreadingTCPServer(socketserver.ThreadingTCPServer):
Custom version that allows us to drop privileges after port binding.
'''
address_family = socket.AF_INET6
def __init__(self, *args, **kwargs):
# NOTE(pabelanger): Set up address_family for socketserver based on the
# fingergw.listen_address setting in zuul.conf.
# param tuple args[0]: The address and port to bind to for
# socketserver.
server_address = args[0]
address_family = None
for res in socket.getaddrinfo(
server_address[0], server_address[1], 0, self.socket_type):
if res[0] == socket.AF_INET6:
# If we get an IPv6 address, break our loop and use that first.
address_family = res[0]
break
elif res[0] == socket.AF_INET:
address_family = res[0]
# Check to see if getaddrinfo failed.
if not address_family:
raise Exception("getaddrinfo returns an empty list")
self.address_family = address_family
self.user = kwargs.pop('user', None)
self.pid_file = kwargs.pop('pid_file', None)
socketserver.ThreadingTCPServer.__init__(self, *args, **kwargs)