cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Infinite loop when calling curl_multi_cleanup

From: Miloš Ljumović <milos_at_expert.its.me>
Date: Tue, 18 Oct 2016 18:08:32 +0200

Here is the (pseudo) code:

long PushRequest(params ...)
{
     CURL* hEasyHandle = 0;
     Lock(); // EnterCriticalSection
     {
         // init easy handle.
         hEasyHandle = curl_easy_init();
     }
     Unlock(); // LeaveCriticalSection

     if (!hEasyHandle)
     {
         return -1;
     }

     curl_easy_setopt(...

     //----------------- !!!!!!!!
     // Here I have to use dynamic memory - strange, cause I saw
"strdup" in libcurl
     char* szUrlBuffer = new char[1024]; // leak, needs upgrade
     StringCchPrintf(szUrlBuffer, 1024, "%s%s", szAPNSAddress,
szDeviceToken);
     curl_easy_setopt(hEasyHandle, CURLOPT_URL, szUrlBuffer);
     //----------------- !!!!!!!!
     curl_easy_setopt(hEasyHandle, CURLOPT_HTTP_VERSION,
CURL_HTTP_VERSION_2_0);
     curl_easy_setopt(hEasyHandle, CURLOPT_SSLCERT, ...);
     ...
     curl_easy_setopt(hEasyHandle, CURLOPT_USERAGENT, "curl/7.51.0-DEV");
     curl_easy_setopt(hEasyHandle, CURLOPT_TCP_KEEPALIVE, 1L);
     curl_easy_setopt(hEasyHandle, CURLOPT_PIPEWAIT, 1L);

     Lock();
     {
         curl_multi_add_handle(hMultiHandle, hEasyHandle);
     }
     Unlock();
     return 0L;
}

DWORD WINAPI ServerProc(LPVOID hParameter)
{
     ...
     long response = 0;
     char* buffer = 0;
     CURLMcode mcResult = CURLM_OK;
     while (event in non-signaled state)
     {
         // here i have logic which waits if no transfers - i don't want
to bother you with synchronization
         int numfds = 0;
         int iStillRunning = 0;
         CURLMcode result = CURLM_OK;
         if ((result = curl_multi_perform(hMultiHandle, &iStillRunning))
== CURLM_OK)
         {
             curl_multi_wait(hMultiHandle, NULL, 0, 100, &numfds);
         }
         do
         {
             int msgq = 0;
             hMsg = curl_multi_info_read(hMultiHandle, &msgq);
             if (hMsg && (hMsg->msg == CURLMSG_DONE))
             {
                 curl_easy_getinfo(hMsg->easy_handle,
CURLINFO_RESPONSE_CODE, &response);
                 curl_easy_getinfo(hMsg->easy_handle,
CURLINFO_EFFECTIVE_URL, &buffer);
                 // enqueue message to dispatcher
                 ...
                 // clean-up
                 Lock();
                 {
                     curl_multi_remove_handle(hMultiHandle,
hMsg->easy_handle);
                 }
                 Unlock();
                 curl_easy_cleanup(hMsg->easy_handle);
             }
         } while(hMsg);
     }
     return 0L;
}

Clients are calling the PushRequest in parallel, while separate thread
is running the ServerProc.
It works fine, I was able to push 100+ concurrent requests towards the
Apple APNs without any problems.
Notifications were delivered.

Complete log running 4 threads is in the attachment.
I'm suspecting I'm doing something wrong, cause I'm getting strange
messages in log (sample):
* Server doesn't support multi-use yet, wait
* Curl_readwrite: forcibly told to drain data
...

Thanks!

-- 
Miloš Ljumović
Operating systems specialist Spec.App.
http://milos.expert.its.me


-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html

Received on 2016-10-18