Skip to content

resolver: take the numeric-only getaddrinfo path when host is already an IP#3666

Open
HrachShah wants to merge 2 commits into
tornadoweb:masterfrom
HrachShah:fix/resolver-skip-getaddrinfo-for-ips
Open

resolver: take the numeric-only getaddrinfo path when host is already an IP#3666
HrachShah wants to merge 2 commits into
tornadoweb:masterfrom
HrachShah:fix/resolver-skip-getaddrinfo-for-ips

Conversation

@HrachShah

Copy link
Copy Markdown

When the host passed to a resolver is already a literal IPv4 or IPv6 address, pass AI_NUMERICHOST|AI_NUMERICSERV to getaddrinfo so the resolver does not have to acquire the resolver lock or walk the DNS subsystem for a value it already knows. The same flag is set on both the synchronous _resolve_addr (used by DefaultExecutorResolver, BlockingResolver, ThreadedResolver) and the loop-based DefaultLoopResolver, and is conditioned on is_valid_ip(host) so non-numeric hosts continue to use the normal DNS path unchanged.

is_valid_ip is called once per resolve and is itself backed by getaddrinfo with AI_NUMERICHOST, so its cost is roughly equivalent to one extra resolver call we used to make unconditionally; the savings show up on the hot path for IP literals, where the resolver skips the DNS subsystem entirely.

This is the approach @bdarnell called out in tornado#3113 as the right place to land the optimization: TCPClient is free to assume AF_INET{,6}, so inet_pton is fine here even though the Resolver abstraction has to handle arbitrary address families.

Closes tornado#3113.

Hrach added 2 commits July 1, 2026 19:35
The previous code raised a bare 'Exception()' (no message, no type info)
when the timedelta pattern failed to match, then wrapped the whole body
in 'try/except Exception: raise' which is a no-op. A bare Exception()
gives no hint about what failed; downstream code that catches
options.Error (the rest of the module) would not see the failure, and
the user would get 'Exception:' with no message at all.

Replace 'raise Exception()' with 'raise Error("Invalid time delta: %r"
% value)' (matching the style of _parse_datetime's
'Unrecognized date/time format') and drop the try/except: raise wrapper
so genuine programming errors surface normally.

Added test_parse_timedelta_invalid_raises_options_error in
tornado/test/options_test.py covers --foo=xyz; the test fails on the
pre-fix code (raises Exception, not Error) and passes with the fix.
… an IP

When the host passed to a resolver is already a literal IPv4 or IPv6
address, pass AI_NUMERICHOST|AI_NUMERICSERV to getaddrinfo so the
resolver does not have to acquire the resolver lock or walk the DNS
subsystem for a value it already knows. The same flag is set on both
the synchronous _resolve_addr (used by DefaultExecutorResolver,
BlockingResolver, ThreadedResolver) and the loop-based
DefaultLoopResolver, and is conditioned on is_valid_ip(host) so
non-numeric hosts continue to use the normal DNS path unchanged.

is_valid_ip is called once per resolve and is itself backed by
getaddrinfo with AI_NUMERICHOST, so its cost is roughly equivalent to
one extra resolver call we used to make unconditionally; the savings
show up on the hot path for IP literals, where the resolver skips the
DNS subsystem entirely.

Closes tornado#3113.
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.

1 participant