Skip to content

Update c-ares to 1.29.0 to add reinit support to Channel#219

Merged
saghul merged 17 commits intosaghul:masterfrom
bdraco:reinit
Apr 30, 2025
Merged

Update c-ares to 1.29.0 to add reinit support to Channel#219
saghul merged 17 commits intosaghul:masterfrom
bdraco:reinit

Conversation

@bdraco
Copy link
Collaborator

@bdraco bdraco commented Apr 29, 2025

to solve aio-libs/aiodns#124

Updates the submodule to tag v1.29.0. Note that v1.22.0 is the version that added reinit: https://c-ares.org/docs/ares_reinit.html but it also introduced some regressions so I went with v1.29.0 as it also fixes GHSA-mg26-v6qh-x48q and has some fixes for reinit

We could go newer as well but #183 seemed to hold back to an older version so I only went as far as what was needed to get reinit without introducing regressions and fixing CVEs. Also some of the tests started failing that looked like they might be real regressions when I went to v1.30.0 so I stopped at v1.29.0

For testing with homebrew I also did
env PYCARES_USE_SYSTEM_LIB=1 LDFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" CFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" pip install .

@bdraco bdraco changed the title Add reinit to Channel Update c-area to 1.22.0 to add reinit suport to Channel Apr 29, 2025
@bdraco bdraco changed the title Update c-area to 1.22.0 to add reinit suport to Channel Update c-area to 1.22.0 to add reinit support to Channel Apr 29, 2025
@bdraco
Copy link
Collaborator Author

bdraco commented Apr 29, 2025

probably should go to 1.24.0 since it fixes a regression introduced in 1.22.0

@bdraco bdraco changed the title Update c-area to 1.22.0 to add reinit support to Channel Update c-area to 1.30.0 to add reinit support to Channel Apr 29, 2025
@bdraco bdraco changed the title Update c-area to 1.30.0 to add reinit support to Channel Update c-areas to 1.30.0 to add reinit support to Channel Apr 29, 2025
@bdraco bdraco changed the title Update c-areas to 1.30.0 to add reinit support to Channel Update c-ares to 1.30.0 to add reinit support to Channel Apr 29, 2025
@bdraco

This comment was marked as outdated.

@bdraco bdraco changed the title Update c-ares to 1.30.0 to add reinit support to Channel Update c-ares to 1.29.0 to add reinit support to Channel Apr 29, 2025
@bdraco bdraco marked this pull request as ready for review April 29, 2025 23:49
@bdraco
Copy link
Collaborator Author

bdraco commented Apr 29, 2025

o there are docs for channel.. need to update them

@saghul saghul merged commit f99e32c into saghul:master Apr 30, 2025
26 checks passed
@saghul
Copy link
Owner

saghul commented Apr 30, 2025

Great work!

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Thanks. I'll work on a PR for aiodns once this makes it into a release

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

It looks like we might not have to do it manually if we turn on the event thread c-ares/c-ares#759

@bdraco bdraco deleted the reinit branch April 30, 2025 06:04
@saghul
Copy link
Owner

saghul commented Apr 30, 2025

If the event thread can be turned on with some flag, adding support for it would be good.

I wouldn't make it the default though.

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

I agree. I'm not sure it will work with asyncio anyways.

Researching that today

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Its explicitly not compatible with ARES_OPT_SOCK_STATE_CB so it looks like we have to do it manually

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Looks like its using inotify.
https://github.com/c-ares/c-ares/pull/759/files#diff-28db3f50243f279af6a0756548de982396815e487d33120e935702549711fd3bR109

aiodns could implement it using asyncinotify (if installed only) ...similar to how we do usb watching in https://github.com/Bluetooth-Devices/aiousbwatcher/blob/main/src/aiousbwatcher/impl.py

@saghul
Copy link
Owner

saghul commented Apr 30, 2025

Its explicitly not compatible with ARES_OPT_SOCK_STATE_CB so it looks like we have to do it manually

I suspected as much, since it uses threading...

aiodns could implement it using asyncinotify (if installed only) ...similar to how we do usb watching in https://github.com/Bluetooth-Devices/aiousbwatcher/blob/main/src/aiousbwatcher/impl.py

IMHO that's kind a big lift for a simple DNS library. You'd also need to port the implementation to Windows and macOS. I'd expose the reinit method and let users handle it.

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Its explicitly not compatible with ARES_OPT_SOCK_STATE_CB so it looks like we have to do it manually

I suspected as much, since it uses threading...

aiodns could implement it using asyncinotify (if installed only) ...similar to how we do usb watching in Bluetooth-Devices/aiousbwatcher@main/src/aiousbwatcher/impl.py

IMHO that's kind a big lift for a simple DNS library. You'd also need to port the implementation to Windows and macOS. I'd expose the reinit method and let users handle it.

Another option would be to create a new lib once we have reinit in aiodns.DNSResolver.

Maybe aiodns-auto-reinit which subclasses aiodns.DNSResolver and implements the automatic reinit. We currently don't install aiodns on windows with aiohttp because of the SelectorEventLoop requirement so we likely wouldn't need to implement windows support (could of course be added later if someone really wants it)

@saghul
Copy link
Owner

saghul commented Apr 30, 2025

Another option could be to explore using the builtin threaded mode, and use run_in_executor to make sure our callbacks gets called in the given thread.

That's a bit more of a question for aiodns, but this lib would need to at least make it possible to enable that mode.

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

I guess it would work with call soon thread safe, however, running in another thread would probably not be so great for performance since we now have the context switching...

@saghul
Copy link
Owner

saghul commented Apr 30, 2025

It kinda already works that way for those not using pycares, they will end up running getaddrinfo on a thread...

Someone with enough motivation could benchmark it ;-)

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

It kinda already works that way for those not using pycares, they will end up running getaddrinfo on a thread...

Someone with enough motivation could benchmark it ;-)

At least with Home Assistant, we know that's much slower because of the cost the context switch, which is why we use the AsyncResolver, and thus aiodns in the first place. We also had a problem with a thundering heard at startup where the executor would get swamped because of all the requests.

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Looking at the example https://c-ares.org/docs.html#examples .. the callbacks might be thread-safe so even if they run in another thread the callback might still be ok to consume in the event loop. need to dig some more

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

So it looks like we can make the calls from the event loop thread and that looks like it's thread-safe but the callbacks will be delivered in another thread so we would need to deliver it back to the event loop future with loop.call_soon_threadsafe. While not great that is still relatively efficient as we don't have all the python overhead to wrap the future running in another thread.

@saghul
Copy link
Owner

saghul commented Apr 30, 2025

Sounds like a plan! Would you like to add support for the threaded mode here, then I can cut a release and you can experiment with aiodns?

I think whatever happens in aiodns the threaded mode is a nice addition to this low(er) level library.

@bdraco
Copy link
Collaborator Author

bdraco commented Apr 30, 2025

Sounds good. About to get on a flight back to the US from FRA so will probably be later this week that I work it out (once I recover from the jet lag)

@saghul
Copy link
Owner

saghul commented Apr 30, 2025

Safe flight and thanks for your work!

@bdraco
Copy link
Collaborator Author

bdraco commented May 1, 2025

implemented in #220

aiodns PR is aio-libs/aiodns#145

used strace to verify auto reloads, so it should solve aio-libs/aiodns#124

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.

2 participants