[php-src] Issue #11418: fgets on a redis socket connection fails on PHP 8.3
| From: | protoinfo | Date: | Fri, 09 Jun 2023 19:07:04 +0000 |
| Subject: | [php-src] Issue #11418: fgets on a redis socket connection fails on PHP 8.3 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244650@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11418
Author: protoinfo
### Description
Hello,
We have been using the same code to interact with Redis since PHP 7.4 but our tests started failing
once we introduced PHP 8.3 into our test setup (We're not running it in production we just want
to make sure that our code will work once the stable release is available). We are running all of
our tests against the same Redis instance and it only fails when testing on PHP 8.3
This example is very simplified but it reproduces the error.
The following code:
```php
<?php
function readLine($connection): string {
$line = fgets($connection);
if ($line === false || $line === '') throw new RuntimeException('Failed to read line
from the server.');
return $line;
}
function read($connection, int $bytes): string {
$bytesLeft = $bytes;
$data = '';
do {
$chunk = fread($connection, min($bytesLeft, 4096));
if ($chunk === false) throw new RuntimeException('Failed to read data from the
server.');
$data .= $chunk;
$bytesLeft = $bytes - strlen($data);
}
while($bytesLeft > 0);
return $data;
}
function write($connection, string $data): int
{
$totalBytesWritten = 0;
$bytesLeft = strlen($data);
do {
$totalBytesWritten += $bytesWritten = fwrite($connection, $data);
if ($bytesWritten === false) throw new RuntimeException('Failed to write data to the
server.');
$bytesLeft -= $bytesWritten;
$data = substr($data, $bytesWritten);
}
while($bytesLeft > 0);
return $totalBytesWritten;
}
$host = 'phpdev-redis';
$port = 6379;
$context = stream_context_create(['socket' => ['tcp_nodelay' => true]]);
$connection = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, 60,
STREAM_CLIENT_CONNECT, $context);
stream_set_timeout($connection, 60);
write($connection, "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
var_dump(readLine($connection));
write($connection, "*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n");
var_dump(readLine($connection));
var_dump(read($connection, 7));
write($connection, "*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n");
var_dump(readLine($connection));
var_dump(read($connection, 7));
```
Resulted in this output:
```
string(5) "+OK
"
string(4) "$5
"
string(7) "value
"
Fatal error: Uncaught RuntimeException: Failed to read line from the server. in
/var/www/html/test/fail.php:5
```
But I expected this output instead:
```
string(5) "+OK
"
string(4) "$5
"
string(7) "value
"
string(4) "$5
"
string(7) "value
"
```
### PHP Version
PHP 8.3.0alpha1
### Operating System
_No response_