php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69008 socket_read() never returns false when client disconnects with PHP_BINARY_READ
Submitted: 2015-02-07 23:39 UTC Modified: 2021-07-28 11:18 UTC
Votes:5
Avg. Score:4.6 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: sjaillet at gmail dot com Assigned:
Status: Verified Package: Sockets related
PHP Version: 5.6.5 OS: Ubuntu 14.04.1
Private report: No CVE-ID: None
 [2015-02-07 23:39 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-02 08:41 UTC] 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
 [2021-07-28 11:18 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-07-28 11:18 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC