From d8faea6cb9d35e1c35807b6023280d096ab64850 Mon Sep 17 00:00:00 2001 From: Gennady Kovshenin Date: Sat, 17 Feb 2018 19:57:25 +0500 Subject: [PATCH] Prevent cURL transport from leaking on Exception The `CURLOPT_HEADERFUNCTION` and `CURLOPT_WRITEFUNCTION` options are not being reset if a cURL handle error occurs (which throws a `Requests_Exception` and stops execution). The destructor is not called due to lingering instance method callbacks in the two options. Move `CURLOPT_HEADERFUNCTION` and `CURLOPT_WRITEFUNCTION` cleanup before any exceptions can happen. --- src/Transport/Curl.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index df98894a3..5547ec670 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -201,12 +201,14 @@ public function request($url, $headers = array(), $data = array(), $options = ar $response = $this->response_data; } - $this->process_response($response, $options); + if (true === $options['blocking']) { + // Need to remove the $this reference from the curl handle. + // Otherwise \WpOrg\Requests\Transport\Curl wont be garbage collected and the curl_close() will never be called. + curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); + curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); + } - // Need to remove the $this reference from the curl handle. - // Otherwise \WpOrg\Requests\Transport\Curl won't be garbage collected and the curl_close() will never be called. - curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); - curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); + $this->process_response($response, $options); return $this->headers; }