From 7234c797c05721507d4e2ad6667f528cab1ee800 Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Tue, 13 Jun 2017 08:39:30 -0700 Subject: [PATCH] Fixes #283 : Fixes CPU busy loop when using request_multiple. This will require more testing across various libcurl versions. I doubt that the timeout is necessary for curl_multi_select (calls select() if it can), but leaving in one just in case of bugs, so that it will end. - Haven't thoroughly checked for relevant libcurl bugs. Asynchronously wait for events with a short timeout if CURLM_CALL_MULTI_PERFORM --- library/Requests/Transport/cURL.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 4429edb64..386724267 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -219,8 +219,20 @@ public function request_multiple($requests, $options) { do { $status = curl_multi_exec($multihandle, $active); + // Return immediately if there's work to be done. + if ($status !== CURLM_CALL_MULTI_PERFORM) { + break; + } + // Block until there is activity on any of the curl_multi connections. + // Timeouts can be specified in microseconds. + // Provide curl_multi_select with a timeout of 100ms (May not be necessary). + // As a fallback, if curl_multi_select couldn't properly asynchronously wait for input, sleep for 0.1 ms. + $select_status = curl_multi_select($multihandle, 0.1); + if ($select_status === -1) { + usleep(100); + } } - while ($status === CURLM_CALL_MULTI_PERFORM); + while (true); $to_process = array();