From 090e6cca9efb90a502e57688b038ec0351947d11 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 22 Jun 2019 12:45:57 -0400 Subject: [PATCH] netutil: Ignore EADDRNOTAVAIL when binding to localhost ipv6 This happens in docker with default configurations and is generally harmless. Fixes #2274 --- tornado/netutil.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tornado/netutil.py b/tornado/netutil.py index 293f3aab5..b309d67d1 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -159,7 +159,29 @@ def bind_sockets( sockaddr = tuple([host, bound_port] + list(sockaddr[2:])) sock.setblocking(False) - sock.bind(sockaddr) + try: + sock.bind(sockaddr) + except OSError as e: + if ( + errno_from_exception(e) == errno.EADDRNOTAVAIL + and address == "localhost" + and sockaddr[0] == "::1" + ): + # On some systems (most notably docker with default + # configurations), ipv6 is partially disabled: + # socket.has_ipv6 is true, we can create AF_INET6 + # sockets, and getaddrinfo("localhost", ..., + # AF_PASSIVE) resolves to ::1, but we get an error + # when binding. + # + # Swallow the error, but only for this specific case. + # If EADDRNOTAVAIL occurs in other situations, it + # might be a real problem like a typo in a + # configuration. + sock.close() + continue + else: + raise bound_port = sock.getsockname()[1] sock.listen(backlog) sockets.append(sock) -- 2.47.2