php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23649 Lost connection to MySQL server during query after pcntl_fork
Submitted: 2003-05-15 17:41 UTC Modified: 2003-05-30 14:39 UTC
From: sthomas at townnews dot com Assigned:
Status: Closed Package: MySQL related
PHP Version: 4.3.2RC2 OS: Redhat Linux 9.0
Private report: No CVE-ID: None
 [2003-05-15 17:41 UTC] sthomas at townnews dot com
To reproduce, simply run this script from the PHP CLI:

<?PHP
print "This is before the fork!\n";

mysql_connect('remote.host', 'user', 'pass');
mysql_select_db('database');

print "Query 1:";
mysql_query('select 1');
print mysql_error();
print "\n";

if (pcntl_fork())
  exit();

print "Query 2:";
mysql_query('select 1');
print mysql_error();
print "\n";

mysql_close();
?>

This will almost always produce a "Lost connection to MySQL server during query" error for the second query.  Though I did notice that it seems to happen only if the host is a remote host.

We are using PHP 4.0.12 as the server, and this bug seems to occour whether we use the included mysql client libraries or compile against the 4.0.12 libraries.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-15 18:51 UTC] sniper@php.net
Does this work if you don't have pcntl_fork() there?



 [2003-05-16 08:40 UTC] sthomas at townnews dot com
Actually yes, it does.  The fork itself seems to be causing this.  All I have to do to see that is comment out the fork/exit and it works just fine.  Unfortunately one of our crazy developers wrote a daemon that maintains a connection to our database and distributes it to child forks to avoid innundating our DB with connections, so this is really screwing us.  It still does this at least all the way back to PHP 4.3.0 - as that's what we're running in production and where we first noticed this.

And I'm not sure it's related, but the PHP 4.3.0 version would print the "This is before the fork!" twice, as if the child was running the whole script.  It doesn't do this in PHP 4.3.2RC2, but after doing a diff to the pcntl code between those versions, there are no differences.  I can't personally see how internals would affect the forking model, so I don't know what's going on.  I can provide you with a strace if you need it - since I can't get a backtrace.
 [2003-05-30 14:39 UTC] georg@php.net
You can't use same connection. You have to establish a new connection inside the forked process.
 [2003-11-29 10:23 UTC] chris dot noden at monstermob dot com
PHP 4.3.3 on RedHat Advanced Server 2.1

I get a similar problem.  The forked (child) process loses the connection the MySQL - apparently during a pcntl_waitpid from the parent.

eg. - all running in CLI mode:
A PHP script forks a child (or many) which go off (reconnecting to MySQL first) and do their stuff.  The parent (original) PHP script periodically checks on the status of the children using pnctl_waitpid querying each child pid in turn using the WUNTRACED option (WNOHANG does not work very well leaving defunct children everywhere).

Sometimes (not always) the child process is disconnected from MySQL and I can't seem to sort it out.  Have tried many workarounds.

The MySQL error is 2013 - Lost connection to MySQL server during query.

Sometimes (less often) the mysql_fetch_array and mysql_num_rows can also fail after a succesful query, assuming that this is caused by the same thing - MySQL being disconnected.
 [2021-04-06 10:40 UTC] git@php.net
Automatic comment on behalf of roojs
Revision: https://github.com/php/pecl-tools-svn/commit/aa7ec08152201302dd89da709b2ccdbf4115c27a
Log: fix #23649 - allow deletion of properties by using NULL as value (patch by Jonas Zeiger)
 [2021-04-06 10:40 UTC] git@php.net
-Status: Not a bug +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 22 20:00:03 2025 UTC