Bug in xxxxx_close();

php.internals

Georg Richter

22 years ago
Hi, currently I don't have time to analyze or fix it, so I hope someone will take a look, it affects most of db-extensions which support (optional) default connection. Sample: <?php for ($i=0;$i<1000; $i++) { $link = mysql_connect("localhost", "foo", "bar"); mysql_close(); } ?> doesn't work correct: Function _close_mysql_link which was registered via zend_register_list_destructors_ex will be called only 1 time at end of script. If you specify mysql_close($link) instead, everything works fine. Georg

Unnamed Person

22 years ago
"Georg Richter" <georg@php.net> a écrit dans le message de news:200309132116.45988.georg@php.net...
> Hi, > > currently I don't have time to analyze or fix it, so I hope someone will
take
> a look, it affects most of db-extensions which support (optional) default > connection. > > Sample: > > <?php > for ($i=0;$i<1000; $i++) { > $link = mysql_connect("localhost", "foo", "bar"); > mysql_close(); > } > ?> > > doesn't work correct:
I see, is mysql_close() supposed to close every link opened then ? If so I could work on a patch soon.
> > Function _close_mysql_link which was registered via > zend_register_list_destructors_ex will be called only 1 time at end of > script. If you specify mysql_close($link) instead, everything works fine. > > Georg
-- Regards. M.CHAILLAN Nicolas nicos@php.net www.WorldAKT.com Hébergement de sites internets.

(Marcus Börger)

22 years ago
Hello nicos, Tuesday, September 16, 2003, 9:45:37 AM, you wrote:
> "Georg Richter" <georg@php.net> a écrit dans le message de > news:200309132116.45988.georg@php.net... >> Hi, >> >> currently I don't have time to analyze or fix it, so I hope someone will > take >> a look, it affects most of db-extensions which support (optional) default >> connection. >> >> Sample: >> >> <?php >> for ($i=0;$i<1000; $i++) { >> $link = mysql_connect("localhost", "foo", "bar"); >> mysql_close(); >> } >> ?> >> >> doesn't work correct:
> I see, is mysql_close() supposed to close every link opened then ? If so I > could work on a patch soon.
No, these functions are supposed to close the default link only when called without a link. I had an idea and experimented with a patch, unfortunatley it didn't help. Maybe it gives you some more ideas and something you might want to try out.
-- Best regards, Marcus mailto:helly@php.net

Unnamed Person

22 years ago
----- Original Message ----- From: "Marcus Börger" <marcus.boerger@t-online.de> To: <nicos@php.net> Cc: <internals@lists.php.net> Sent: Tuesday, September 16, 2003 11:28 AM Subject: Re: [PHP-DEV] Re: Bug in xxxxx_close();
> Hello nicos, > > Tuesday, September 16, 2003, 9:45:37 AM, you wrote: > > > > "Georg Richter" <georg@php.net> a écrit dans le message de > > news:200309132116.45988.georg@php.net... > >> Hi, > >> > >> currently I don't have time to analyze or fix it, so I hope someone
will
> > take > >> a look, it affects most of db-extensions which support (optional)
default
> >> connection. > >> > >> Sample: > >> > >> <?php > >> for ($i=0;$i<1000; $i++) { > >> $link = mysql_connect("localhost", "foo", "bar"); > >> mysql_close(); > >> } > >> ?> > >> > >> doesn't work correct: > > > I see, is mysql_close() supposed to close every link opened then ? If so
I
> > could work on a patch soon. > > No, these functions are supposed to close the default link only when
called
> without a link. I had an idea and experimented with a patch, unfortunatley > it didn't help. Maybe it gives you some more ideas and something you might > want to try out.
Do you consider the default link as the last created one?
> > -- > Best regards, > Marcus mailto:helly@php.net
Thank you Marcus. Nicos.

(Marcus Börger)

22 years ago
Hello nicos, Tuesday, September 16, 2003, 11:43:44 AM, you wrote:
> ----- Original Message ----- > From: "Marcus Börger" <marcus.boerger@t-online.de> > To: <nicos@php.net> > Cc: <internals@lists.php.net> > Sent: Tuesday, September 16, 2003 11:28 AM > Subject: Re: [PHP-DEV] Re: Bug in xxxxx_close();
>> Hello nicos, >> >> Tuesday, September 16, 2003, 9:45:37 AM, you wrote: >> >> >> > "Georg Richter" <georg@php.net> a écrit dans le message de >> > news:200309132116.45988.georg@php.net... >> >> Hi, >> >> >> >> currently I don't have time to analyze or fix it, so I hope someone > will >> > take >> >> a look, it affects most of db-extensions which support (optional) > default >> >> connection. >> >> >> >> Sample: >> >> >> >> <?php >> >> for ($i=0;$i<1000; $i++) { >> >> $link = mysql_connect("localhost", "foo", "bar"); >> >> mysql_close(); >> >> } >> >> ?> >> >> >> >> doesn't work correct: >> >> > I see, is mysql_close() supposed to close every link opened then ? If so > I >> > could work on a patch soon. >> >> No, these functions are supposed to close the default link only when > called >> without a link. I had an idea and experimented with a patch, unfortunatley >> it didn't help. Maybe it gives you some more ideas and something you might >> want to try out.
> Do you consider the default link as the last created one?
If no link is open already then a xxx_open() will store the created link as the default link. So in the example the default link would be created and directly closed inside the loop. I think the example doesn't work because the variable $link keeps a second connection to the link. The close call should work on the default link and hence reduce its refcount. When the next assign to $link occures it must be freed before doing so. And that should result in another refcount reduction which should also free the resource and hence close the db connection. Lemme try to unroll the loop and explain what i would expect to happen: $link = mysql_open() // default-link set (refcount=1), $link= (refcount=2) mysql_close() // default-link refcount-- (refcount=1) $link = mysql_open() // new-link, zval_dtor($link)->close default-link, // $link=new-link, ?? what happens to default-link, imo it should be closed // new-link(refcount=1) mysql_close() // ?? nothing no default-link $link = mysql_open() // ?? default-link-set (refcount=1), // zval_dtor($link)->close, $link= default-link (refcount=2) mysql_close() // ... <EOF>
-- Best regards, Marcus mailto:helly@php.net

Zeev Suraski

22 years ago
At 22:16 13/09/2003, Georg Richter wrote:
>Hi, > >currently I don't have time to analyze or fix it, so I hope someone will take >a look, it affects most of db-extensions which support (optional) default >connection. > >Sample: > ><?php > for ($i=0;$i<1000; $i++) { > $link = mysql_connect("localhost", "foo", "bar"); > mysql_close(); > } >?> > >doesn't work correct: > >Function _close_mysql_link which was registered via >zend_register_list_destructors_ex will be called only 1 time at end of >script. If you specify mysql_close($link) instead, everything works fine.
It's actually by-design(tm). mysql_close() will close the default link, but $link still holds it open. Every subsequent call to mysql_connect() will reuse this already opened link. If you explicitly use mysql_close($link), or otherwise get rid of $link (like unsetting it) - it should work. Zeev