php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55473 mysql_pconnect leaks file descriptors on reconnect
Submitted: 2011-08-21 10:52 UTC Modified: 2011-08-22 10:57 UTC
From: littlesavage at rambler dot ru Assigned: andrey (profile)
Status: Closed Package: MySQL related
PHP Version: 5.3.7 OS:
Private report: No CVE-ID: None
 [2011-08-21 10:52 UTC] littlesavage at rambler dot ru
Description:
------------
Whem Mysql closes created by mysql_pconnect() peristent connection, file descriptor lefts opened and is never reused.
I have a server that stops working due to opened file descriptors limit.

Test script:
---------------
<?php

function connect($passwd) {
   $conn = mysql_pconnect('localhost:3306','root', $passwd);
   if (!$conn)
      die(mysql_error()."\n");
   mysql_query("set wait_timeout=5", $conn);
   return $conn;
}

$passwd = $argv[1];
$conn = connect($passwd);

for($i=0;$i<10;$i++) {
   /* wait while mysql closes connection */
   sleep(6);

   if (!mysql_ping($conn)) {
      echo "reconnect\n";
      $conn = connect($passwd);
   }

   $r = mysql_query('select 1', $conn);
   $error = $r ? 'OK' : mysql_error();
   $opened_files = trim(exec("lsof -np " . getmypid() . " | wc -l"));
   echo "$error. opened files: $opened_files\n";
}

?>


Expected result:
----------------
$ php ./test.php
reconnect
OK. opened files: 76
reconnect
OK. opened files: 76
reconnect
OK. opened files: 76
reconnect
OK. opened files: 76
...

Actual result:
--------------
$ php ./test.php
reconnect
OK. opened files: 76
reconnect
OK. opened files: 77
reconnect
OK. opened files: 78
reconnect
OK. opened files: 79
...

Patches

Bug55473.diff (last revision 2011-08-22 08:33 UTC by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-22 03:05 UTC] laruence@php.net
I can't reproduce this bug in PHP-5.3.7 on Linux Redhat(64-bit).
What's your OS type?
 [2011-08-22 06:20 UTC] littlesavage at rambler dot ru
I have found this bug on Debian squeeze (x86_64), php PHP 5.3.6-6~dotdeb.1 with Suhosin-Patch, mysql 5.1.58.
Then i repeat it on FreeBSD 9, php 5.3.7 with Suhosin-Patch, mysql 5.5.12.

On FreeBSD i have found that this bug reproducable only when i use mysqlnd native driver. Everything work as expected with standart mysql driver.
 [2011-08-22 08:33 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: Bug55473.diff
Revision:   1314002014
URL:        https://bugs.php.net/patch-display.php?bug=55473&patch=Bug55473.diff&revision=1314002014
 [2011-08-22 08:35 UTC] laruence@php.net
I found this is due to, when doing a reconnect, mysqlnd_connect didn't free the 
stream. I have submitted a patch, but need georg&andrey&ulf to review first..
 [2011-08-22 08:35 UTC] laruence@php.net
-Status: Open +Status: Analyzed
 [2011-08-22 09:01 UTC] laruence@php.net
and the reason for why this cleanup cound not be done in dtor,  is that in dtor, 
it also clean the content, option, which should not free at that memont,

and that is not a dtor time in theory.
 [2011-08-22 10:42 UTC] andrey@php.net
Automatic comment from SVN on behalf of andrey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=315270
Log: Fix Bug #55473 	mysql_pconnect leaks file descriptors on reconnect
The fix is for now in 5_4 and trunk, to be merged into 5_3 after 5.3.8
is packaged (expected today). The test case goes to all branches
 [2011-08-22 10:57 UTC] laruence@php.net
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: andrey
 [2011-08-22 10:57 UTC] laruence@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.4-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2011-08-23 19:19 UTC] neweracracker at gmail dot com
Maybe its time to apply in 5.3?

http://svn.php.net/viewvc?view=revision&revision=315270
 [2012-02-28 06:46 UTC] jim at addmired dot com
We are still getting hit by this bug. This patch hasn't been applied in 5.3 
releases yet, apparently. It was supposed to go into 5.3.8 but still no joy. =( 
Any hope of this getting into the code now? =(
 [2012-04-18 09:49 UTC] laruence@php.net
Automatic comment on behalf of andrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9baee858445278ea1efe45414440b4dd0566c922
Log: Fix Bug #55473 	mysql_pconnect leaks file descriptors on reconnect The fix is for now in 5_4 and trunk, to be merged into 5_3 after 5.3.8 is packaged (expected today). The test case goes to all branches
 [2012-07-24 23:40 UTC] rasmus@php.net
Automatic comment on behalf of andrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9baee858445278ea1efe45414440b4dd0566c922
Log: Fix Bug #55473 	mysql_pconnect leaks file descriptors on reconnect The fix is for now in 5_4 and trunk, to be merged into 5_3 after 5.3.8 is packaged (expected today). The test case goes to all branches
 [2013-11-17 09:36 UTC] laruence@php.net
Automatic comment on behalf of andrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9baee858445278ea1efe45414440b4dd0566c922
Log: Fix Bug #55473 	mysql_pconnect leaks file descriptors on reconnect The fix is for now in 5_4 and trunk, to be merged into 5_3 after 5.3.8 is packaged (expected today). The test case goes to all branches
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC