Skip to content

Conversation

@yumkam
Copy link
Collaborator

@yumkam yumkam commented Nov 7, 2025

There can be some spurious/double wakeup, but this should not matter much

Changelog entry

...

Changelog category

  • Not for changelog (changelog entry is not required)

Description for reviewers

...

There can be some spurious/double wakeup, but this should not matter much
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

🟢 2025-11-07 12:28:49 UTC The validation of the Pull Request description is successful.

@yumkam yumkam marked this pull request as ready for review November 7, 2025 12:28
@yumkam yumkam requested a review from a team as a code owner November 7, 2025 12:28
Copilot AI review requested due to automatic review settings November 7, 2025 12:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the HTTP gateway implementation to improve resource management and implement a wake-up mechanism for efficient buffer handling. The key change is converting the CURL multi-handle from a raw pointer to a std::shared_ptr to enable proper lifetime tracking.

Key Changes:

  • Convert CURLM* handle to std::shared_ptr<CURLM> with custom deleter for automatic cleanup
  • Add handle and threshold parameters to TCountedContent to trigger wake-ups when buffered data falls below threshold
  • Implement automatic wake-up logic in TCountedContent destructor and Extract() method when buffer size crosses threshold

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
ydb/library/yql/providers/common/http_gateway/yql_http_gateway.h Updated TCountedContent constructor signature and added Handle and Threshold member variables
ydb/library/yql/providers/common/http_gateway/yql_http_gateway.cpp Converted CURL multi-handle to shared_ptr, updated all handle usage sites to call .get(), implemented wake-up logic in TCountedContent, and added busy-wait synchronization in cleanup

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}
auto weakHandle = std::weak_ptr<CURLM>(Handle);
Handle.reset();
while (!weakHandle.expired()) {} // short busy-wait in case of TCountedContent
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

This busy-wait loop is problematic. It will consume 100% CPU while waiting for all TCountedContent instances to be destroyed. Consider using a more efficient synchronization mechanism like a condition variable, or at least add a small sleep/yield inside the loop to reduce CPU usage.

auto weakHandle = std::weak_ptr<CURLM>(Handle);
Handle.reset();
std::this_thread::yield();
while (!weakHandle.expired()) {
    std::this_thread::sleep_for(std::chrono::microseconds(100));
}
Suggested change
while (!weakHandle.expired()) {} // short busy-wait in case of TCountedContent
while (!weakHandle.expired()) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
}

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In general, yes, busy-loops are bad; however, in this case it is expected to happen only on destruction (already rare!), only in cases when there are concurrently executed move-out or destruction from TCountedContent, and only for very short period (when curl_multi_wakeup is executed)
Hence, comparing required expenses and complications from better synchronization primitives, the choice is pretty obvious.

Comment on lines +1072 to +1076
if (oldSize >= Threshold && oldSize - size() < Threshold) {
if (auto handle = Handle.lock()) {
curl_multi_wakeup(handle.get());
}
}
Copy link

Copilot AI Nov 7, 2025

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)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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.

Comment on lines 1084 to 1088
if (oldSize >= Threshold && oldSize - size() < Threshold) {
if (auto handle = Handle.lock()) {
curl_multi_wakeup(handle.get());
}
}
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

Same issue as in the destructor: if Threshold is 0 (the default), the condition oldSize >= Threshold && oldSize - size() < Threshold would trigger on every extraction where size() > 0, potentially causing excessive wake-up calls. Consider adding a check: if (Threshold > 0 && oldSize >= Threshold && oldSize - size() < Threshold)

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 12:32:03 UTC Pre-commit check linux-x86_64-relwithdebinfo for 07a90f5 has started.
2025-11-07 12:32:19 UTC Artifacts will be uploaded here
2025-11-07 12:34:36 UTC ya make is running...
2025-11-07 13:25:44 UTC Check cancelled

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 12:32:10 UTC Pre-commit check linux-x86_64-release-asan for 07a90f5 has started.
2025-11-07 12:32:26 UTC Artifacts will be uploaded here
2025-11-07 12:34:40 UTC ya make is running...
2025-11-07 13:25:43 UTC Check cancelled

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 13:29:32 UTC Pre-commit check linux-x86_64-relwithdebinfo for 2792ab0 has started.
2025-11-07 13:29:50 UTC Artifacts will be uploaded here
2025-11-07 13:32:07 UTC ya make is running...
🔴 2025-11-07 13:34:07 UTC Build failed, see the logs. Also see fail summary

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 13:29:54 UTC Pre-commit check linux-x86_64-release-asan for 2792ab0 has started.
2025-11-07 13:30:12 UTC Artifacts will be uploaded here
2025-11-07 13:32:32 UTC ya make is running...
🔴 2025-11-07 13:34:41 UTC Build failed, see the logs. Also see fail summary

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 13:45:10 UTC Pre-commit check linux-x86_64-release-asan for f055b54 has started.
2025-11-07 13:47:19 UTC Artifacts will be uploaded here
2025-11-07 13:49:31 UTC ya make is running...
🟡 2025-11-07 16:01:35 UTC Some tests failed, follow the links below. This fail is not in blocking policy yet

Ya make output | Test bloat

TESTS PASSED ERRORS FAILED SKIPPED MUTED?
17596 17072 0 211 281 32

🟢 2025-11-07 16:01:49 UTC Build successful.
🟢 2025-11-07 16:02:16 UTC ydbd size 3.8 GiB changed* by +11.5 KiB, which is < 100.0 KiB vs main: OK

ydbd size dash main: 4579632 merge: f055b54 diff diff %
ydbd size 4 072 955 080 Bytes 4 072 966 832 Bytes +11.5 KiB +0.000%
ydbd stripped size 1 511 824 008 Bytes 1 511 829 928 Bytes +5.8 KiB +0.000%

*please be aware that the difference is based on comparing your commit and the last completed build from the post-commit, check comparation

@github-actions
Copy link

github-actions bot commented Nov 7, 2025

2025-11-07 13:45:22 UTC Pre-commit check linux-x86_64-relwithdebinfo for f055b54 has started.
2025-11-07 13:46:57 UTC Artifacts will be uploaded here
2025-11-07 13:49:09 UTC ya make is running...
🟡 2025-11-07 15:31:39 UTC Some tests failed, follow the links below. Going to retry failed tests...

Ya make output | Test bloat

TESTS PASSED ERRORS FAILED SKIPPED MUTED?
41208 38377 0 3 2801 27

2025-11-07 15:32:00 UTC ya make is running... (failed tests rerun, try 2)
🟢 2025-11-07 15:41:55 UTC Tests successful.

Ya make output | Test bloat | Test bloat

TESTS PASSED ERRORS FAILED SKIPPED MUTED?
73 (only retried tests) 58 0 0 0 15

🟢 2025-11-07 15:42:05 UTC Build successful.
🟢 2025-11-07 15:42:26 UTC ydbd size 2.3 GiB changed* by +6.3 KiB, which is < 100.0 KiB vs main: OK

ydbd size dash main: 4579632 merge: f055b54 diff diff %
ydbd size 2 431 787 616 Bytes 2 431 794 040 Bytes +6.3 KiB +0.000%
ydbd stripped size 516 526 256 Bytes 516 527 344 Bytes +1.1 KiB +0.000%

*please be aware that the difference is based on comparing your commit and the last completed build from the post-commit, check comparation

@yumkam yumkam requested a review from GrigoriyPA November 7, 2025 15:27
auto weakHandle = std::weak_ptr<CURLM>(Handle);
Handle.reset();
while (!weakHandle.expired()) { // short busy-wait in unlikely case of collision with TCountedContent
Sleep(TDuration::MicroSeconds(1));
Copy link
Collaborator

Choose a reason for hiding this comment

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

путь к зависаниям. давай таймаут сделаем

Copy link
Collaborator Author

@yumkam yumkam Nov 8, 2025

Choose a reason for hiding this comment

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

Последствия досрочного выхода из этого цикла хуже, чем бесконечное ожидание. И тут никогда не может быть таймаут, обработка таймаута создаст ложное впечатление, что он возможен

@yumkam yumkam requested a review from uzhastik November 8, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants