Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only use AI_ADDRCONFIG when supported by getaddrinfo #10542

Merged
merged 6 commits into from
Mar 16, 2025

Conversation

maxbachmann
Copy link
Contributor

The fallback implementation for getaddrinfo in CPython doesn't support AI_ADDRCONFIG and currently fails with a bad flags error. This changes the implementation to only set the flag if it's part of AI_MASK. Since AI_MASK isn't necessarily available either this has to be checked first.

E.g. the fallback implementation for getaddrinfo in CPython doesn't support it and currently fails with a bad flags error
@maxbachmann maxbachmann requested a review from asvetlov as a code owner March 12, 2025 11:47
Copy link

codspeed-hq bot commented Mar 12, 2025

CodSpeed Performance Report

Merging #10542 will not alter performance

Comparing maxbachmann:patch-1 (7d7d6b8) with master (a59e74b)

Summary

✅ 47 untouched benchmarks

Copy link

codecov bot commented Mar 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.71%. Comparing base (45b861f) to head (7d7d6b8).
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10542      +/-   ##
==========================================
+ Coverage   98.69%   98.71%   +0.01%     
==========================================
  Files         122      125       +3     
  Lines       37230    37370     +140     
  Branches     2064     2064              
==========================================
+ Hits        36745    36888     +143     
+ Misses        338      335       -3     
  Partials      147      147              
Flag Coverage Δ
CI-GHA 98.58% <66.66%> (+0.01%) ⬆️
OS-Linux 98.25% <33.33%> (+<0.01%) ⬆️
OS-Windows 96.19% <33.33%> (+<0.01%) ⬆️
OS-macOS 97.36% <66.66%> (+<0.01%) ⬆️
Py-3.10.11 97.28% <66.66%> (+0.01%) ⬆️
Py-3.10.16 97.81% <33.33%> (+<0.01%) ⬆️
Py-3.11.11 97.90% <33.33%> (+0.01%) ⬆️
Py-3.11.9 97.35% <66.66%> (+<0.01%) ⬆️
Py-3.12.9 98.35% <66.66%> (+<0.01%) ⬆️
Py-3.13.2 98.34% <66.66%> (+<0.01%) ⬆️
Py-3.9.13 97.15% <66.66%> (-0.01%) ⬇️
Py-3.9.21 97.68% <33.33%> (+<0.01%) ⬆️
Py-pypy7.3.16 92.77% <33.33%> (+3.53%) ⬆️
VM-macos 97.36% <66.66%> (+<0.01%) ⬆️
VM-ubuntu 98.25% <33.33%> (+<0.01%) ⬆️
VM-windows 96.19% <33.33%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bdraco
Copy link
Member

bdraco commented Mar 15, 2025

https://github.com/python/cpython/blob/e82c2ca2a59235bc1965b259f4421635861e0470/Modules/addrinfo.h#L92

I'm missing some context for this PR, as it looks like AI_MASK will always make AI_ADDRCONFIG ineffective.

@maxbachmann
Copy link
Contributor Author

AI_MASK are valid flag mask for getaddrinfo. In the case of Pythons own fallback implementation AI_ADDRCONFIG isn't supported in getaddrinfo and so it will always make it ineffective. However there are other systems that do provide getaddrinfo + AI_MASK and do support AI_ADDRCONFIG. An example for such a system is Android.

@bdraco
Copy link
Member

bdraco commented Mar 16, 2025

We need a changelog fragment for this change.

As there's no linked issue, and this is outside of my expertise, I need a bit more context to understand what this is solving in order to suggest one. It would be helpful to provide a back trace of the current problem, as well as which system/platform it's happening on.

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Mar 16, 2025

In the case of Pythons own fallback implementation

I think what's meant here, is that the addrinfo.h file is only used if a system version or something isn't already included during compilation, presumably the __sgi variable here (whatever that is):
https://github.com/python/cpython/blob/e82c2ca2a59235bc1965b259f4421635861e0470/Modules/socketmodule.c#L415-L427

It doesn't appear to be used on my Debian system:

>>> import socket
>>> socket.AI_MASK
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'socket' has no attribute 'AI_MASK'

So, on my system, this change will have no effect.

@maxbachmann
Copy link
Contributor Author

Python uses getaddrinfo from one of two places:

  1. the OS if it exists + isn't considered as broken (e.g. for mac os < 10.2 they didn't use the system version in the past)
  2. a fallback implementation implemented inside CPython

These implementations don't all support the same flags. Some systems make the list of supported flags public via the AI_MASK parameter (e.g. Android or Free Bsd). Others like Linux or Windows don't have AI_MASK and require the user to know what is supported.

The Python fallback implementation does provide AI_MASK to determine the supported flags + supports a pretty small set of flags. Some of those unsupported options are: AI_NUMERICSERV, AI_ADDRCONFIG and AI_V4MAPPED.
AI_NUMERICSERV is pretty simple to support and so I hope it will be supported in Python 3.14 via python/cpython#114918

AI_ADDRCONFIG on the other hand is more complex and platform specific in how it would have to be implemented and so I don't plan to implement it in Cpython.

With this patch we stop using the flag in case AI_MASK exists and tells us that the flag is unsupported. Without this we would get a gaierror exception for calling getaddrinfo with invalid flags.

We do use this fallback implementation in our PlayStation port of Cpython. With this patch we can use aiohttp on there without any problems.

@maxbachmann
Copy link
Contributor Author

I think what's meant here, is that the addrinfo.h file is only used if a system version or something isn't already included during compilation, presumably the __sgi variable here (whatever that is):

It's bound to HAVE_GETADDRINFO. See https://github.com/python/cpython/blob/e82c2ca2a59235bc1965b259f4421635861e0470/Modules/socketmodule.c#L437-L443 and https://github.com/python/cpython/blob/e82c2ca2a59235bc1965b259f4421635861e0470/Modules/addrinfo.h#L30

@Dreamsorcerer Dreamsorcerer added the backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot label Mar 16, 2025
@bdraco
Copy link
Member

bdraco commented Mar 16, 2025

Thanks for the additional context, I think this is good to go once we get a change fragment

@maxbachmann
Copy link
Contributor Author

Do you have a suggestion for one?

@Dreamsorcerer
Copy link
Member

Do you have a suggestion for one?

Fixed DNS resolution on platforms that don't support AI_ADDRCONFIG (e.g. Android).

Something like that?

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 16, 2025
@maxbachmann
Copy link
Contributor Author

Fixed DNS resolution on platforms that don't support AI_ADDRCONFIG (e.g. Android).

Sorry if that came across in an incorrect way. Android provides AI_MASK which means the branch is taken. However it does support AI_ADDRCONFIG as well. I added the rest of it as changelog entry.

@bdraco bdraco added the backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot label Mar 16, 2025
@bdraco bdraco enabled auto-merge (squash) March 16, 2025 23:06
@bdraco
Copy link
Member

bdraco commented Mar 16, 2025

Thanks @maxbachmann

@bdraco bdraco merged commit e1d2d77 into aio-libs:master Mar 16, 2025
41 checks passed
Copy link
Contributor

patchback bot commented Mar 16, 2025

Backport to 3.11: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.11/e1d2d77cc678e81f7ab7dc687edf8dd7add3ef0b/pr-10542

Backported as #10578

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 16, 2025
The fallback implementation for getaddrinfo in CPython doesn't support
`AI_ADDRCONFIG` and currently fails with a bad flags error. This changes
the implementation to only set the flag if it's part of `AI_MASK`. Since
`AI_MASK` isn't necessarily available either this has to be checked
first.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit e1d2d77)
Copy link
Contributor

patchback bot commented Mar 16, 2025

Backport to 3.12: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.12/e1d2d77cc678e81f7ab7dc687edf8dd7add3ef0b/pr-10542

Backported as #10579

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 16, 2025
The fallback implementation for getaddrinfo in CPython doesn't support
`AI_ADDRCONFIG` and currently fails with a bad flags error. This changes
the implementation to only set the flag if it's part of `AI_MASK`. Since
`AI_MASK` isn't necessarily available either this has to be checked
first.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <[email protected]>
(cherry picked from commit e1d2d77)
bdraco pushed a commit that referenced this pull request Mar 16, 2025
bdraco pushed a commit that referenced this pull request Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.11 Trigger automatic backporting to the 3.11 release branch by Patchback robot backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants