resolver: take the numeric-only getaddrinfo path when host is already an IP#3666
Open
HrachShah wants to merge 2 commits into
Open
resolver: take the numeric-only getaddrinfo path when host is already an IP#3666HrachShah wants to merge 2 commits into
HrachShah wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the host passed to a resolver is already a literal IPv4 or IPv6 address, pass
AI_NUMERICHOST|AI_NUMERICSERVtogetaddrinfoso 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 byDefaultExecutorResolver,BlockingResolver,ThreadedResolver) and the loop-basedDefaultLoopResolver, and is conditioned onis_valid_ip(host)so non-numeric hosts continue to use the normal DNS path unchanged.is_valid_ipis called once per resolve and is itself backed bygetaddrinfowithAI_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
@bdarnellcalled out in tornado#3113 as the right place to land the optimization:TCPClientis free to assumeAF_INET{,6}, soinet_ptonis fine here even though theResolverabstraction has to handle arbitrary address families.Closes tornado#3113.