My project uses multiple threads to execute the following logic block, and then it crashed. The crash occurred at the line:
CURLcode re_code = curl_easy_perform(curl);
The stack trace is as follows:
0 libpthread.so.0 + 0xc0f8
1 libcrypto.so.1.1 + 0x181228
2 libcrypto.so.1.1 + 0x181228
3 libcrypto.so.1.1 + 0x1129d8
4 libcrypto.so.1.1 + 0x112be4
5 libcurl.so.4!ossl_strerror.constprop.16 + 0x1c
6 libcurl.so.4!servercert + 0xe3c
7 libcurl.so.4!ossl_connect_common + 0x268
8 libcurl.so.4!Curl_ssl_connect_nonblocking + 0x94
9 libcurl.so.4!https_connecting + 0x20
10 libcurl.so.4!multi_runsingle + 0x310
11 libcurl.so.4!curl_multi_perform + 0x98
12 libcurl.so.4!curl_easy_perform + 0x160
Here is the code:
{
int nret = 0;
CURL *curl = nullptr;
struct curl_slist *header = nullptr;
try
{
curl = curl_easy_init();
if (nullptr == curl)
{
return -1;
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
if (is_enable_proxy_)
{
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_url_.c_str());
}
// header
std::string request_header;
header = curl_slist_append(nullptr, "");
for (size_t i = 0; i < requestHeaders.size(); i++)
{
curl_slist_append(header, requestHeaders[i].c_str());
request_header += requestHeaders[i];
request_header += "n";
}
if (nullptr == header)
{
return -1;
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION,
HttpUtils::sslFunction);
// body
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
HttpUtils::OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strResponse);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseHeader);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, time_out_);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out_);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
CURLcode re_code = curl_easy_perform(curl);
// some handle ...
}
catch (const std::exception &e)
{
if (nullptr != header)
{
curl_slist_free_all(header);
}
if (nullptr != curl)
{
curl_easy_cleanup(curl);
}
// some log ...
}
catch (...)
{
if (nullptr != header)
{
curl_slist_free_all(header);
}
if (nullptr != curl)
{
curl_easy_cleanup(curl);
}
// some log ...
}
return -1;
}
I’ve done some research, and it seems to be a potential thread safety issue related to OpenSSL, but I’m not sure if that’s the case. Can anyone help me analyze this and figure out how to resolve it? I’m not very familiar with curl; I only started studying it a couple of days ago.
By the way, according to the logs, the program ran for several hours, and before the crash, the network requests were consistently in a timeout state.