cURL / Mailing Lists / curl-users / Single Mail

curl-users

SSL Context not cleaned up in multi handle

From: Lau, Hang Kin <hklau_at_avistar.com>
Date: Tue, 20 Feb 2007 10:11:15 -0800

It seems SSL ctx is not cleaned up properly when multi handle is used.
The problem is more apparent when I used the CURLOPT_CAINFO option to
set a file which contains those root certificates. A lot of memory
blocks were found as "memory leaks" under MSVC .net. Those may not be
actual memory leaks that keeps eating memory when the process runs. But
the large amount of blocks detected by MSVC is disturbing as when the
process ends, it takes MSVC quite a while to display all the blocks.
 
It seems curl_multi_cleanup() didn't release the SSL ctx in it's
connection cache as curl_easy_cleanup() does.
 
 
The flow of my program is similar to this:
 
curl* pEasyHandle = curl_easy_init();
curl* pMultiHandle = curl_multi_init();
 
curl_easy_setopt(pEasyHandle, CURLOPT_CAINFO, "cacert.pem");
curl_easy_setopt(pEasyHandle, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(pEasyHandle, CURLOPT_SSL_VERIFYHOST, 2);
 
curl_multi_add_handle(pMultiHandle, pEasyHandle);
 
while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pMultiHandle,
&integer);
 
curl_multi_remove_handle(pMultiHandle, pEasyHandle);
 
curl_easy_cleanup(pEasyHandle);
curl_multi_cleanup(pMultiHandle);
 
 
By making the following changes, it seems to work and cleanup the SSL
ctx correctly but I am not sure if it could break something else in
libcurl?
 
Index: curl/lib/multi.c
===================================================================
--- curl/lib/multi.c (curl 7.16.0)
+++ curl/lib/multi.c (working copy)
@@ -591,7 +591,10 @@
       /* and modify the connectindex since this handle can't point to
the
          connection cache anymore */
       if(easy->easy_conn)
+ {
+ easy->easy_conn->data = NULL;
         easy->easy_conn->connectindex = -1;
+ }
     }
 
     /* change state without using multistate(), only to make
singlesocket() do
Index: curl/lib/url.c
===================================================================
--- curl/lib/url.c (curl 7.16.0)
+++ curl/lib/url.c (working copy)
@@ -1687,6 +1687,8 @@
   if (!conn)
     return;
 
+ Curl_ssl_close(conn);
+
   /* close possibly still open sockets */
   if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
     sclose(conn->sock[SECONDARYSOCKET]);
@@ -1796,8 +1798,6 @@
                                        allocated by libidn */
 #endif
 
- Curl_ssl_close(conn);
-
   /* Indicate to all handles on the pipe that we're dead */
   if (IsPipeliningEnabled(data)) {
     signalPipeClose(conn->send_pipe);
Received on 2007-02-20