Skip to content

Commit 26bf212

Browse files
authored
curl minutiae (#65)
* unlock during HTTP response handler * appease the lambda capture warning
1 parent 9c9a1ba commit 26bf212

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/datadog/curl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class CurlImpl {
208208
};
209209

210210
void run();
211-
void handle_message(const CURLMsg &);
211+
void handle_message(const CURLMsg &, std::unique_lock<std::mutex> &);
212212
CURLcode log_on_error(CURLcode result);
213213
CURLMcode log_on_error(CURLMcode result);
214214

@@ -476,7 +476,7 @@ void CurlImpl::run() {
476476
// us to handle. Handle any pending messages.
477477
while ((message = curl_.multi_info_read(multi_handle_,
478478
&num_messages_remaining))) {
479-
handle_message(*message);
479+
handle_message(*message, lock);
480480
}
481481

482482
const int max_wait_milliseconds = 10 * 1000;
@@ -514,7 +514,8 @@ void CurlImpl::run() {
514514
curl_.global_cleanup();
515515
}
516516

517-
void CurlImpl::handle_message(const CURLMsg &message) {
517+
void CurlImpl::handle_message(const CURLMsg &message,
518+
std::unique_lock<std::mutex> &lock) {
518519
if (message.msg != CURLMSG_DONE) {
519520
return;
520521
}
@@ -536,17 +537,21 @@ void CurlImpl::handle_message(const CURLMsg &message) {
536537
error_message += curl_.easy_strerror(result);
537538
error_message += "): ";
538539
error_message += request.error_buffer;
540+
lock.unlock();
539541
request.on_error(
540542
Error{Error::CURL_REQUEST_FAILURE, std::move(error_message)});
543+
lock.lock();
541544
} else {
542545
long status;
543546
if (log_on_error(curl_.easy_getinfo_response_code(request_handle,
544547
&status)) != CURLE_OK) {
545548
status = -1;
546549
}
547550
HeaderReader reader(&request.response_headers_lower);
551+
lock.unlock();
548552
request.on_response(static_cast<int>(status), reader,
549553
std::move(request.response_body));
554+
lock.lock();
550555
}
551556

552557
log_on_error(curl_.multi_remove_handle(multi_handle_, request_handle));

src/datadog/http_client.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class HTTPClient {
4343
// a response is delivered (even if that response contains an error HTTP
4444
// response status). Invoke the specified `on_error` if an error occurs
4545
// outside of HTTP, such as a connection failure. If an error occurs while
46-
// preparing the request, return an `Error`.
46+
// preparing the request, return an `Error`. The behavior is undefined if
47+
// either of `on_response` or `on_error` throws an exception.
4748
virtual Expected<void> post(const URL& url, HeadersSetter set_headers,
4849
std::string body, ResponseHandler on_response,
4950
ErrorHandler on_error) = 0;

test/test_tracer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ TEST_CASE("128-bit trace IDs") {
10171017
// Use a clock that always returns a hard-coded `TimePoint`.
10181018
// May 6, 2010 14:45:13 America/New_York
10191019
const std::time_t flash_crash = 1273171513;
1020-
const Clock clock = [flash_crash]() {
1020+
const Clock clock = []() {
10211021
TimePoint result;
10221022
result.wall = std::chrono::system_clock::from_time_t(flash_crash);
10231023
return result;

test/test_tracer_telemetry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using namespace datadog::tracing;
1414

1515
TEST_CASE("Tracer telemetry") {
1616
const std::time_t mock_time = 1672484400;
17-
const Clock clock = [mock_time]() {
17+
const Clock clock = []() {
1818
TimePoint result;
1919
result.wall = std::chrono::system_clock::from_time_t(mock_time);
2020
return result;

0 commit comments

Comments
 (0)