|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46740 PHP Not Cleanly Closing MySQL Connections on Apache Death
Submitted: 2008-12-03 15:45 UTC Modified: 2014-12-30 20:53 UTC
Avg. Score:4.6 ± 0.7
Reproduced:25 of 25 (100.0%)
Same Version:8 (32.0%)
Same OS:3 (12.0%)
From: php at matthewboehm dot com Assigned: jacob (profile)
Status: Closed Package: PDO MySQL
PHP Version: 5.2.9 OS: RHEL5.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: php at matthewboehm dot com
New email:
PHP Version: OS:


 [2008-12-03 15:45 UTC] php at matthewboehm dot com
When using PHP/MySQL with persistent connections, upon the death of 
an apache child, you get an 'aborted connection' message inside the 
MySQL server's error log.

In my supremely most humbled opinion, I can only presume that PHP is 
not cleanly closing opened MySQL connections when an apache child 
dies and the PHP module is unloaded from apache's memory.

Reproduce code:
To reproduce:

 1. Start httpd (I used 2.2.10) with PHP 5.2.6. httpd should use the prefork MPM with the following settings on the MPM:
    StartServers           5
    MinSpareServers        5
    MaxSpareServers       10
    MaxRequestsPerChild 1000

 2. Browser the following PHP file several times to initiate several persistent connections to your MySQL database. This can be either of the MySQL methods that support persistence. (mysql_pconnect, and PDO)

        $dbh = new PDO('mysql:host=DBHOST;dbname=DBNAME', 'DBUSER', 'DBPASS', array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

        if($res = $dbh->query("SELECT * FROM myTable"))
                print "Query success";
                print "Query failed";
catch(PDOException $e)
        echo 'Error: ' . $e->getMessage();

3. Stop apache via any means. You will see in your MySQL log 1 aborted connection line for each terminated apache process.

In my testing, I used 'ab' to throw 10,000 requests of the above code at apache. Every 1000 requests, I would see the error in MySQL. This is because of the MaxRequestsPerChild setting to be 1000. Every 1000, an apache thread will suicide and then spawn a new thread.

Once the load test was complete, if more than MaxSpareServers threads where running, apache would kill them off as well and you would see that same number of errors in MySQL.

Expected result:
I would not expect to see any errors from MySQL. I would expect all 
connections to be properly closed upon the death of an apache 

Actual result:
The actual result is an incorrectly closed MySQL connection.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-01 14:34 UTC] dstuff at brainsware dot org
Got the same problem using PHP version 5.2.9 on Debian (Lenny)
 [2009-07-08 16:18 UTC] dmlance at gmail dot com
Same problem with 2.2.11 and 5.2.10. 
Due high load on my server with croned scripts I got about 60%  aborted connections in my logs.
 [2012-05-04 14:47 UTC] justice dot libra dot boyz at gmail dot com
yeah i found same problem using
any patch from inside php engine guys?
 [2012-05-04 15:38 UTC] justice dot libra dot boyz at gmail dot com

php.ini >> mysql.allow_persistent = off
 [2014-01-01 12:51 UTC]
-Package: PDO related +Package: PDO MySQL
 [2014-12-30 20:53 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: jacob
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 05:03:33 2021 UTC