From: michel dot sanchez dot 83 at gmail dot com
Operating system: Linux
PHP version: 5.4.12
Package: Streams related
Bug Type: Bug
Bug description:strange stream_socket_client
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 bug report at https://bugs.php.net/bug.php?id=64398&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=64398&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=64398&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=64398&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64398&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64398&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64398&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64398&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64398&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64398&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64398&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=64398&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=64398&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64398&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64398&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64398&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64398&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64398&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64398&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64398&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64398&r=mysqlcfg