From:             kevin at hatry dot com
Operating system: windows XP + linux
PHP version:      4.3.11
PHP Bug Type:     MySQL related
Bug description:  implicit (and redundant) mysql_close on mysql resource 
destruction

Description:
------------
I originally posted this info on follow up of bug id 30525 but it has been
reported as bogus and i dont think the problem has been fully understood.
Please *do* try to understand the problem here as there *is* one !

PHP version used :
-> php 4.3.11 on win32 with the bundled 3.23.49 mysql client
-> php 4.3.11 on linux kernel 2.4.29 with external 4.1.11 mysql client

both with apache 1.3.33 and with the cli binary.

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 but that there now appears to be reference counting before
finally closing the TCP connection which is a better thing for me.

The problem is :
It seems that affecting a value or 'unset'ing a variable of type mysql
resource or living the function in which the variable was created calls
mysql_close on that variable (and so reduces the reference) even if a
previous call was already made on that variable. So the reference count
becomes wrong.

So it is very hard to keep track of the real status (connected, not
connected, of type mysql, of type unknown) of a mysql resource.

The reproduce code is quite simple but is, i think, a typical way of
programming : you have a function (or class) that query mysql by creating
its own resource.
Normally, with this version of PHP, the call to mysql_close should not
close the shared connection so the code is ok to use. But with the bug a
second call to mysl_close is made when the function returns.

A last note : the same bug can be found if, instead of calling the
function "do_something_in_mysql" we do $link1 = -1; after the line
"mysql_close($link1);".

Thank you for your time, i hope you will see the bug this time.

Reproduce code:
---------------
<?php
$link1 = mysql_connect('localhost','root','',false);
$link2 = mysql_connect('localhost','root','',false);

echo "link1 : "; var_dump($link1);
echo "link2 : "; var_dump($link2);

do_something_in_mysql();

// here $link1 and $link2 are still usable to do
// queries which is ok for me

mysql_close($link1);

// here $link2 is no more usable to do queries
// which is *not* ok => the bug.

echo "link1 : "; var_dump($link1);
echo "link2 : "; var_dump($link2);

function do_something_in_mysql () {
 $link3 = mysql_connect('localhost','root','',false);
 echo "link3 : "; var_dump($link3);
 // do some common queries here
 mysql_close($link3);
}
?>


Expected result:
----------------
link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
link3 : resource(4) of type (mysql link)
link1 : resource(4) of type (Unknown)
link2 : resource(4) of type (mysql link)


because $link2 has not been explicitly closed the reference count should
be of 1 instead of 0 (there was 3 calls to mysql_connect and only 2 calls
to mysql_close) and link2 should still be usable.

Actual result:
--------------
link1 : resource(4) of type (mysql link)
link2 : resource(4) of type (mysql link)
link3 : resource(4) of type (mysql link)
link1 : resource(4) of type (Unknown)
link2 : resource(4) of type (Unknown)


$link2 is no longer a mysql resource and the TCP connection to the mysql
server is lost although there was 3 calls to mysql_connect and only 2
explicit calls to mysql_close.

-- 
Edit bug report at http://bugs.php.net/?id=32866&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=32866&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=32866&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=32866&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=32866&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=32866&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=32866&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=32866&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=32866&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=32866&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=32866&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=32866&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=32866&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=32866&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=32866&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=32866&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=32866&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=32866&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=32866&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=32866&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=32866&r=mysqlcfg

Reply via email to