From eafccdb3150115e94f16a3a21ea989aeb65a0210 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Wed, 19 Dec 2012 19:52:11 +0100 Subject: bundles connection caching: some out of memory handling fixes --- lib/multi.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'lib/multi.c') diff --git a/lib/multi.c b/lib/multi.c index bab61578a..a1dd70513 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -432,6 +432,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle, struct Curl_one_easy *easy; struct Curl_multi *multi = (struct Curl_multi *)multi_handle; struct SessionHandle *data = (struct SessionHandle *)easy_handle; + struct SessionHandle *new_closure = NULL; /* First, make some basic checks that the CURLM handle is a good handle */ if(!GOOD_MULTI_HANDLE(multi)) @@ -447,15 +448,6 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle, /* possibly we should create a new unique error code for this condition */ return CURLM_BAD_EASY_HANDLE; - /* This is a good time to allocate a fresh easy handle to use when closing - cached connections */ - if(!multi->closure_handle) { - multi->closure_handle = - (struct SessionHandle *)curl_easy_init(); - Curl_easy_addmulti(easy_handle, multi_handle); - multi->closure_handle->state.conn_cache = multi->conn_cache; - } - /* Allocate and initialize timeout list for easy handle */ timeoutlist = Curl_llist_alloc(multi_freetimeout); if(!timeoutlist) @@ -469,6 +461,17 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle, return CURLM_OUT_OF_MEMORY; } + /* In case multi handle has no closure_handle yet, allocate + a new easy handle to use when closing cached connections */ + if(!multi->closure_handle) { + new_closure = (struct SessionHandle *)curl_easy_init(); + if(!new_closure) { + free(easy); + Curl_llist_destroy(timeoutlist, NULL); + return CURLM_OUT_OF_MEMORY; + } + } + /* ** No failure allowed in this function beyond this point. And ** no modification of easy nor multi handle allowed before this @@ -476,6 +479,14 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle, ** won't be undone in this function no matter what. */ + /* In case a new closure handle has been initialized above, it + is associated now with the multi handle which lacked one. */ + if(new_closure) { + multi->closure_handle = new_closure; + Curl_easy_addmulti(multi->closure_handle, multi_handle); + multi->closure_handle->state.conn_cache = multi->conn_cache; + } + /* Make easy handle use timeout list initialized above */ data->state.timeoutlist = timeoutlist; timeoutlist = NULL; -- cgit v1.2.3