Skip to content

Commit 224ae88

Browse files
authored
Return a default value if address resolution fails (#9051)
1 parent 3f17ddb commit 224ae88

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

distributed/utils.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,23 @@ def get_fileno_limit():
171171
@toolz.memoize
172172
def _get_ip(host, port, family):
173173
def hostname_fallback():
174-
addr_info = socket.getaddrinfo(
175-
socket.gethostname(), port, family, socket.SOCK_DGRAM, socket.IPPROTO_UDP
176-
)[0]
177-
return addr_info[4][0]
174+
try:
175+
addr_info = socket.getaddrinfo(
176+
socket.gethostname(),
177+
port,
178+
family,
179+
socket.SOCK_DGRAM,
180+
socket.IPPROTO_UDP,
181+
)[0]
182+
return addr_info[4][0]
183+
# If getaddrinfo() fails, relay the error and return a sane default
184+
except socket.gaierror as e:
185+
warnings.warn(
186+
f"Couldn't detect a suitable IP address ({e}). "
187+
"Falling back to 127.0.0.1.",
188+
RuntimeWarning,
189+
)
190+
return "127.0.0.1"
178191

179192
# By using a UDP socket, we don't actually try to connect but
180193
# simply select the local address through which *host* is reachable.

0 commit comments

Comments
 (0)