jart a few seconds ago

[delayed]

Someone 7 minutes ago

> Then it needs to sort them if there is more than one address. And in order to do that it needs to read /etc/gai.conf

I don’t see why glibc would have to do that inside a call to getaddrinfo. can’t it do that once at library initialization? If it has to react to changes to that file while a process is running, couldn’t it have a separate thread for polling that file for changes, or use inotify for a separate thread to be called when it changes? Swapping in the new config atomically might be problematic, but I would think that is solvable.

Even ignoring the issue mentioned it seems wasteful to open, parse, and close that file repeatedly.

pizlonator an hour ago

At first I wondered if musl does it better, so I checked, and the version I have disables cancellation in the guts of `getaddrinfo`.

I've always thought APIs like `pthread_cancel` are too nasty to use. Glad to see well documented evidence of my crank opinion

rwmj 32 minutes ago

Netscape used to start a new thread (or maybe it was a subprocess?) to handle DNS lookups, because the API at the time (gethostbyname) was blocking. It's kind of amazing that we're 30 years on and this is still a problem.

  • nly 26 minutes ago

    If you want DNS resolution to obey user/system preferences then you need to use the system provided API

    • rwmj 22 minutes ago

      For sure! The only problem is there should be a non-blocking system-provided API and there isn't.

      • foota 15 minutes ago

        System provided is maybe a strange word to use here since getaddrinfo is a libc function, not a system call.

        • rwmj 13 minutes ago

          POSIX as the system, of course.

  • silon42 26 minutes ago

    As long as broken APIs exist, they will be problematic... they really should be deprecated.

    Calling a separate (non-cancellable) thread to perform the lookup sounds a like viable solution...

nly 24 minutes ago

Why is running the DNS resolution thread a problem? It should be dequeuing resolution requests and pushing responses and sleeping when there is nothing to do

When someone kills off the curl context surely you simply set a suicide flag on the thread and wake it up so it can be joined.

  • foota 18 minutes ago

    The thread started sounds like it's single use, not a thread handling requests in a loop. Anyway, a single thread handling requests in a loop would serialize these DNS lookups which if they're hanging would be problematic.

  • rwmj 20 minutes ago

    One problem may be that fork() kills background threads, so now any program that uses libcurl + fork has to have a new API to restart the DNS thread (or use posix_atfork which is a big PITA), and that might break existing programs using curl.

throwaway81523 19 minutes ago

There might be a way to getaddrinfo asynchronously with io_uring by now. Otherwise just call the synchronous version in another thread and let it time out so the thread exits normally, right? Why bother with pthread_cancel?

Aardwolf 24 minutes ago

Maybe this is naive, but could there just be some amount of worker threads that run forever, wait for and take jobs when needed, and message when the jobs are done? Don't need to be canceled, don't block

  • danappelxx 8 minutes ago

    If the DNS resolution call blocks the thread, then you need N worker threads to perform N DNS calls. Threads aren’t free, so this is suboptimal. OTOH some thread pools e.g. libdispatch on Apple operating systems will spawn new threads on demand to prevent starvation, so this _can_ be viable. Though of course this can lead to thread explosion which may be even more problematic depending on the use case. In libcurl’s situation, spawning a million threads is probably even worse than a memory leak, which is worse than long timeouts.

    In general, what you really want is for the API call to be nonblocking so you’re not forced to burn a thread.