Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to bind to IPv6 address specified by name, not address #2689

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions gunicorn/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,50 @@ def create_sockets(conf, log, fds=None):

# no sockets is bound, first initialization of gunicorn in this env.
for addr in laddr:
sock_type = _sock_type(addr)
sock = None
for i in range(5):
try:
sock = sock_type(addr, conf, log)
except OSError as e:
if e.args[0] == errno.EADDRINUSE:
log.error("Connection in use: %s", str(addr))
if e.args[0] == errno.EADDRNOTAVAIL:
log.error("Invalid address: %s", str(addr))
msg = "connection to {addr} failed: {error}"
log.error(msg.format(addr=str(addr), error=str(e)))
if i < 5:
log.debug("Retrying in 1 second.")
time.sleep(1)
some_sock_succeeded = False
try:
if isinstance(addr, tuple):
addrinfos = socket.getaddrinfo(addr[0], addr[1], type=socket.SOCK_STREAM)
else:
addrinfos = [(socket.AF_UNIX, None, None, None, addr)]
except OSError as e:
log.error("Failed getaddrinfo for %s: %s", str(addr), str(e))
continue
for sock_family, _, _, _, sock_addr in addrinfos:
if sock_family == socket.AF_INET:
sock_type = TCPSocket
elif sock_family == socket.AF_INET6:
sock_type = TCP6Socket
elif sock_family == socket.AF_UNIX:
sock_type = UnixSocket
else:
break
log.warning("Ignoring unknown socket family: %s", str(sock_family))
continue
sock = None
for i in range(5):
try:
sock = sock_type(sock_addr, conf, log)
except OSError as e:
if e.args[0] == errno.EADDRINUSE:
log.error("Connection in use: %s", str(sock_addr))
if e.args[0] == errno.EADDRNOTAVAIL:
log.error("Invalid address: %s", str(sock_addr))
msg = "connection to {addr} failed: {error}"
log.error(msg.format(addr=str(sock_addr), error=str(e)))
if i < 5:
log.debug("Retrying in 1 second.")
time.sleep(1)
else:
break

if sock is None:
if sock is not None:
listeners.append(sock)
some_sock_succeeded = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we bind to all addresses returned from getaddrinfo, or just the first one that succeeds?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should bind to them all, try at least. IIUC getaddrinfo returns the result in unspecified order. It means that one call may return [IPv6 address, IPv4 address] and the next [IPv4 address, IPv6 address]. If binding only to the first one we'd be introducing non-determinism between runs.


if not some_sock_succeeded:
log.error("Can't connect to %s", str(addr))
sys.exit(1)

listeners.append(sock)

return listeners


Expand Down
Loading