Bug #44942 [Com]: exec() hangs apache

From: Date: Wed, 05 Dec 2012 12:04:02 +0000
Subject: Bug #44942 [Com]: exec() hangs apache
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175136@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=44942&edit=1

 ID:                 44942
 Comment by:         claudix dot kernel at gmail dot com
 Reported by:        inqualab1985 at gmail dot com
 Summary:            exec() hangs apache
 Status:             Duplicate
 Type:               Bug
 Package:            Program Execution
 Operating System:   Windows 2000 SP4
 PHP Version:        5.2.5
 Block user comment: N
 Private report:     N

 New Comment:

Seen the same behavior, not only in exec(), but also in similar functions as proc_open/proc_close.
When there are concurrent scripts during a same PHP session, the script spawning the process
randomly hangs.
System:
    - Windows 2003 server SP2
    - Apache 2.2.22/ PHP 5.4.3

I can confirm that calling session_write_close() and then session_start() does the trick. 

I've observed, though, that the code below doesn't work:

    $proc = proc_open($cmd,$pipedesc,$pipes);    
    //do stuff with pipes... 
    //... and close pipes
    session_write_close();  //Close session before hanging function
    $retval = proc_close($proc);
    session_start(); //restore session

But the code below *does* work:
    
    session_write_close();  //Close the session before proc_open()
    $proc = proc_open($cmd,$pipedesc,$pipes);
    //do stuff with pipes... 
    //... and close pipes
    $retval = proc_close($proc);
    session_start(); //restore session

This made me go into the PHP source code (actually the source file "proc_open.c").
I've noticed that the command passed to proc_open() is spawned by calling the WINAPI function
CreateProcess(...) with the parameter "bInheritHandles" set to TRUE. As of MSDN
documentation, if this parameter is TRUE then all handles are inherited by the child process. It
seems that the handle of the session is being inherited by the child process but for some reason the
OS doesn't release it when the process ends, eventually yielding a deadlock. The code snippets
above show this: the session has to be closed before calling proc_open() to prevent the spawned
command from inheriting the session handle. People using exec() cannot see this effect because
exec() virtually embeds proc_open/proc_close.

May this give a clue to PHP developers?

Claudi


Previous Comments:
------------------------------------------------------------------------
[2012-10-15 15:00:52] mail at GerhardBechtold dot com

Pajoye,

I didn't find any documentation on the service sensitivity of the new PHP.

You might be right, that the eof of the console stream is not taken care properly, but in my case
the system is now working (again).

------------------------------------------------------------------------
[2012-10-15 09:12:30] pajoye@php.net

@mail at GerhardBechtold dot com

this is documented, the shell/exec permissions have to be given.

However I do not think it is related to the original issue which is caused by a 
real bug in the php stream, where the eof of the console stream is not correctly 
detected and ends in an endless loop.

------------------------------------------------------------------------
[2012-10-14 15:28:38] mail at GerhardBechtold dot com

After many hours of testing, I managed to solve my problem of exec() calls in PHP. This might be
useful also for other developers, as I have seen many struggling with te same problem.

1. xampp must be installed in real (!) administrator mode (in Windows 7 / 32 & 64 bit). 

2. In many environments, Apache and MySQL should not run as services, but be manually started (even
not with interaction with desktop).

I have put a step-by-step procedure to troubleshoot at:

http://www.gerhardbechtold.com/LUPMIS/Manual/a15_xamppmap_maker_installation.html

(Ignore the references to our system LUPMIS).

Good luck to everybody 
Gerhard

------------------------------------------------------------------------
[2012-09-27 21:02:13] mail at GerhardBechtold dot com

Thanks for looking into my problems with exec() at the latest PHP.

Example for actual code, as in application (was running nicely in earlier PHP installations, but not
under PHP 2.4 anymore):

$str1Name = "C:\Map Maker\MMmacro.exe";	
$str2Name = "command=remove layer";		
exec(chr(34).$str1Name.chr(34).chr(32).chr(34).$str2Name.chr(34));
		
I am using a GIS called Map Maker, with powerful MMmacro functions (www.mapmaker.com). 'remove
layer' is one of the most basic parameters of MMmacro.

A more simple test version also failed:

$str1Name = "C:\Windows\Notepad.exe";
exec($str1Name);

I also tested, all without success:
- with bat file, which then calls notepad.exe
- with exe/bat in different folders: document root (C:\xampp\htdocs) or from calling directory
(C:\xampp\htdocs\lupmis_s)
- with 'start .....'
- with popen
- with exec( .... < file.in > file.out 2> nul", $output);
- with exec(....,$output, $return);
- with exec("ping google.com", $output, $return);

-------------

My environment:

PHP 5.4.4 (VC 9)
Apache 2.4.2
Xampp 'default' installation 1.8 on C:\xampp
Apache with PHP, MySQL running as services

Win 7 Home Premium / 64 bit
4 GB RAM
HP Pavilion g7 Notebook
Bought in Germany, but OS and all programs installed in English
Running as administrator

Total application has > 220 PHP programs, with > 250 exec calls. 

If you give me your full email address, I can also send you the php.ini and httpd.conf.

Thanks for any advice
Gerhard

------------------------------------------------------------------------
[2012-09-27 10:51:24] pajoye@php.net

Tell us which command you use and how you call it, along with the parameters 
(external files and the likes can be sent to my email (my handle @ php.net).

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=44942


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=44942&edit=1


Thread (43 messages)

« previous php.bugs (#175136) next »