php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74834 stream_socket_shutdown don't work!
Submitted: 2017-06-30 10:39 UTC Modified: 2017-07-03 02:29 UTC
From: 452476865 at qq dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.6.30 OS: ubuntu
Private report: No CVE-ID: None
 [2017-06-30 10:39 UTC] 452476865 at qq dot com
Description:
------------
stream_socket_shutdown don't close the TCP connection.

this cause TCP connection overflow when high concurrency on WEB server.

i think stream_socket_shutdown should close TCP connection, not just TIME_WAIT status.


Test script:
---------------
<?php

function one($host, $port){
	$host = 'tcp://' . $host . ':' . $port;
        $socket = @stream_socket_client(
            $host,
            $errorNumber,
            $errorDescription,
            ini_get("default_socket_timeout")
        );
	stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
}

$host = '192.168.2.243';
$port = 7000;

for($i=0; $i<100; $i++){
    one($host, $port);
}

echo "done";
?>

Expected result:
----------------
netstat -an |grep '192.168.2.243'

return :

none

Actual result:
--------------
netstat -an |grep '192.168.2.243'

return :

tcp        0      0 192.168.2.66:57336      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57282      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57222      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57298      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57236      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57318      192.168.2.243:7000      TIME_WAIT  
tcp        0      0 192.168.2.66:57398      192.168.2.243:7000      TIME_WAIT

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-30 10:42 UTC] kelunik@php.net
No, stream_socket_shutdown doesn't close a connection, use fclose for that.
 [2017-07-01 10:55 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2017-07-01 10:55 UTC] bwoebi@php.net
If you do not have a well-defined reason for not using fclose(), better use fclose() rather than stream_socket_shutdown(STREAM_SHUT_RDWR).

It will remain in TIME_WAIT until you actually close the resource (and implicitly the file descriptor beyond it).
 [2017-07-03 02:29 UTC] 452476865 at qq dot com
it's still at TIME_WAIT status using fclose with two method.



method one==========================

function connectRedis($host, $port){
	$host = 'tcp://' . $host . ':' . $port;
        $socket = @stream_socket_client(
            $host,
            $errorNumber,
            $errorDescription,
            ini_get("default_socket_timeout")
        );
	//stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
        fclose($socket);
}

$host = '192.168.2.243';
$port = 7000;

for($i=0; $i<100; $i++){
    connectRedis($host, $port);
}

echo "done\r\n";

method two==========================

function connectRedis($host, $port){
	$host = 'tcp://' . $host . ':' . $port;
        $socket = @stream_socket_client(
            $host,
            $errorNumber,
            $errorDescription,
            ini_get("default_socket_timeout")
        );
	stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
        fclose($socket);
}

$host = '192.168.2.243';
$port = 7000;

for($i=0; $i<100; $i++){
    connectRedis($host, $port);
}

echo "done\r\n";
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC