Reputation: 339
I'm using libcurl to send POST requests to a server. Everything worked fine until I noticed that sometimes the app terminates unexpectedly (not timeout, it just ends without even an error message). I have narrowed it down to a curl_esy_perform. The server is long to reply to that request (tested with Postman and it takes 20 minutes+ but it does reply in the end) and my app crashes after about 12 minutes without any error message. The timeout is set to 3600 seconds. The write memory callback isn't getting called at all (it shouldn't as the server doesn't return partial data) This is how I'm using curl:
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl;
CURLcode res;
struct MemoryStruct chunk;
int err = -1;
chunk.memory = (PCHAR)malloc(1);
chunk.size = 0;
if (!(curl = curl_easy_init())) {
cout << "Error initializing cURL" << endl;
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PORT, port);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteMemCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (PVOID)&chunk);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600);
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(body));
// It crashes during curl_easy_perform, after it hangs for ~12 minutes
res = curl_easy_perform(curl);
.
.
.
size_t CurlWriteMemCallback(PVOID contents, size_t size, size_t nmemb, PVOID userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (PCHAR)realloc(mem->memory, mem->size + realsize + 1);
if (!mem->memory) {
FATAL() << "Not enough memory in cURL write memory callback";
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
Upvotes: 0
Views: 1479
Reputation: 11
Is the process exiting due to the behaviour described in the man pages for curl regarding signal on timeout? https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
i.e. "This option may cause libcurl to use the SIGALRM signal to timeout system calls."
If your application doesn't handle the signal then it will exit.
There is an option that disables generating this signal in case of timeout: "In unix-like systems, this might cause signals to be used unless CURLOPT_NOSIGNAL is set."
Hope this isn't too late to help others at least.
Upvotes: 1