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: 2017-08-02 09:35 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: Open Package: Sockets related
PHP Version: 5.6.5 OS: Ubuntu 14.04.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sjaillet at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Oct 20 06:01:27 2019 UTC