php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31538 MySQLi disconnects after termination of child in master
Submitted: 2005-01-13 15:42 UTC Modified: 2005-02-28 21:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: rudy dot metzger at xs4all dot nl Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.0.3 OS: Fedora Core 3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rudy dot metzger at xs4all dot nl
New email:
PHP Version: OS:

 

 [2005-01-13 15:42 UTC] rudy dot metzger at xs4all dot nl
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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-13 15:45 UTC] rudy dot metzger at xs4all dot nl
Used DB versions

MySQL-devel-4.1.7-0
MySQL-shared-4.1.7-0
MySQL-server-4.1.7-0
MySQL-client-4.1.7-0
 [2005-02-14 19:23 UTC] peter at netkey dot at
You need a seperate connection for each child, or MySQL will get confused. 
See http://dev.mysql.com/doc/mysql/en/gone-away.html the comment from Christophe C.

HTH
 [2005-02-28 21:13 UTC] sniper@php.net
..

 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun May 09 20:01:23 2021 UTC