Edit report at http://bugs.php.net/bug.php?id=52948&edit=1
ID: 52948 Updated by: cataphr...@php.net Reported by: gilles dot rayrat at continuent dot com Summary: Improper connection closing logic leads to TIME_WAIT sockets on server Status: Bogus Type: Bug Package: MySQLi related Operating System: Any PHP Version: 5.2.14 Block user comment: N New Comment: read: "this is about mysql's C client library, not mysqlnd." Previous Comments: ------------------------------------------------------------------------ [2010-10-01 19:54:34] cataphr...@php.net If you're not using mysqlnd, it's not under our control how the connection is closed. We just rely on the mysql library that's installed in the system; whatever socket is used, it's opaque to us. I have serious doubts that calling shutdown() would make any difference whatsoever. See http://lxr.free-electrons.com/source/net/ipv4/tcp.c#L1871 and compare with tcp_shutdown. In any case, I'm closing as bogus as this is not about mysql's C client library, not mysqlnd. ------------------------------------------------------------------------ [2010-10-01 18:58:40] gilles dot rayrat at continuent dot com So I'm not using mysqlnd at all. This behavior is not predictable and probably depends on the link and on the velocity of both ends. The shutdown calls allow better control over time_wait tcp connection state, so why not introducing an option so that the user can choose the behavior he wants. Note that there are some threads where sys admins complain about these numerous time_wait sockets and they might appreciate the trick. For the record, here is the mysql (non i) code: <?php # Simple script to demonstrate the TIME_WAIT issue # Connects to the database, nothing more $host = "u2"; # Host where the Tungsten Connector is running $port = 3306; # Using the Tungsten Connector port $username = "tungsten"; $password = "secret"; $dbname = "test"; echo "Connecting to $host $username $password $dbname $port\n"; # Make the connection $connection = mysql_connect($host, $username, $password); mysql_close($connection); ?> ------------------------------------------------------------------------ [2010-10-01 18:12:22] cataphr...@php.net You can check if you're using mysqlnd with phpinfo() or php -i, if in the CLI. Example: php -i | grep mysqlnd Client API library version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $ mysqlnd mysqlnd => enabled Version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $ If you're not using mysqlnd, then it's not our problem to fix. See: http://www.php.net/manual/en/mysqlnd.overview.php As to the shutdown implementation in Linux, it doesn't seem to do a whole lot with SHUT_RD (in terms of packets sent): http://lxr.free-electrons.com/source/net/ipv4/af_inet.c#L768 (note the how++ on line #776) http://lxr.free-electrons.com/source/net/ipv4/tcp.c#L1852 SHUT_RW would force a FIN, but my tests show a FIN is always being sent by the client. The only variable is if the FIN reaches the server before it sends its own FIN. If it does reach it before, then we have a TIME_WAIT on the client; otherwise we have a simultaneous termination and TIME_WAIT on both. There's no way we would be able to prevent this. Web servers typically wait a few seconds to allow the client to disconnect and avoid the TIME_WAIT, maybe the mysql server could do the same. In any case, it's out of our hands. ------------------------------------------------------------------------ [2010-10-01 17:26:22] gilles dot rayrat at continuent dot com Re-reading my previous comment, I realized that the 1st paragraph can be misunderstood. No offense, I just need you to tell me how to provide more accurate info Cheers, Gilles. ------------------------------------------------------------------------ [2010-10-01 17:18:55] gilles dot rayrat at continuent dot com I'm not sure what I'm using, I am not a php user. I run ubuntu 9.10 and got the package "php5-mysql". If you need further info, please advise on how to collect it. I also encountered the case of double time_wait, I guess that the behavior is not fully predictable, and depends on whom between server and client starts the connection closing first. Using, on client, shutdown(sock, SHUT_RD) before sending the quit command, then running shutdown(sock, SHUT_WR) will give more predictable (and, at least to me, appropriate) results ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=52948 -- Edit this bug report at http://bugs.php.net/bug.php?id=52948&edit=1