Skip to content

Commit

Permalink
Prevent cURL transport from leaking on Exception
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
soulseekah authored and schlessera committed Nov 10, 2021
1 parent 38b5686 commit 43b291e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,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;
}
Expand Down

0 comments on commit 43b291e

Please sign in to comment.