具有多线程卷曲请求的Segfault

时间:2014-07-05 18:28:15

标签: c++ linux multithreading curl libcrypto

我在使用C ++程序时遇到了一些麻烦。基本上我已经为http请求编写了一个简单的包装器,能够一次执行多个请求。 工作得很好,但是当我做httpS请求时,它会在多线程模式下随机崩溃。我正在使用curl和posix线程。 Backtrace看起来像这样:

======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80996)[0x7fea9046d996]
/lib/x86_64-linux-gnu/libc.so.6(+0x82b80)[0x7fea9046fb80]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0xf2)[0x7fea90470ae2]
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0(CRYPTO_realloc+0x49)[0x7fea8f9c6169]
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0(lh_insert+0x101)[0x7fea8fa4bfb1]
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0(+0xe844e)[0x7fea8fa4e44e]
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0(ERR_get_state+0xde)[0x7fea8fa4eeee]
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0(ERR_clear_error+0x15)[0x7fea8fa4f065]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(+0x24e79)[0x7fea90f10e79]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(+0x39ea0)[0x7fea90f25ea0]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(+0xf8fd)[0x7fea90efb8fd]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(+0x219f5)[0x7fea90f0d9f5]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(+0x35538)[0x7fea90f21538]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(curl_multi_perform+0x91)[0x7fea90f21d31]
/usr/lib/x86_64-linux-gnu/libcurl.so.4(curl_easy_perform+0x107)[0x7fea90f19457]
./exbot[0x40273a]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x7f6e)[0x7fea90cd6f6e]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fea904e79cd]

这可能是libcrypto中的错误吗? 我能以某种方式告诉curl不要使用libcrypto吗?任何替代品? 它只使用httpS请求来解析wenn,并且即使10000个同时的http查询也能正常工作。

干杯, 托马斯

为了完整我的代码:

// simple wrapper for http requests

#ifndef _REQUEST_H_
#define _REQUEST_H_


#include <curl/curl.h>
#include <pthread.h>
#include <string>
#include <iostream>


//////////////////////////////////
// MACROS
//////////////////////////////////
#define ERR(_msg) std::cerr << __FUNCTION__ << ": " << _msg << std::endl


//////////////////////////////////
// REQUEST WRAPPER
//////////////////////////////////
typedef unsigned int uint;
class RequestWrapper
{
private: // non copyable
    RequestWrapper();
    RequestWrapper(const RequestWrapper &that);
    RequestWrapper &operator=(const RequestWrapper &that);

public:
    struct Response
    {
        Response() : msg(""), success(false) {}
        std::string msg;
        bool success;
    };

    static Response simpleGET(std::string url, uint timeout);
    static size_t write(char *content, size_t size, size_t nmemb, void *userp);
};


//////////////////////////////////
// GET
//////////////////////////////////
inline size_t RequestWrapper::write(char *content, size_t size, size_t nmemb, void *userp)
{
    std::string *buf = static_cast<std::string *>(userp);
    size_t realsize = size * nmemb;
    for (uint i = 0; i < realsize; ++i)
    {
        buf->push_back(content[i]);
    }
    return realsize;
}
inline RequestWrapper::Response RequestWrapper::simpleGET(std::string url, uint timeout)
{
    Response resp;
    CURL *curl;
    CURLcode res;
    std::string buf;

    // send request
    buf.clear();
    curl = curl_easy_init();
    if (!curl)
    {
        //ERR("libcurl init failed");
        return resp;
    }
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void *>(&buf));
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
    {
        //ERR("libcurl request failed, CODE: " << res);
        return resp;
    }
    curl_easy_cleanup(curl);

    // done
    resp.msg = buf;
    resp.success = true;
    return resp;
}


//////////////////////////////////
// MULTITHREADED REQUEST
//////////////////////////////////
class RequestList
{
private:
    std::vector<std::string> _reqs;
    static void *sender(void *payload);
    static pthread_mutex_t _mutex;

public:
    inline void add(std::string request)
    {
        _reqs.push_back(request);
    }

    inline void clear()
    {
        _reqs.clear();
    }

    std::vector<std::string> send(uint timeout) const;

    struct Payload
    {
        std::string url;
        std::vector<std::string> *out;
        uint tout, index;
        Payload(std::string url,
                std::vector<std::string> *out,
                uint tout, uint index) : url(url), out(out), tout(tout), index(index) { }
        Payload() : url(""), out(NULL), tout(0), index(0) { }
    };
};


//////////////////////////////////
// SEND MT REQUEST
//////////////////////////////////
pthread_mutex_t RequestList::_mutex;
void *RequestList::sender(void *payload)
{
    Payload *pl = static_cast<Payload *>(payload);
    RequestWrapper::Response resp = RequestWrapper::simpleGET(pl->url, pl->tout);
    pthread_mutex_lock(&_mutex);
    if (resp.success)
    {
        pl->out->at(pl->index) = resp.msg;
        std::cerr << ".";
    }
    else
    {
        std::cerr << "x";
    }
    pthread_mutex_unlock(&_mutex);
    return NULL;
}
inline std::vector<std::string> RequestList::send(uint timeout) const
{
    std::vector<std::string> resp;
    resp.resize(_reqs.size());
    Payload *payloads = new Payload[_reqs.size()];
    pthread_t *tids = new pthread_t[_reqs.size()];

    // create mutex
    pthread_mutex_init(&_mutex, NULL);

    // prepare payload and create thread
    for (uint i = 0; i < _reqs.size(); ++i)
    {
        payloads[i] = Payload(_reqs[i], &resp, timeout, i);
        pthread_create(&tids[i], NULL, RequestList::sender, static_cast<void *>(&payloads[i]));
    }

    // wait for threads to finish
    for (uint i = 0; i < _reqs.size(); ++i)
    {
        pthread_join(tids[i], NULL);
    }
    std::cerr << std::endl;

    //destroy mutex
    pthread_mutex_destroy(&_mutex);

    delete[] payloads;
    delete[] tids;
    return resp;
}


#endif

1 个答案:

答案 0 :(得分:5)

Libcrypto是OpenSSL的一部分,除非您提供必要的回调,否则not thread-safe。根据{{​​3}},在符合POSIX的系统(具有线程本地errno)上,默认的thread-id实现是可接受的,因此您只需要一个锁定函数:

void locking_function(int mode, int n, const char *file, int line);

此函数需要维护一组CRYPTO_num_locks()互斥锁,并根据n的值锁定或解锁mode个互斥锁。您可以阅读文档以获取更多详细信息。 libcurl网站实际上有documentation显示如何执行此操作。

或者,您可以使用不同的线程安全SSL库构建libcurl,例如some sample code