Bug #69008 [Opn->Ver]: socket_read() never returns false when client disconnects with PHP_BINARY_READ
| From: | cmb@php.net | Date: | Wed, 28 Jul 2021 11:18:47 +0000 |
| Subject: | Bug #69008 [Opn->Ver]: socket_read() never returns false when client disconnects with PHP_BINARY_READ | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235433@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69008&edit=1
ID: 69008
Updated by: cmb@php.net
Reported by: sjaillet at gmail dot com
Summary: socket_read() never returns false when client
disconnects with PHP_BINARY_READ
-Status: Open
+Status: Verified
Type: Bug
Package: Sockets related
Operating System: Ubuntu 14.04.1
PHP Version: 5.6.5
Block user comment: N
Private report: N
New Comment:
The documentation[1] states:
| socket_read() returns the data as a string on success, or false
| on error (including if the remote host has closed the connection).
[1] <https://www.php.net/manual/en/function.socket-read.php#refsect1-function.socket-read-returnvalues>
Previous Comments:
------------------------------------------------------------------------
[2017-08-02 08:41:17] emilien dot calmels at gmail dot com
I've got same problem in php 7.0.16-3 (cli) on Debian 8.
This bug has been reported several times for 17 years and hasn't been fixed in 2017!
Please do something
------------------------------------------------------------------------
[2015-02-07 23:39:21] sjaillet at gmail dot com
Description:
------------
According to the documentation socket_read() is supposed to return false on error however it never
return false when the connection has been reseted by peer.
Test script:
---------------
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo socket_strerror(socket_last_error()) . "\n";
exit(-1);
}
if (socket_bind($sock, '127.0.0.1', 10000) === false) {
echo socket_strerror(socket_last_error($sock)) . "\n";
exit(-1);
}
if (socket_listen($sock, 5) === false) {
echo socket_strerror(socket_last_error($sock)) . "\n";
exit(-1);
}
$clients = [];
do {
$read = [];
$read[] = $sock;
$read = array_merge($read, $clients);
if (socket_select($read, $write, $except, 10) < 1)
{
continue;
}
if (in_array($sock, $read)) {
if (($client = socket_accept($sock)) === false) {
echo socket_strerror(socket_last_error($sock)) . "\n";
break;
}
$clients[] = $client;
}
foreach ($clients as $key => $client) {
if (in_array($client, $read)) {
if (($buffer = socket_read($client, 1024)) === false) {
unset($clients[$key]);
echo socket_strerror(socket_last_error($client)) . "\n";
}
var_dump($buffer);
}
}
} while (true);
socket_close($sock);
?>
You can reproduce the bug by executing the following command in two different terminals.
Terminal 1:
php server.php
Terminal 2:
nc localhost 10000
Then hit Ctrl+C in the terminal 2 to close the process
Expected result:
----------------
Socket_read(): unable to read from socket []: Connection reset by peer
bool(false)
Actual result:
--------------
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
etc.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69008&edit=1