From: rudy dot metzger at xs4all dot nl
Operating system: Fedora Core 3
PHP version: 5.0.3
PHP Bug Type: MySQLi related
Bug description: MySQLi disconnects after termination of child in master
Description:
------------
When connecting to a database, then forking off a new child, the database
connection will be terminated (disconnected) when the child exits.
I have the feeling that at the "cleanup" of the child process the database
connection is closed. This should not be the case because the master
process might still use it.
This is not the case with the mysql extension, only with mysqli. With the
mysql extension, you might be required to give the optional parameter
"open new session" on the connect. at least this is what I did.
Reproduce code:
---------------
<?php
function ping_db( $res, $msg )
{
if ( mysqli_ping($res) )
echo getmypid()." $msg: still alive\n";
else
echo getmypid()." $msg: died\n";
}
function SigChild( $signal )
{
while ( ($pid=pcntl_wait($status,WNOHANG)) > 0 ) {
if ( !pcntl_wifexited($status) )
echo "Collected killed pid $pid\n";
else
echo "Collected pid $pid\n";
}
}
declare(ticks=1);
pcntl_signal( SIGCHLD, 'SigChild' );
$res = mysqli_connect('localhost','zpc','zpc');
ping_db($res,'after connect');
$pid = pcntl_fork();
ping_db($res,'right after fork');
if ( !$pid ) { // --- the slave
echo "child sleep\n";
sleep(5);
echo "child exiting\n";
exit(0);
}
ping_db($res,'after fork');
echo "master sleep\n";
sleep(10);
ping_db($res,'after sleep');
while ( ($pid=pcntl_wait($status)) > 0 ) {
if ( !pcntl_wifexited($status) )
echo "Collected killed pid $pid\n";
else
echo "Collected pid $pid\n";
}
mysqli_close( $res );
?>
Expected result:
----------------
The database connection in the master should not have terminated.
Actual result:
--------------
6311 after connect: still alive
6313 right after fork: still alive
child sleep
6311 right after fork: still alive
6311 after fork: still alive
master sleep
child exiting
Collected pid 6313
6311 after sleep: died
In the last line you can see that the connection died (6311 is master,
6313 is child)
--
Edit bug report at http://bugs.php.net/?id=31538&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=31538&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=31538&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=31538&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=31538&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=31538&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=31538&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=31538&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=31538&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=31538&r=support
Expected behavior: http://bugs.php.net/fix.php?id=31538&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=31538&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=31538&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=31538&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=31538&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=31538&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=31538&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=31538&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=31538&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=31538&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=31538&r=mysqlcfg