线程使用libcurl的巨坑踩了一天!!!
2019-07-12 20:16发布
生成海报
用libcurl一段时间遇到莫名其妙的程序崩溃的情况,开始觉得是线程栈溢出导致的段错误,专门增加了线程栈的大小,还是有问题。线程也是分离的。用valgrind定位到问题可能出现在curl的调用上。
排查的时候也发现了libcurl一些额外的坑,现做个总结笔记。
1.、
线程使用libcurl访问时,设置了超时时间,而libcurl库不会为这个超时信号做任何处理,信号产生而没有信号句柄处理,可能导致程序退出。用以下选项禁止访问超时的时候抛出超时信号。
curl_setopt(curl, CURLOPT_NOSIGNAL,1L);
2、
正常使用流程是先调用curl_global_init初始化资源,而这个函数不是线程安全的。curl_easy_init发现没有做初始化时会自动初始化,多个线程同时进行curl_easy_init时会导致异常。curl官方建议在主线程中先做初始化。
curl_global_init(CURL_GLOBAL_ALL);
3、
用valgrind调试,做压力测试多线程运行时,valgrind弹出类似访问冲突的错误,把错误定位到libcurl的互斥访问的处理上。
大致是进行访问设计到ssl的东西的时候,需要做相应的加锁处理,libcurl源码没有加这块处理,不过给了个针对这种情况加锁的例子,总的来说curl的示例代码很重要很有用。
https://curl.haxx.se/libcurl/c/example.html
[cpp] view plain copy print?- #define USE_OPENSSL
-
- #include
- #include
- #include
-
- #define NUMT 4
-
-
- static pthread_mutex_t *lockarray;
-
- #ifdef USE_OPENSSL
- #include
- static void lock_callback(int mode, int type, char *file, int line)
- {
- (void)file;
- (void)line;
- if(mode & CRYPTO_LOCK) {
- pthread_mutex_lock(&(lockarray[type]));
- }
- else {
- pthread_mutex_unlock(&(lockarray[type]));
- }
- }
-
- static unsigned long thread_id(void)
- {
- unsigned long ret;
-
- ret=(unsigned long)pthread_self();
- return ret;
- }
-
- static void init_locks(void)
- {
- int i;
-
- lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
- sizeof(pthread_mutex_t));
- for(i=0; i
- pthread_mutex_init(&(lockarray[i]), NULL);
- }
-
- CRYPTO_set_id_callback((unsigned long (*)())thread_id);
- CRYPTO_set_locking_callback((void (*)())lock_callback);
- }
-
- static void kill_locks(void)
- {
- int i;
-
- CRYPTO_set_locking_callback(NULL);
- for(i=0; i
- pthread_mutex_destroy(&(lockarray[i]));
-
- OPENSSL_free(lockarray);
- }
- #endif
-
- #ifdef USE_GNUTLS
- #include
- #include
-
- GCRY_THREAD_OPTION_PTHREAD_IMPL;
-
- void init_locks(void)
- {
- gcry_control(GCRYCTL_SET_THREAD_CBS);
- }
-
- #define kill_locks()
- #endif
-
-
- const char * const urls[]= {
- ”https://www.example.com/”,
- ”https://www2.example.com/”,
- ”https://www3.example.com/”,
- ”https://www4.example.com/”,
- };
-
- static void *pull_one_url(void *url)
- {
- CURL *curl;
-
- curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, url);
-
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
-
- return NULL;
- }
-
- int main(int argc, char **argv)
- {
- pthread_t tid[NUMT];
- int i;
- int error;
- (void)argc;
- (void)argv;
-
-
- curl_global_init(CURL_GLOBAL_ALL);
-
- init_locks();
-
- for(i=0; i< NUMT; i++) {
- error = pthread_create(&tid[i],
- NULL,
- pull_one_url,
- (void *)urls[i]);
- if(0 != error)
- fprintf(stderr, ”Couldn’t run thread number %d, errno %d
”, i, error);
- else
- fprintf(stderr, ”Thread %d, gets %s
”, i, urls[i]);
- }
-
-
- for(i=0; i< NUMT; i++) {
- error = pthread_join(tid[i], NULL);
- fprintf(stderr, ”Thread %d terminated
”, i);
- }
-
- kill_locks();
-
- return 0;
- }
#define USE_OPENSSL
#include
#include
#include
#define NUMT 4
/* we have this global to let the callback get easy access to it */
static pthread_mutex_t *lockarray;
#ifdef USE_OPENSSL
#include
static void lock_callback(int mode, int type, char *file, int line)
{
(void)file;
(void)line;
if(mode & CRYPTO_LOCK) {
pthread_mutex_lock(&(lockarray[type]));
}
else {
pthread_mutex_unlock(&(lockarray[type]));
}
}
static unsigned long thread_id(void)
{
unsigned long ret;
ret=(unsigned long)pthread_self();
return ret;
}
static void init_locks(void)
{
int i;
lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
sizeof(pthread_mutex_t));
for(i=0; i
#include
GCRY_THREAD_OPTION_PTHREAD_IMPL;
void init_locks(void)
{
gcry_control(GCRYCTL_SET_THREAD_CBS);
}
#define kill_locks()
#endif
/* List of URLs to fetch.*/
const char * const urls[]= {
"https://www.example.com/",
"https://www2.example.com/",
"https://www3.example.com/",
"https://www4.example.com/",
};
static void *pull_one_url(void *url)
{
CURL *curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
/* this example doesn't verify the server's certificate, which means we
might be downloading stuff from an impostor */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_perform(curl); /* ignores error */
curl_easy_cleanup(curl);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid[NUMT];
int i;
int error;
(void)argc; /* we don't use any arguments in this example */
(void)argv;
/* Must initialize libcurl before any threads are started */
curl_global_init(CURL_GLOBAL_ALL);
init_locks();
for(i=0; i< NUMT; i++) {
error = pthread_create(&tid[i],
NULL, /* default attributes please */
pull_one_url,
(void *)urls[i]);
if(0 != error)
fprintf(stderr, "Couldn't run thread number %d, errno %d
", i, error);
else
fprintf(stderr, "Thread %d, gets %s
", i, urls[i]);
}
/* now wait for all threads to terminate */
for(i=0; i< NUMT; i++) {
error = pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d terminated
", i);
}
kill_locks();
return 0;
}
未完待续。。。
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮