]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
netutil: Ignore EADDRNOTAVAIL when binding to localhost ipv6
authorBen Darnell <ben@bendarnell.com>
Sat, 22 Jun 2019 16:45:57 +0000 (12:45 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 22 Jun 2019 16:49:56 +0000 (12:49 -0400)
This happens in docker with default configurations and is generally
harmless.

Fixes #2274

tornado/netutil.py

index 293f3aab5dd920081ff32d32bb6936b56e38c4ba..b309d67d18083e0533debcaed700b2d0f58ea59e 100644 (file)
@@ -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)