Repository navigation
Conversation
When Paramiko raises socket.gaierror on a direct connection, the connector retries getaddrinfo and falls back to gethostbyname if that lookup fails. It tries the returned addresses with the configured port and timeout, preserving IPv6 scope IDs. It passes the connected socket and original hostname to Paramiko so host key checks still use the hostname. If the SSH retry fails, the connector attempts to close the socket and propagates the SSH error. Fixes pyinfra-dev#2057
wowi42
left a comment
There was a problem hiding this comment.
The fallback needs fixes before merge. The main issues are resolver edge cases and socket timeout behavior, detailed inline.
| os.environ["SSH_AUTH_SOCK"] = identity_agent | ||
| try: | ||
| super().connect(hostname, **config) | ||
| except socket.gaierror as original_error: |
There was a problem hiding this comment.
Empty getaddrinfo results do not raise socket.gaierror, so they bypass this fallback. Paramiko 3.5.1 raises IndexError on the initial empty result, and an empty retry re-raises the original error here. Please handle empty results explicitly and cover both paths.
There was a problem hiding this comment.
Good catch, fixed in 94ef8c3.
- The fallback now also catches the IndexError Paramiko 3.5.1 raises on an empty getaddrinfo result (direct connections only). If the retry does return addresses, that IndexError is re-raised as is.
- An empty retry is now treated like a failed one, so it falls through to gethostbyname instead of re-raising the original error.
- If nothing resolves after an empty result, it raises socket.gaierror with "getaddrinfo returned no results", which the ssh connector already reports as "Could not resolve hostname".
Tests cover the IndexError path (the first connect goes through real Paramiko 3.5.1, with socket calls mocked), empty result with no gethostbyname fallback, gaierror followed by an empty retry, and an unrelated IndexError not being masked.
| last_error: OSError = original_error | ||
| for candidate in candidates: | ||
| try: | ||
| sock = socket.create_connection(candidate, timeout=config.get("timeout")) |
There was a problem hiding this comment.
Passing timeout=None to socket.create_connection explicitly disables a process-wide default set with socket.setdefaulttimeout. Please omit the timeout argument when it is unset.
There was a problem hiding this comment.
Fixed in 567c38e: the fallback path now passes timeout to socket.create_connection only when config.get("timeout") is set, so a process-wide socket.setdefaulttimeout still applies otherwise.
| def setUp(self): | ||
| get_ssh_config.cache = {} | ||
|
|
||
| @patch("pyinfra.connectors.sshuserclient.client.get_ssh_config", return_value=None) |
There was a problem hiding this comment.
This mocks Paramiko and all resolver and connection calls, so it only proves the IPv4 happy path. Please add real-Paramiko coverage with lower-level socket doubles for empty results, resolver failures, candidate failures, existing sock and ProxyJump paths, ports, timeouts, cleanup, and host-key verification.
There was a problem hiding this comment.
Added in 0da4b66. These run the real paramiko.SSHClient.connect from pyinfra's client and replace only socket.getaddrinfo, socket.gethostbyname, socket.create_connection and the Transport (through Paramiko's transport_factory argument):
- test_real_paramiko_fallback_connects_over_ipv4: port 2222 and timeout=7 reach create_connection and start_client, host key checked against known hosts, password auth runs
- test_real_paramiko_fallback_tries_next_candidate: IPv6 candidate fails, IPv4 candidate is used
- test_real_paramiko_fallback_all_candidates_fail: the last connect error is raised, no transport is built
- test_real_paramiko_fallback_closes_socket_on_handshake_failure: the fallback socket is closed when start_client fails
- test_real_paramiko_fallback_rejects_mismatched_host_key: BadHostKeyException, no auth attempt, socket closed
- test_real_paramiko_explicit_sock_skips_fallback: a caller-supplied sock (the ProxyJump path) gets no resolver or create_connection calls and is not closed
test_connect_empty_getaddrinfo_paramiko_index_error from 94ef8c3 covers the empty result through real Paramiko. I left out an in-process SSH server.
An empty getaddrinfo result does not raise socket.gaierror. Paramiko
3.5.1 then raises IndexError while building NoValidConnectionsError({}),
and an empty retry in the fallback re-raised the original error without
trying gethostbyname.
Catch that IndexError for direct connections, treat an empty retry like
a failed one and fall back to gethostbyname, and raise a gaierror saying
getaddrinfo returned no results when nothing resolves. An IndexError
when the retry does return addresses is re-raised unchanged.
Passing timeout=None to socket.create_connection overrides a process-wide default set with socket.setdefaulttimeout. Only pass timeout when the connection config sets one.
Drive paramiko.SSHClient.connect for real and replace only socket-level calls (getaddrinfo, gethostbyname, create_connection) and the Transport, via Paramiko's transport_factory argument. Covers the IPv4 fallback, trying the next candidate, all candidates failing, closing the fallback socket on a handshake failure or host key mismatch, and an explicit sock bypassing the fallback.
|
The macOS 3.14 unit job looks like it never picked up a runner and was cancelled at 15 minutes, which is what took |
Background
A deployment can fail with “Could not resolve hostname” even when the same machine can reach the host by name over SSH.
The SSH connector let Paramiko’s getaddrinfo error end the connection attempt. It never tried gethostbyname, which can still return an IPv4 address for the same host.
Summary of the fix
When Paramiko raises socket.gaierror on a direct connection, the connector retries getaddrinfo and falls back to gethostbyname if that lookup fails. It tries the returned addresses with the configured port and timeout, preserving IPv6 scope IDs. It passes the connected socket and original hostname to Paramiko so host key checks still use the hostname. If the SSH retry fails, the connector attempts to close the socket and propagates the SSH error.
Verification
uv run --frozen --project . pytest -q tests/test_connectors/test_sshuserclient.pypassed all 30 tests, including the mocked IPv4 fallback, with one warning.repo_lintran five times with no recorded output.uv run python scripts/lint_arguments_sync.pyran with no recorded output.uv run python scripts/lint_operation_arguments.pyran with no recorded output. The tests intests/test_connectors/test_sshuserclient.pyfail without the fix in the pyinfra SSHClient change (checked by reverting the source changes). Regression check for the pyinfra SSHClient change: 1 fails on 3.x, all 30 pass with the fix; hosted CI not run.Fixes #2057
AI disclosure
AI was used for assistance.