From: l dot cameron2 at ugrad dot unimelb dot edu dot au Operating system: Fedora Core 2 PHP version: 5.0.2 PHP Bug Type: MySQL related Bug description: mysql_close() fails in some situations with shared links
Description: ------------ Preface: I *do* understand that by default MySQL connections are shared in PHP. I also note that in older versions a single mysql_close() would close all of the links; see #9107: there now there appears to be reference counting before finally closing the TCP connection -- which is IMHO better than the older behaviour, but the implementation has its own bugs: PHP appears to keeps an internal count of the number of times a link has been duplicated. When the link count is <= 0, the underlying TCP connection is actually closed. * close() reduces the link count by 1 * setting the connection to null *also* reduces the link count by 1 -- even if that link has already been close()d Currently the only workarounds for this are to: [1] Set new_link to true every time you mysql_connect() -- potentially creating a lot of TCP connections and slowing the program down [2] close() the link, but never set it to null and hope that PHP won't clean it up until the end of the program: This *will* fail sometimes though; see the example at http://www.levi.id.au/mysql4.php.txt [3] Never mysql_close() links, only set them to null and hope that PHP will in fact clean up the TCP connection before MySQL runs out of available connections (admittedly only a problem when you have a lot of simultaneous connections to your database) -- this does work now, but we're not supposed to assume anything about when PHP does its object destruction. The third is really the only viable solution; but is dependent on the internal implementation of the MySQL extension. At best, the current situation is that if you ever have shared links, you should never call mysql_close if you ever expect to use that database again in your program. Reproduce code: --------------- Simple example: #!/usr/local/bin/php -q <? $conn1 = mysql_connect('localhost:3306', 'levi', 'DaCr0n!'); $conn2 = mysql_connect('localhost:3306', 'levi', 'DaCr0n!'); mysql_select_db('surveytest', $conn1); mysql_select_db('surveytest', $conn2); mysql_close($conn1); $conn1 = null; mysql_close($conn2); $conn2 = null; ?> See also the example at http://www.levi.id.au/mysql4.php.txt Expected result: ---------------- Blank output. Actual result: -------------- PHP Warning: mysql_close(): 1 is not a valid MySQL-Link resource in /home/levi/public_html/mysql2.php on line 10 <br /> <b>Warning</b>: mysql_close(): 1 is not a valid MySQL-Link resource in <b>/home/levi/public_html/mysql2.php</b> on line <b>10</b><br /> (If I remove the mysql_close($conn1); it works) (If I remove the $conn1 = null; it also works) -- Edit bug report at http://bugs.php.net/?id=30525&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=30525&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=30525&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=30525&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=30525&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=30525&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=30525&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=30525&r=needscript Try newer version: http://bugs.php.net/fix.php?id=30525&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=30525&r=support Expected behavior: http://bugs.php.net/fix.php?id=30525&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=30525&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=30525&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=30525&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=30525&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=30525&r=dst IIS Stability: http://bugs.php.net/fix.php?id=30525&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=30525&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=30525&r=float MySQL Configuration Error: http://bugs.php.net/fix.php?id=30525&r=mysqlcfg