|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37998 Parent process lost MySQLi connection after child process gone
Submitted: 2006-07-03 15:13 UTC Modified: 2007-11-19 11:25 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dbs at is dot ua Assigned: georg (profile)
Status: Not a bug Package: MySQLi related
PHP Version: 5.1.4 OS: linux-2.4.32
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.
Block user comment
Status: Assign to:
Bug Type:
From: dbs at is dot ua
New email:
PHP Version: OS:


 [2006-07-03 15:13 UTC] dbs at is dot ua
Parent process lost MySQLi connection when his child gone by external kill (*NIX `kill') or from self by posix_kill($pid, $sig);

Have different database users, different databases, separate connections.

Reproduce code:

declare(ticks = 1);

define("MYSQL_HOST", "");
define("MYSQL_DB1", "db1");
define("MYSQL_DB2", 'db2');
define("MYSQL_USER1", "root1");
define("MYSQL_PASS1", 'pass1');
define("MYSQL_USER2", "root2");
define("MYSQL_PASS2", 'pass2');
define("MAX_THREADS", 3);

class WTF_child {

    var $ident; // numeric thread identifier
    var $pid;   // my PID
    var $func;  // some functions container
    var $db;    // database object container
    var $daemon = true;

    function WTF_child($ident, &$f) {
        $this->ident = $ident;
        $this->pid = posix_getpid();
        $this->func = $f;
        pcntl_signal(SIGTERM, array(&$this, "sig_Chandler"));
        $this->func->db_connect(MYSQL_USER2, MYSQL_PASS2, MYSQL_DB2, $this->db, $this);

    function go() {
        while ($this->daemon) {
            if ($rs = $this->db->query("select " . $this->ident)) {
                list($id) = $rs->fetch_row();
                echo "\tchild process [".$this->pid."]: ".$id."\r\n";

    function sig_Chandler($sig) {
        switch ($sig) {
            case SIGTERM:
                echo "\tchild process [".$this->pid."]: SIGTERM received\r\n";
                $this->daemon = false;

class WTF_parent {

    var $pid;  // my process ID
    var $func; // some functions
    var $db;   // db object
    var $childs = array();
    var $daemon = true;

    function WTF_parent() {
        $this->pid = posix_getpid();
        $this->func = new func();
        pcntl_signal(SIGTERM, array(&$this, "sig_Phandler"));
        $this->func->db_connect(MYSQL_USER1, MYSQL_PASS1, MYSQL_DB1, $this->db, $this);

    function go() {
        for ($thread_no = 1; $thread_no <= MAX_THREADS; $thread_no++) {
            $pid = pcntl_fork();
            if ($pid == -1) {
                die("can't fork!");
            } elseif ($pid != 0) {
                // parent space
                $this->childs[$pid] = $thread_no;
            } else {
                // child space
                $child = new WTF_child($thread_no, $this->func);

        while ($this->daemon) {
            foreach ($this->childs as $cpid => $ident) {
                $rs = pcntl_waitpid($cpid, &$status, WNOHANG);
                if ($rs == $cpid || $rs == -1) {
                    echo "parent process [".$this->pid."]: who kill my thread #".$ident." with PID = ".$cpid." ? :(\r\n";
            if ($rs = $this->db->query("select 'nice job'")) {
                list($str) = $rs->fetch_row();
                echo "parent process [".$this->pid."]: ".$str."\r\n";

    function sig_Phandler($sig) {
        switch ($sig) {
            case SIGTERM:
                echo "parent process [".$this->pid."]: SIGTERM received\r\n";
                $this->daemon = false;

class func {

    function db_connect($user, $pass, $db, &$db_obj, &$caller) {
        $db_obj = new mysqli(MYSQL_HOST, $user, $pass, $db);
        if (mysqli_connect_errno()) {
            echo "[", get_class($caller), ", PID = ", $caller->pid, "]: database connection failed: " , mysqli_connect_error();

    function db_close(&$db_obj) {

$parent = new WTF_parent();



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-18 21:18 UTC]
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

 [2006-10-26 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-11-19 11:25 UTC]
After a fork the two processes share the file handle for the mysql connection socket,
when one of them terminates it closes its open file handles and so the shared connection
socket gets closed for both -> so expected behavior ...
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 04:01:34 2024 UTC