CURLOPT_NOSIGNAL

本文详细解释了在多线程环境下未设置CURLOPT_NOSIGNAL选项可能导致的crash问题,并介绍了使用c-ares的libcurl版本实现异步域名解析的方法来解决此问题。重点强调了此选项在避免DNS超时带来的风险上的关键作用。

此接口并没有使用到SSL/TLS,但会不会是用到了signals导致的crash呢?官方建议在多线程场景下应该设置CURLOPT_NOSIGNAL选项,因为在解析DNS出现超时的时候将会发生“糟糕”的情况。官方也给出了解决方法,可以使用c-ares[2]的libcurl版本实现异步域名解析来预防这种“糟糕”的情况,但是最后一句还是告诫我们:在多线程场景下,若不设置CURLOPT_NOSIGNAL选项,可能会有“意外”的情况发生。通过官方这段描述,可以大致猜测到是没有设置这个选项造成的crash。下面是官方对此选项的说明[3]

CURLOPT_NOSIGNAL

int cloud_https_post(const char *pUrl, const char *request, char **response, st_http_resinfo *pHttpResInfo) { CURLcode res; CURL* curl = NULL; struct curl_slist *headers = NULL; #ifdef CLOUD_HTTPS_DEBUG char errbuf[CURL_ERROR_SIZE]; memset(errbuf, '\0', CURL_ERROR_SIZE); #endif if (NULL == pUrl || NULL == request || NULL == response || NULL == pHttpResInfo) { return CURLE_FAILED_INIT; } res = curl_global_init(CURL_GLOBAL_ALL); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl global init fail and ret %d", res); return res; } curl = curl_easy_init(); if (NULL == curl) { res = CURLE_FAILED_INIT; HTTPS_LOG(LOG_LEVEL_ERROR, "curl init fail"); goto exit; } headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8"); if (NULL == headers) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl get header list fail"); goto exit; } #ifdef CLOUD_HTTPS_DEBUG //provide a buffer to store errors in res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_ERRORBUFFER ret %d", res); goto exit; } #endif if (IS_SESSION_DEBUG_ON()) { HTTPS_LOG(LOG_LEVEL_DEBUG, "post sesstion debug on"); res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_VERBOSE ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_DEBUGFUNCTION ret %d", res); goto exit; } } res = curl_easy_setopt(curl, CURLOPT_URL, pUrl); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_URL ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_HTTPHEADER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_POST, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POST ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POSTFIELDS ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_READFUNCTION ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData_Post); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEFUNCTION ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEDATA ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_NOSIGNAL ret %d", res); goto exit; } if (IS_CA_PATH_NULL()) { res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYHOST ret %d", res); goto exit; } } else { res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_CAINFO, GET_CA_PATH()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CAINFO ret %d", res); goto exit; } if (GET_CA_TYPE()) { res = curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,GET_CA_TYPE()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSLCERTTYPE ret %d", res); goto exit; } } else { //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM"); } } res = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, GET_P_CONNECT_TIMEOUT()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CONNECTTIMEOUT ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, GET_P_TRANSFER_TIMEOUT()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_TIMEOUT ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_FOLLOWLOCATION ret %d", res); goto exit; } res = curl_easy_perform(curl); #ifdef CLOUD_HTTPS_DEBUG if(CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_DEBUG, "curl post return error: %s", errbuf); } #endif curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &(pHttpResInfo->status_code)); HTTPS_LOG(LOG_LEVEL_DEBUG, "cloud_https_post done. ret %d, http status code %ld", res, pHttpResInfo->status_code); exit: if (headers) { curl_slist_free_all(headers); } if (curl) { curl_easy_cleanup(curl); } curl_global_cleanup(); return res; }解析函数
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值