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

wsgi server: address family discovery is not quite right #1006

Merged
merged 1 commit into from Feb 13, 2024

Conversation

jclulow
Copy link
Contributor

@jclulow jclulow commented Feb 11, 2024

The code introduced to improve binding to an IPv6 address is based on similar code in Python itself, but is missing some critical arguments to the socket.getaddrinfo() call: in particular, the socket type must be set to SOCK_STREAM, because we want a TCP connection; the AI_PASSIVE flag should also be passed because we intend to use the result for binding a listen socket rather than making an outbound connection.

@jclulow
Copy link
Contributor Author

jclulow commented Feb 11, 2024

By way of example, this is the output with and without the flags on an Ubuntu 22.04 system:

#!/usr/bin/env python3

import socket

print("WITH type set:")
print(socket.getaddrinfo("0.0.0.0", 9009,
    type=socket.SOCK_STREAM, flags=socket.AI_PASSIVE))

print("WITHOUT type set:")
print(socket.getaddrinfo("0.0.0.0", 9009))
WITH type set:
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('0.0.0.0', 9009))]
WITHOUT type set:
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('0.0.0.0', 9009)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('0.0.0.0', 9009)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('0.0.0.0', 9009))]

On some other platforms, getaddrinfo() requires at least the socket type to function as is expected here; e.g.,

WITH type set:
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 2>, 6, '', ('0.0.0.0', 9009))]
WITHOUT type set:
Traceback (most recent call last):
  File "/tmp/sigh_test.py", line 10, in <module>
    print(socket.getaddrinfo("0.0.0.0", 9009))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 9] service name not available for the specified socket type

The code introduced to improve binding to an IPv6 address is based on
similar code in Python itself, but is missing some critical arguments to
the socket.getaddrinfo() call: in particular, the socket type must be
set to SOCK_STREAM, because we want a TCP connection; the AI_PASSIVE
flag should also be passed because we intend to use the result for
binding a listen socket rather than making an outbound connection.

Signed-off-by: Joshua M. Clulow <josh@sysmgr.org>
Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

Thanks!

@csmarchbanks csmarchbanks merged commit 6ae7737 into prometheus:master Feb 13, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants