php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45590 pcntl_fork() with MySQL queries
Submitted: 2008-07-22 14:48 UTC Modified: 2008-07-22 17:12 UTC
From: alan dot blotz at students dot uni-mainz dot de Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.6 OS: Debian Lenny
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.
Password:
Status:
Package:
Bug Type:
Summary:
From: alan dot blotz at students dot uni-mainz dot de
New email:
PHP Version: OS:

 

 [2008-07-22 14:48 UTC] alan dot blotz at students dot uni-mainz dot de
Description:
------------
Already a long time ago the bug http://bugs.php.net/bug.php?id=38228 was active, but I experienced some problems with the pcntl_fork() function today. I already fixed the problem with multiple children connecting to the MySQL server by using mysql_connect(..., true) or mysql_pconnect() (the latter I'm using right now). My problem is, that with any of these methods I'm partly getting wrong results to queries (see code). This really NOT happening always, just sometimes when you don't expect it.

I can't post the original code due to copyrights, but the example below should give you an idea of what is happening. I'm working with multiple children in my application (executed as CLI), at least five at a time. Is it perhaps a memory problem of PHP and / or MySQL?


Alan

Reproduce code:
---------------
// Queries for five children
for ($i = 0; $i < 5; $i++) {
	
	// If I am the child
	if (!pcntl_fork()) {
		
		switch ($i) {
			
			case 0:
				$sql = 'SELECT a, b FROM t';
				break;
			case 1:
				$sql = 'SELECT c FROM s';
				break;
			case 2:
				$sql = 'SELECT x FROM s';
				break;
			case 3:
				$sql = 'SELECT a, b, d FROM t';
				break;
			case 4:
				$sql = 'SELECT d FROM t';
				break;
		}
		
		$result = mysql_query($sql);
		// Get the result from DB with mysql_fetch_array() for example
	}
}

Expected result:
----------------
Result of columns a and b from table t in first child
Result of column c from table s in second child
Result of column x from table s in third child
Result of columns a, b and d from table t in fourth child
Result of column d from table t in fifth child

Actual result:
--------------
For example (some worst case):

Nothing in first child
Result of column x from table s in second child
Result of column c from table s in third child
Result of columns a and b from table t in fourth child
Result of column d from table t in fifth child

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-22 17:12 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You must not use the same connection from different processes. The protocol only allows a single query per time so the queries from the different childs end up in a queue and the answers are read by the process which randomly expects some results at the correct time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC