php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75283 Disconnection not detected by socket_read()
Submitted: 2017-09-29 09:54 UTC Modified: 2017-09-29 13:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: privacy dot and dot spam dot protection at gmx dot fr Assigned:
Status: Open Package: Sockets related
PHP Version: 7.0.24 OS: Debian
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-09-29 09:54 UTC] privacy dot and dot spam dot protection at gmx dot fr
Description:
------------
According to documentation, an empty string ("") is returned by socket_read() when there's no more data to read and false value on error (including if the remote host has closed the connection).

However when socket_read has no data to read false is still returned.
So it's impossible to detect if connection has been reseted by pear.



Test script:
---------------
$co = false;
while (!$co) {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket !== false) echo "OK 1.\n";
    $result = socket_connect($socket, $address, $service_port);
    if ($socket !== false) {
        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 1));
        echo "OK 2.\n";
        $co = true;
    }
}
while (true) {
    socket_clear_error($socket);
    $out               = socket_read($socket, 2048);
    $socket_last_error = socket_last_error($socket);
    $strErr            = socket_strerror($socket_last_error) . ' ' . $socket_last_error;
    echo date('Ymd H:i:s') . " : $strErr ($socket_last_error) => " . var_export($out, true) . PHP_EOL;
}

Expected result:
----------------
OK 1.
Try connection '9.3.0.23' on port '30001'...
OK 2.
// client's connected on server
// no data have been sent by server
20170929 11:09:17 : Success 0 (0) => ''
20170929 11:09:18 : Success 0 (0) => ''
20170929 11:09:19 : Success 0 (0) => ''
//server send data
20170929 11:09:19 : Success 0 (0) => 'a'
20170929 11:09:19 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
// no data have been sent by server
20170929 11:09:21 : Success 0 (0) => ''
20170929 11:09:22 : Success 0 (0) => ''
20170929 11:09:23 : Success 0 (0) => ''
// connection is reset by server
20170929 11:09:24 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:25 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:26 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:27 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:28 : Resource temporarily unavailable 11 (11) => false


Actual result:
--------------
OK 1.
Try connection '9.3.0.23' on port '30001'...
OK 2.
// client's connected on server
// no data have been sent by server
20170929 11:09:17 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:18 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:19 : Resource temporarily unavailable 11 (11) => false
//server send data
20170929 11:09:19 : Success 0 (0) => 'a'
20170929 11:09:19 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
20170929 11:09:20 : Success 0 (0) => 'a'
// no data have been sent by server
20170929 11:09:21 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:22 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:23 : Resource temporarily unavailable 11 (11) => false
// connection is reset by server
20170929 11:09:24 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:25 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:26 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:27 : Resource temporarily unavailable 11 (11) => false
20170929 11:09:28 : Resource temporarily unavailable 11 (11) => false


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-29 11:13 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-09-29 11:13 UTC] requinix@php.net
Looks like the socket is still open, not reset.

I haven't been able to reproduce your output in a way where the socket is reset/closed. What's the server code? What's the actual client code that sends data to the server in order to trigger the RST?
 [2017-09-29 13:18 UTC] privacy dot and dot spam dot protection at gmx dot fr
-Status: Feedback +Status: Open
 [2017-09-29 13:18 UTC] privacy dot and dot spam dot protection at gmx dot fr
I haven't got server code because I use an RS232 TCP/IP converter's box. Problem happens when there's an ellectric shutdown, PHP socket_read function doesn't detect  disconnection
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Apr 22 02:01:25 2019 UTC