Skip to content

Keep curl teardown from hanging on stalled DNS - #1562

Open
bmehta001 wants to merge 7 commits into
microsoft:mainfrom
bmehta001:curl-nonblocking-dns-teardown
Open

bmehta001 wants to merge 7 commits into
microsoft:mainfrom
bmehta001:curl-nonblocking-dns-teardown

Conversation

@bmehta001

@bmehta001 bmehta001 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The Linux curl transport could stall host shutdown when DNS is unreachable. curl_easy_perform() does not run its progress callback during name resolution, and resolver cleanup may wait for a worker blocked in getaddrinfo().

Fix

  • Query the loaded libcurl version independently of the build headers, so runtime quick-exit support is detected even with older headers.
  • Establish libcurl lifetime protection on the operation-creation thread before enabling quick-exit cleanup. The main executable is recognized by its loaded mapping, independent of its path or the current working directory; other modules are pinned with RTLD_NODELETE. If an unloadable module cannot be pinned, quick-exit remains disabled.
  • Keep curl_multi_poll and curl_multi_wait optional at load time when headers are newer than runtime libcurl. Use whichever bounded multi wait API is available, and fall back to curl_easy_perform() only if neither is present.
  • Keep the modern multi-handle transfer path's cancellation polling, and enable CURLMOPT_QUICK_EXIT only when supported and protected. This covers resolver workers detached after both failed and successful transfers; legacy easy transfers are protected too.
  • Set CURLOPT_NOPROXY on the stalled-DNS regression test's easy handle so proxy environment settings cannot bypass the intercepted hostname.

Unload tradeoff: When quick-exit is used, libcurl's module remains mapped for the process lifetime, including after successful transfers, because idle resolver workers may also be detached. This avoids both loader-lock deadlocks from worker-thread pinning and use-after-unload crashes.

Tests

  • HttpClientCurlDnsStallTests.DestroyingClientDuringStalledResolutionDoesNotHang intercepts getaddrinfo() for dns-stall.invalid only, confirms client teardown completes within 3 seconds, and checks for one terminal response. It skips on runtimes without the required async resolver/quick-exit support.
  • HttpClientCurlModuleTests.IdentifiesMainExecutableAfterWorkingDirectoryChanges checks main-image recognition after chdir().
  • Linux/FETCH-only isolated host/module tests verify stalled-resolver release after dlclose and module retention after a successful transfer leaves a resolver worker idle. The success case routes a reserved .invalid hostname through the test resolver to loopback and requires the resolver to return before teardown.
  • Linux/system-only load tests use version-matched libcurl.so.4 shims to omit curl_multi_poll, then both optional wait symbols, and verify the SDK module still loads with RTLD_NOW.

Validation (WSL Ubuntu 24.04)

Run Result
HttpClientCurl*, system libcurl 8.5 38 passed
Full UnitTests, system libcurl 8.5 731 passed
HttpClientCurl*, fetched libcurl 8.21 38 passed
Full UnitTests, fetched libcurl 8.21 731 passed
Fetched-curl stalled-DNS and successful-transfer unload regressions 2 passed
System-curl load tests with poll absent and both optional symbols absent 2 passed
Pinned misspell workflow and git diff --check Passed

When the collector's DNS is unreachable, teardown of the curl transport
could block host process exit for the system resolver timeout (tens of
seconds). curl_easy_perform() does not run the progress callback while a
name is resolving, so Abort() waited for the 5s connect timeout, and
curl_easy_cleanup()/curl_multi_cleanup() then joined the resolver thread
still blocked in getaddrinfo().

Set CURLOPT_QUICK_EXIT on every handle, and run modern-libcurl transfers
on a private multi handle with CURLMOPT_QUICK_EXIT, polling the abort flag
every 100ms. Option ids are passed numerically so a runtime libcurl newer
than the build headers still receives them. A Linux unit test interposes
getaddrinfo() to stall deterministically; it hangs 60s without this change.

Files changed:
- lib/http/HttpClient_Curl.hpp
- tests/unittests/CMakeLists.txt
- tests/unittests/HttpClientCurlTests.cpp

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ff620832-ec83-4315-b3e0-6f5d825d45e1
@bmehta001
bmehta001 requested a review from a team as a code owner November 19, 2026 00:01
Include the integer type dependency in ICdsFactory.hpp and avoid the racy mkdir implementation in build-runner-tests.py.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: ea7f5226-1c17-476f-abaa-90cfa4b32e3c
CURLOPT_QUICK_EXIT lets teardown skip joining a resolver thread blocked in
getaddrinfo(). That thread later returns into libcurl, so unloading the
module that holds libcurl first (a shared libcurl losing its last
reference, or a library that links it statically) would crash.

When an aborted or timed-out transfer never finished name resolution,
pin libcurl's module once with dlopen(RTLD_NOLOAD | RTLD_NODELETE). Hosts
that never stall can still unload the module; the rare stalled case leaks
the mapping instead of crashing. The DNS-stall test now checks the pin.

Files changed:
- lib/http/HttpClient_Curl.hpp
- tests/unittests/HttpClientCurlTests.cpp

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 75770221-1676-4767-bba3-18994bc39f5f

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved resolver-lifetime and loader-lock issues can cause crashes or deadlocks during libcurl module unload.

4 open issues
What changed in this PR

This PR aims to prevent stalled DNS lookups from blocking curl transport teardown while protecting libcurl from unsafe unloading.

Changes:

  • Adds abortable multi-handle transfers, quick-exit options, and module pinning.
  • Adds a Linux stalled-DNS regression test and loader-library linkage.
  • Hardens build-runner tests and adds a missing integer-type include.
File Description
tests/​unittests/​HttpClientCurlTests.cpp Tests teardown during stalled DNS resolution.
tests/​unittests/​CMakeLists.txt Links Linux tests with dynamic-loader support.
tests/​build-runner-tests.py Makes concurrent-child selection independent of mkdir.
lib/​include/​public/​ICdsFactory.hpp Includes integer-type definitions explicitly.
lib/​http/​HttpClient_Curl.hpp Adds cancellation polling, quick-exit cleanup, and module pinning.

Review effort: Balanced

Comment thread lib/http/HttpClient_Curl.hpp Resolved
Comment thread lib/http/HttpClient_Curl.hpp Resolved
Comment thread lib/http/HttpClient_Curl.hpp Resolved
Comment thread tests/unittests/HttpClientCurlTests.cpp Resolved
r4811531007: pin before worker startup to avoid loader-lock deadlocks during teardown. Verified at lib/http/HttpClient_Curl.hpp:119,378.

r4811531023: detect runtime support independently of header version and protect legacy easy transfers. Verified at lib/http/HttpClient_Curl.hpp:365-389.

r4811531035: gate multi quick-exit on established module lifetime protection, including successful cleanup. Verified at lib/http/HttpClient_Curl.hpp:387,1067-1078.

r4811531043: disable inherited proxies so the DNS regression test reaches its stalled hostname. Verified at tests/unittests/HttpClientCurlTests.cpp:1308-1350.

Files changed: lib/http/HttpClient_Curl.hpp; tests/unittests/HttpClientCurlTests.cpp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b759695a-3b22-4233-8d5e-fa894dcdb031

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Relative executable paths can incorrectly disable quick-exit and leave teardown blocked by stalled DNS, while module-unload and successful-transfer cleanup regressions remain untested.

2 open issues
4 resolved since last review

Review effort: Balanced

Comment thread lib/http/HttpClient_Curl.hpp Resolved
Comment thread lib/http/HttpClient_Curl.hpp Resolved
4813699957: identify the main executable by its loaded mapping instead of a current-directory-sensitive path, with a chdir regression.

4813699987: add isolated fetched-curl unload regressions for stalled resolver release after dlclose and idle workers after a successful transfer.

Files changed: lib/http/HttpClient_Curl.hpp; tests/unittests/CMakeLists.txt; tests/unittests/HttpClientCurlTests.cpp; tests/unittests/curl-module-unload-module.cpp; tests/unittests/curl-module-unload-test-api.hpp; tests/unittests/curl-module-unload-test.cpp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b759695a-3b22-4233-8d5e-fa894dcdb031

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The direct curl_multi_poll dependency prevents loading on libcurl runtimes older than 7.66 before the legacy fallback can be selected.

3 open issues
1 resolved since last review

Review effort: Balanced

Comment thread lib/http/HttpClient_Curl.hpp Resolved
4813831962: keep curl_multi_poll and curl_multi_wait optional at load time; use bounded multi polling when available and retain the legacy easy path otherwise.

4813831981 and 4813699987: route a reserved test hostname through the controlled resolver to loopback and require its return before teardown, ensuring the successful transfer leaves a real resolver worker idle.

Files changed: lib/http/HttpClient_Curl.hpp; tests/unittests/curl-module-unload-test.cpp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b759695a-3b22-4233-8d5e-fa894dcdb031

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The load-time compatibility fallback lacks regression coverage for runtimes missing optional symbols, leaving accidental strong references that could prevent SDK loading undetected.

2 open issues
2 resolved since last review

Review effort: Balanced

Comment thread lib/http/HttpClient_Curl.hpp Resolved
4813891935: add system-libcurl shims that omit curl_multi_poll, then omit both poll and wait. The load-only host verifies the selected shim is mapped and dlopens the SDK module with RTLD_NOW, catching strong references before runtime fallback can run.

Files changed: tests/unittests/CMakeLists.txt; tests/unittests/curl-optional-symbol-load-module.cpp; tests/unittests/curl-optional-symbol-load-test.cpp; tests/unittests/curl-runtime-shim.cpp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b759695a-3b22-4233-8d5e-fa894dcdb031

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because an unpublished finding indicates the curl_multi_wait fallback can busy-loop without descriptors and consume a CPU core.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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