Infinite waiting for CURLOPT_WRITEFUNCTION in case of no response
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
To reach an address that responds every 5 seconds, I use CURLOPT_WRITEFUNCTION. Since I want to maintain an infinite connection, I have CURLOPT_TIMEOUT => 0.
function writer($ch, $data) {
handler($data);
return strlen($data);
}
$ch = curl_init("$urlcam/cgi-bin/eventManager.cgi?action=attach&codes=[NewFile]&heartbeat=5");
curl_setopt_array($ch, [
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_WRITEFUNCTION => 'writer'
]);
$result = curl_exec($ch);
curl_close($ch);
The response looks like this:
--myboundary
Content-Type: text/plain
Content-Length: 9
Heartbeat
--myboundary
Content-Type: text/plain
Content-Length: 9
Heartbeat
...
Everything works as long as there is a response from the url. However, if the url stops responding, nothing happens. No error or warning. The PHP script will stay inside the "writer" function and never leave it.
For example, if you implement this functionality using fopen:
$resource = fopen("http://".$host.$uri, 'r', false, $context);
while (!feof($resource)) {
print_r(date("d-m-Y H:i:s"));
$line = fgets($resource);
if (str_contains($line,"--myboundary")) {
handler($response);
$response = '';
} else {
$response.=$line;
}
}
Then the print_r function will be called every minute. Yes, if the url is restored, the fopen connection will not be restored, but at least we can handle the error/state to restart the script. In the case of CURL, this cannot be done as nothing happens.
We need to add functionality to leave the function specified in CURLOPT_WRITEFUNCTION. I suppose you could take CURLOPT_CONNECTTIMEOUT and display an error if the WRITEFUNCTION has not received a response within that time.
PHP Version
8.1.23
Operating System
Ubuntu 20.04
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides a PHP curl_exec reproduction using CURLOPT_WRITEFUNCTION and an indefinitely streaming URL; start by reproducing the stalled-response case and tracing the option's handling in php-src. Compare the observed behavior with the requested timeout and error semantics, and consider the work done when a no-response period can be detected and handled as described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100