Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61b3b14

Browse files
committedSep 11, 2023
Use weak references to avoid the test causing a leak
1 parent dd91131 commit 61b3b14

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed
 

‎src/Transport/Curl.php

-6
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ public function request($url, $headers = [], $data = [], $options = []) {
187187
}
188188
}
189189

190-
$this->hooks = $options['hooks'];
191-
192-
$this->setup_handle($url, $headers, $data, $options);
193-
194-
$options['hooks']->dispatch('curl.before_send', [&$this->handle]);
195-
196190
$this->response_data = '';
197191
$this->response_bytes = 0;
198192
$this->response_byte_limit = false;

‎tests/Transport/Curl/CurlTest.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WpOrg\Requests\Tests\Transport\Curl;
44

55
use CurlHandle;
6+
use WeakReference;
67
use WpOrg\Requests\Exception;
78
use WpOrg\Requests\Hooks;
89
use WpOrg\Requests\Requests;
@@ -15,7 +16,10 @@ final class CurlTest extends BaseTestCase {
1516
/**
1617
* Temporary storage of the cURL handle to assert against.
1718
*
18-
* @var null|resource|\CurlHandle
19+
* The handle is stored as a weak reference in order to avoid the test itself
20+
* becoming the source of the memory leak due to locking the resource.
21+
*
22+
* @var null|WeakReference
1923
*/
2024
protected $curl_handle;
2125

@@ -34,14 +38,20 @@ protected function getOptions($other = []) {
3438

3539
$this->curl_handle = null;
3640

41+
// On PHP < 7.2, we lack the capability to store weak references.
42+
// In this case, we just skip the memory leak testing.
43+
if (version_compare(PHP_VERSION, '7.2.0') < 0) {
44+
return $options;
45+
}
46+
3747
if (!array_key_exists('hooks', $options)) {
3848
$options['hooks'] = new Hooks();
3949
}
4050

4151
$options['hooks']->register(
4252
'curl.before_request',
4353
function ($handle) {
44-
$this->curl_handle = $handle;
54+
$this->curl_handle = WeakReference::create($handle);
4555
}
4656
);
4757

@@ -54,19 +64,19 @@ function ($handle) {
5464
* This is used for asserting that cURL handles are not leaking memory.
5565
*/
5666
protected function assert_post_conditions() {
57-
if ($this->curl_handle === null) {
67+
if (version_compare(PHP_VERSION, '7.2.0') < 0 || !$this->curl_handle instanceof WeakReference) {
5868
// No cURL handle was used during this particular test scenario.
5969
return;
6070
}
6171

62-
if ($this->curl_handle instanceof CurlHandle) {
72+
if ($this->curl_handle->get() instanceof CurlHandle) {
6373
// CURL handles have been changed from resources into CurlHandle
6474
// objects starting with PHP 8.0, which don;t need to be closed.
6575
return;
6676
}
6777

68-
if ($this->shouldClosedResourceAssertionBeSkipped($this->curl_handle) === false) {
69-
$this->assertIsClosedResource($this->curl_handle);
78+
if ($this->shouldClosedResourceAssertionBeSkipped($this->curl_handle->get()) === false) {
79+
$this->assertIsClosedResource($this->curl_handle->get());
7080
}
7181
}
7282

0 commit comments

Comments
 (0)
Please sign in to comment.