RIP pthreadcancel Posted on September 13, 2025 Background The author previously added pthreadcancel support in libcurl (introduced in curl 8.16.0) to handle DNS name resolution more efficiently: getaddrinfo() is a blocking POSIX call used for DNS resolution. To avoid blocking the main thread, libcurl calls getaddrinfo() in a separate pthread. Managing these threads is challenging: pthreadjoin() blocks, causing stalls. pthreaddetach() lets threads linger, causing resource buildup. pthreadcancel() was used to interrupt and clean up these threads, theoretically solving the issue. The Problem: Memory Leaks on Cancellation After release, users reported (#18532) that cancelled pthreads leaked memory. Investigation Findings: The glibc implementation of getaddrinfo() reads /etc/gai.conf to sort resolved addresses. This involves opening files (using fopen()), which are pthread cancellation points. Cancelling a getaddrinfo() pthread while reading /etc/gai.conf causes allocated memory to leak. Repeated calls to getaddrinfo() cause the process to potentially leak repeatedly because /etc/gai.conf is read on each resolution with multiple addresses. There may be other undiscovered points where cancellation leaks memory. glibc is not designed to handle cancellations safely without leaks. Decision: Removing pthreadcancel Memory leaks are unacceptable especially in repeated DNS resolution performed by libcurl. The pthreadcancel strategy is abandoned, removing it in pull request #18540. The project accepts that it may have to wait on long-running getaddrinfo() calls without cancellation. Users can use asynchronous DNS resolution with c-ares instead, but it doesn’t cover all glibc capabilities. Summary Using pthreadcancel() with getaddrinfo() leads to hard-to-fix memory leaks due to internal file I/O cancellation. Libcurl has removed pthreadcancel() support to maintain stability and prevent memory issues. DNS remains a complicated area to handle efficiently and safely in applications. --- References POSIX pthread cancellation points glibc getaddrinfo source investigation /etc/gai.conf man page GitHub issue on memory leak: #18532 Pull request removing pthreadcancel: #18540