Skip to content

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>
@jclulow jclulow force-pushed the fix-getaddrinfo-call branch from 2291d9f to 4d05006 Compare February 11, 2024 18:20
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants