-
Notifications
You must be signed in to change notification settings - Fork 737
http_gateway: wake up poll when we released inflight buffers #28390
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
Changes from all commits
80a7702
1c29e04
c3e08d0
c69cc25
5aa2498
08c33b8
44239f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||||||||||||||||||||||||
| #include <util/generic/yexception.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <util/stream/str.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <util/string/builder.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <util/datetime/base.h> | ||||||||||||||||||||||||||||||||||||||||||||
| #include <yql/essentials/utils/log/log.h> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #include <thread> | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -480,6 +481,8 @@ class TEasyCurlStream : public TEasyCurl { | |||||||||||||||||||||||||||||||||||||||||||
| IHTTPGateway::TOnNewDataPart onNewData, | ||||||||||||||||||||||||||||||||||||||||||||
| IHTTPGateway::TOnDownloadFinish onFinish, | ||||||||||||||||||||||||||||||||||||||||||||
| const ::NMonitoring::TDynamicCounters::TCounterPtr& inflightCounter, | ||||||||||||||||||||||||||||||||||||||||||||
| std::weak_ptr<CURLM> handle, | ||||||||||||||||||||||||||||||||||||||||||||
| size_t threshold, | ||||||||||||||||||||||||||||||||||||||||||||
| const TCurlInitConfig& config = TCurlInitConfig(), | ||||||||||||||||||||||||||||||||||||||||||||
| TDNSGateway<>::TDNSConstCurlListPtr dnsCache = nullptr) | ||||||||||||||||||||||||||||||||||||||||||||
| : TEasyCurl(counter, downloadedBytes, uploadededBytes, url, std::move(headers), EMethod::GET, offset, sizeLimit, 0ULL, std::move(config), std::move(dnsCache)) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -488,6 +491,8 @@ class TEasyCurlStream : public TEasyCurl { | |||||||||||||||||||||||||||||||||||||||||||
| , OnFinish(std::move(onFinish)) | ||||||||||||||||||||||||||||||||||||||||||||
| , Counter(std::make_shared<std::atomic_size_t>(0ULL)) | ||||||||||||||||||||||||||||||||||||||||||||
| , InflightCounter(inflightCounter) | ||||||||||||||||||||||||||||||||||||||||||||
| , Handle(std::move(handle)) | ||||||||||||||||||||||||||||||||||||||||||||
| , Threshold(threshold) | ||||||||||||||||||||||||||||||||||||||||||||
| {} | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| static TPtr Make( | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -502,10 +507,12 @@ class TEasyCurlStream : public TEasyCurl { | |||||||||||||||||||||||||||||||||||||||||||
| IHTTPGateway::TOnNewDataPart onNewData, | ||||||||||||||||||||||||||||||||||||||||||||
| IHTTPGateway::TOnDownloadFinish onFinish, | ||||||||||||||||||||||||||||||||||||||||||||
| const ::NMonitoring::TDynamicCounters::TCounterPtr& inflightCounter, | ||||||||||||||||||||||||||||||||||||||||||||
| std::weak_ptr<CURLM> handle = {}, | ||||||||||||||||||||||||||||||||||||||||||||
| size_t threshold = 0, | ||||||||||||||||||||||||||||||||||||||||||||
| const TCurlInitConfig& config = TCurlInitConfig(), | ||||||||||||||||||||||||||||||||||||||||||||
| TDNSGateway<>::TDNSConstCurlListPtr dnsCache = nullptr) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| return std::make_shared<TEasyCurlStream>(counter, downloadedBytes, uploadededBytes, std::move(url), std::move(headers), offset, sizeLimit, std::move(onStart), std::move(onNewData), std::move(onFinish), inflightCounter, std::move(config), std::move(dnsCache)); | ||||||||||||||||||||||||||||||||||||||||||||
| return std::make_shared<TEasyCurlStream>(counter, downloadedBytes, uploadededBytes, std::move(url), std::move(headers), offset, sizeLimit, std::move(onStart), std::move(onNewData), std::move(onFinish), inflightCounter, handle, threshold, std::move(config), std::move(dnsCache)); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| enum class EAction : i8 { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -565,8 +572,9 @@ class TEasyCurlStream : public TEasyCurl { | |||||||||||||||||||||||||||||||||||||||||||
| size_t Write(void* contents, size_t size, size_t nmemb) final { | ||||||||||||||||||||||||||||||||||||||||||||
| MaybeStart(CURLE_OK); | ||||||||||||||||||||||||||||||||||||||||||||
| const auto realsize = size * nmemb; | ||||||||||||||||||||||||||||||||||||||||||||
| if (!Cancelled) | ||||||||||||||||||||||||||||||||||||||||||||
| OnNewData(IHTTPGateway::TCountedContent(TString(static_cast<char*>(contents), realsize), Counter, InflightCounter)); | ||||||||||||||||||||||||||||||||||||||||||||
| if (!Cancelled) { | ||||||||||||||||||||||||||||||||||||||||||||
| OnNewData(IHTTPGateway::TCountedContent(TString(static_cast<char*>(contents), realsize), Counter, InflightCounter, Handle, Threshold)); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return realsize; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -583,6 +591,8 @@ class TEasyCurlStream : public TEasyCurl { | |||||||||||||||||||||||||||||||||||||||||||
| bool Paused = false; | ||||||||||||||||||||||||||||||||||||||||||||
| bool Cancelled = false; | ||||||||||||||||||||||||||||||||||||||||||||
| long HttpResponseCode = 0L; | ||||||||||||||||||||||||||||||||||||||||||||
| std::weak_ptr<CURLM> Handle; | ||||||||||||||||||||||||||||||||||||||||||||
| size_t Threshold; | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| using TKeyType = std::tuple<TString, size_t, IHTTPGateway::THeaders, TString, IHTTPGateway::TRetryPolicy::TPtr>; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -676,8 +686,8 @@ friend class IHTTPGateway; | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ~THTTPMultiGateway() { | ||||||||||||||||||||||||||||||||||||||||||||
| curl_multi_wakeup(Handle); | ||||||||||||||||||||||||||||||||||||||||||||
| IsStopped = true; | ||||||||||||||||||||||||||||||||||||||||||||
| curl_multi_wakeup(Handle.get()); | ||||||||||||||||||||||||||||||||||||||||||||
| if (Thread.joinable()) { | ||||||||||||||||||||||||||||||||||||||||||||
| Thread.join(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -691,23 +701,26 @@ friend class IHTTPGateway; | |||||||||||||||||||||||||||||||||||||||||||
| TCurlInitConfig InitConfig; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| void InitCurl() { | ||||||||||||||||||||||||||||||||||||||||||||
| // FIXME: NOT SAFE (see man libcurl(3)) | ||||||||||||||||||||||||||||||||||||||||||||
| const CURLcode globalInitResult = curl_global_init(CURL_GLOBAL_ALL); | ||||||||||||||||||||||||||||||||||||||||||||
| if (globalInitResult != CURLE_OK) { | ||||||||||||||||||||||||||||||||||||||||||||
| throw yexception() << "curl_global_init error " << int(globalInitResult) << ": " << curl_easy_strerror(globalInitResult) << Endl; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Handle = curl_multi_init(); | ||||||||||||||||||||||||||||||||||||||||||||
| Handle = std::shared_ptr<CURLM>(curl_multi_init(), [](auto handle) { | ||||||||||||||||||||||||||||||||||||||||||||
| const CURLMcode multiCleanupResult = curl_multi_cleanup(handle); | ||||||||||||||||||||||||||||||||||||||||||||
| if (multiCleanupResult != CURLM_OK) { | ||||||||||||||||||||||||||||||||||||||||||||
| Cerr << "curl_multi_cleanup error " << int(multiCleanupResult) << ": " << curl_multi_strerror(multiCleanupResult) << Endl; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| curl_global_cleanup(); // FIXME: NOT SAFE (see man libcurl(3)) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| curl_global_cleanup(); // FIXME: NOT SAFE (see man libcurl(3)) | |
| // WARNING: curl_global_cleanup() must only be called after all libcurl operations have completed | |
| // and all easy and multi handles have been destroyed. This function is not thread-safe and must not | |
| // be called concurrently with any other libcurl function. Since this cleanup is performed in the | |
| // shared_ptr deleter, it is critical to ensure that no other libcurl handles are in use and no other | |
| // threads are using libcurl when this is called. See man libcurl(3) and the libcurl documentation for details. | |
| curl_global_cleanup(); |
Copilot
AI
Nov 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential race condition here. Between fetch_sub and checking the threshold, another thread could also call fetch_sub, causing multiple wakeup calls even though only one thread actually crossed the threshold. While extra wakeups are generally harmless, consider if this could cause performance issues under high concurrency. If so, consider using compare-and-exchange to ensure only one thread triggers the wakeup when crossing the threshold.
| auto oldSize = Counter->fetch_sub(size()); | |
| if (oldSize >= Threshold && oldSize - size() < Threshold) { | |
| size_t expected = Counter->load(); | |
| bool triggered = false; | |
| while (true) { | |
| if (expected < Threshold || expected - size() >= Threshold) { | |
| // No threshold crossing, just subtract | |
| if (Counter->compare_exchange_weak(expected, expected - size())) { | |
| break; | |
| } | |
| // compare_exchange_weak updates expected, so loop | |
| } else { | |
| // Threshold crossing, only one thread should trigger | |
| if (Counter->compare_exchange_weak(expected, expected - size())) { | |
| triggered = true; | |
| break; | |
| } | |
| // compare_exchange_weak updates expected, so loop | |
| } | |
| } | |
| if (triggered) { |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The threshold check logic has a potential race condition. Between checking oldSize >= Threshold and evaluating oldSize - size() < Threshold, another thread could modify the Counter, making the condition incorrect. This is compounded by the fact that oldSize is the value after the subtraction (fetch_sub returns the previous value), so oldSize - size() is actually the new value after this operation.
However, there's a more fundamental issue: if Threshold is 0 (the default), the condition oldSize >= Threshold && oldSize - size() < Threshold would trigger on every destruction where size() > 0, potentially causing excessive wake-up calls. Consider adding a check: if (Threshold > 0 && oldSize >= Threshold && oldSize - size() < Threshold)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counter is sum of all size(), hence both should not be issue.
Anyway, rare spurious (or even missed) wake-up should not be issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FIXME comment indicates a known safety issue with
curl_global_initbut doesn't explain what the problem is or how it should be fixed. Consider documenting thatcurl_global_initis not thread-safe and should be called only once before any threads are started, or reference the specific safety concerns from the libcurl documentation.