php.net |  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
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: 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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dbs at is dot ua
New email:
PHP Version: OS:

 

 [2006-07-03 15:13 UTC] dbs at is dot ua
Description:
------------
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:
---------------
#!/usr/local/bin/php
<?php

declare(ticks = 1);

define("MYSQL_HOST", "192.168.102.10");
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);
        $this->go();
        $this->func->db_close($this->db);
    }

    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";
                $rs->close();
            }
            sleep(3);
        }
    }

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

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);
        $this->go();
        $this->func->db_close($this->db);
    }

    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);
                die();
            }
        }

        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";
                    unset($this->childs[$cpid]);
                }
            }
            if ($rs = $this->db->query("select 'nice job'")) {
                list($str) = $rs->fetch_row();
                echo "parent process [".$this->pid."]: ".$str."\r\n";
                $rs->close();
            }
            sleep(5);
        }
    }

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

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();
            die();
        }
    }

    function db_close(&$db_obj) {
        $db_obj->close();
    }
}

$parent = new WTF_parent();

?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-18 21:18 UTC] tony2001@php.net
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] hholzgra@php.net
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: Thu Dec 26 13:01:30 2024 UTC