php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38228 Pcntl based parallel threads loose connection to MySQL server
Submitted: 2006-07-26 20:09 UTC Modified: 2006-08-29 21:26 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: piotr at bulczak dot name Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.1.4 OS: Fedora Core 5 Linux
Private report: No CVE-ID: None
 [2006-07-26 20:09 UTC] piotr at bulczak dot name
Description:
------------
For long time I was using multithreaded scripts (pcntl module). Each script was using their own connection to MySQL server (mysql module). They are working properly on FC1 - FC4 linuxes (PHP 4.3.11 - 5.0.4). I tried for the first time to tun them on FC5 which includes standard rpm packages - PHP 5.1.4 in this case (iptables and selinux turned off).

I noticed that my threads fail with message "server has gone away" when using "mysql_select_db. The behaviour is the same regardless if it is connection via unix socket file, tcp/ip connection to localhost or some remote IP address. It does not matter what MySQL server version I am connecting to (tried with 3.23.58 - 5.0.22)

I tried also switch to "mysqli" module on FC5 and the script started working properly again so I assume only "mysql" module is affected.

I do not have major changes to standard php.ini... just max_execution_time=300 and memory_limit=32M. Test script is just a minimal version (without any threads synchronization, etc. stuff).

Please help :-)

Reproduce code:
---------------
http://www.buleksoft.pl/mysql_pcntl_test.html

Expected result:
----------------
[root@host ~]# php -q test.php
Forking thread number 0
Thread 0 doing some mysql tasks
Forking thread number 1
Thread 1 doing some mysql tasks
Forking thread number 2
Thread 2 doing some mysql tasks
Forking thread number 3
Thread 3 doing some mysql tasks


Actual result:
--------------
[root@host ~]# php -q test.php
Forking thread number 0
Thread 0 doing some mysql tasks
Forking thread number 1
Thread 1: Could not select DB because MySQL server has gone away
Forking thread number 2
Thread 2: Could not select DB because MySQL server has gone away
Forking thread number 3
Thread 3: Could not select DB because MySQL server has gone away


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-26 23:23 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2006-07-27 18:25 UTC] piotr at bulczak dot name
I compiled php5.2-latest.tar.gz on FC5 with configure options '--enable-pcntl' '--with-mysql' '--with-mysqli' '--with-pdo-mysql'. Undoftunately the bug still exist in this version. My test script produced again error message "MySQL server has gone away".

Looking for some workaround I noticed that the problem does not occure if I close all MySQL connections in parrent script before forking childs. Maybe this info will help to spot problem source.
 [2006-08-17 14:11 UTC] tony2001@php.net
Try with mysql_connect ( <server>, <username>, <password>, TRUE);
 [2006-08-18 19:09 UTC] piotr at bulczak dot name
I first tried mysql_connect with fourth parameter set to TRUE only in parent process - it did not work. Then I set the same also in child threads. It looks like it solved the problem... all the threads did not indicate any MySQL errors this time.

So I am actually not sure if it is problem resolution or just workaround. Anyway it helped... many thanks for this suggestion!

Cheers,
Piotr
 [2006-08-29 21:26 UTC] tony2001@php.net
Child processes were trying to use parents connection resource (because mysql_connect() tries to look for an existing connection first), so using the 4th parameter of mysql_connect() is the solution.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 01:01:28 2024 UTC