Infinite waiting for CURLOPT_WRITEFUNCTION in case of no response
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.1k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue には CURLOPT_WRITEFUNCTION と無期限にストリーミングする URL を使用した PHP の curl_exec の再現コードがあるため、まず応答が停止するケースを再現し、php-src でのオプションの処理を追跡します。観測された動作を、要求されている timeout とエラーのセマンティクスと比較し、応答がない期間を説明どおりに検出して処理できるようになった時点で作業完了とみなします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- php
- 領域
- networking
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100