php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64398 strange stream_socket_client
Submitted: 2013-03-09 09:05 UTC Modified: 2014-12-30 10:41 UTC
From: michel dot sanchez dot 83 at gmail dot com Assigned:
Status: No Feedback Package: Streams related
PHP Version: 5.4.12 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-03-09 09:05 UTC] 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

 - ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-02 12:34 UTC] mike@php.net
-Status: Open +Status: Feedback
 [2014-04-02 12:34 UTC] mike@php.net
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.


 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC