Bug #64398 [Opn->Fbk]: strange stream_socket_client

From: Date: Wed, 02 Apr 2014 12:34:08 +0000
Subject: Bug #64398 [Opn->Fbk]: strange stream_socket_client
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185036@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64398&edit=1

 ID:                 64398
 Updated by:         mike@php.net
 Reported by:        michel dot sanchez dot 83 at gmail dot com
 Summary:            strange stream_socket_client
-Status:             Open
+Status:             Feedback
 Type:               Bug
 Package:            Streams related
 Operating System:   Linux
 PHP Version:        5.4.12
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.




Previous Comments:
------------------------------------------------------------------------
[2013-03-09 09:05:41] michel dot sanchez dot 83 at gmail dot com

Description:
------------
In order to communicate with many servers at the same time in a persistent way, i use the
stream_socket_client function.

Once the connection is established with all the servers, I wait for the http timeout of each
connection.

The servers send a "close_wait".

When i try to reused the previous connections, a new connection is automaticaly establish with the
first server while all other connections are reused with a feof available.

Why the first connection not have the same comportment than other ?

Test script:
---------------
<!DOCTYPE html>
<meta charset="utf-8">
<title>Bug in stream_socket_client ?</title>

<pre><?php

ob_start();

$remotes = array (
    'cl108.advertstream.com' => '91.121.95.33',
    'cl109.advertstream.com' => '94.23.35.164',
    'cl111.advertstream.com' => '94.23.206.224',
    'cl121.advertstream.com' => '91.121.163.5',
);

$errno;
$errstr;

$read = [];

foreach ($remotes as $key => $target) {

    do {

        $socket = stream_socket_client(
            'tcp://'.$target.':80',
            $errno,
            $errstr,
            0.700,
            STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT
        );

        stream_set_blocking($socket, 0);

        $reused = (bool) ftell($socket);
        fread($socket, 4096);
        $want_close = feof($socket);

        $local = stream_socket_get_name($socket, false);

        if (true === $want_close) {
            echo $key . ' (' . $local . '): want_close' . PHP_EOL;
            fclose($socket);
            continue;
        }

        if (true === $reused) {
            echo $key . ' (' . $local . '): reused connection' . PHP_EOL;
        } else {
            echo $key . ' (' . $local . '): new connection' . PHP_EOL;
        }

        echo PHP_EOL;

        break;

    } while (true);

    $read[] = $socket;

    $message = "GET / HTTP/1.1\r\n".
        "Host: $host\r\n".
        "Connection: Keep-Alive\r\n".
        "\r\n";

    fwrite($socket, $message);
}

$write;
$except;
$originalRead = $read;

$success = 0;
$iteration = count($originalRead);

do {

    stream_select($read, $write, $except, 0, 500e3);

    foreach ($read as $stream) {
        $bytes = fread($stream, 4096);

        if (false !==  strpos($bytes, "0\r\n\r\n")) {
            $success++;
        }
    }

    if ($iteration === $success) {
        break;
    }

    $read = $originalRead;

} while (true);

ob_end_flush();

?></pre>

Expected result:
----------------
 - calling script (1)

cl108.advertstream.com (192.168.2.10:59816): new connection
cl109.advertstream.com (192.168.2.10:52000): new connection
cl111.advertstream.com (192.168.2.10:43161): new connection
cl121.advertstream.com (192.168.2.10:39815): new connection

 - watching netstat

tcp        0      0 michel-p6-2312ef.:59816 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52000 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43161 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39815 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59816 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52000 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43161 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39815 cl121.advertstream:http CLOSE_WAIT

 - calling script (2)

cl108.advertstream.com (192.168.2.10:59816): want_close <<<< EXPECTED <<<<
cl108.advertstream.com (192.168.2.10:59821): new connection
cl109.advertstream.com (192.168.2.10:52000): want_close
cl109.advertstream.com (192.168.2.10:52005): new connection
cl111.advertstream.com (192.168.2.10:43161): want_close
cl111.advertstream.com (192.168.2.10:43166): new connection
cl121.advertstream.com (192.168.2.10:39815): want_close
cl121.advertstream.com (192.168.2.10:39820): new connection

 - watching netstat

tcp        0      0 michel-p6-2312ef.:59821 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52005 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43166 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39820 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59821 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52005 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43166 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39820 cl121.advertstream:http CLOSE_WAIT

 - calling script (3)

cl108.advertstream.com (192.168.2.10:59821): want_close <<<< EXPECTED <<<<
cl108.advertstream.com (192.168.2.10:59826): new connection
cl109.advertstream.com (192.168.2.10:52005): want_close
cl109.advertstream.com (192.168.2.10:52010): new connection
cl111.advertstream.com (192.168.2.10:43166): want_close
cl111.advertstream.com (192.168.2.10:43171): new connection
cl121.advertstream.com (192.168.2.10:39820): want_close
cl121.advertstream.com (192.168.2.10:39825): new connection

tcp        0      0 michel-p6-2312ef.:59826 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52010 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43171 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39825 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59826 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52010 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43171 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39825 cl121.advertstream:http CLOSE_WAIT

 - ...

Actual result:
--------------
 - calling script (1)

cl108.advertstream.com (192.168.2.10:59816): new connection
cl109.advertstream.com (192.168.2.10:52000): new connection
cl111.advertstream.com (192.168.2.10:43161): new connection
cl121.advertstream.com (192.168.2.10:39815): new connection

 - watching netstat

tcp        0      0 michel-p6-2312ef.:59816 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52000 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43161 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39815 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59816 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52000 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43161 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39815 cl121.advertstream:http CLOSE_WAIT

 - calling script (2)

cl108.advertstream.com (192.168.2.10:59821): new connection
cl109.advertstream.com (192.168.2.10:52000): want_close
cl109.advertstream.com (192.168.2.10:52005): new connection
cl111.advertstream.com (192.168.2.10:43161): want_close
cl111.advertstream.com (192.168.2.10:43166): new connection
cl121.advertstream.com (192.168.2.10:39815): want_close
cl121.advertstream.com (192.168.2.10:39820): new connection

 - watching netstat

tcp        0      0 michel-p6-2312ef.:59821 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52005 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43166 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39820 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59821 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52005 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43166 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39820 cl121.advertstream:http CLOSE_WAIT

 - calling script (3)

cl108.advertstream.com (192.168.2.10:59826): new connection
cl109.advertstream.com (192.168.2.10:52005): want_close
cl109.advertstream.com (192.168.2.10:52010): new connection
cl111.advertstream.com (192.168.2.10:43166): want_close
cl111.advertstream.com (192.168.2.10:43171): new connection
cl121.advertstream.com (192.168.2.10:39820): want_close
cl121.advertstream.com (192.168.2.10:39825): new connection

tcp        0      0 michel-p6-2312ef.:59826 cl108.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:52010 cl109.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:43171 cl111.advertstream:http ESTABLISHED
tcp        0      0 michel-p6-2312ef.:39825 cl121.advertstream:http ESTABLISHED

 - waiting for http timeout...
 - watching netstat

tcp        1      0 michel-p6-2312ef.:59826 cl108.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:52010 cl109.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:43171 cl111.advertstream:http CLOSE_WAIT
tcp        1      0 michel-p6-2312ef.:39825 cl121.advertstream:http CLOSE_WAIT

 - ...


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64398&edit=1


Thread (3 messages)

« previous php.bugs (#185036) next »