|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-17 15:35 UTC] pgj at ds-fr dot com
Description:
------------
I launch processes in a script and put stout in file
after more than 2000 processes and can't open a new one and have this message.
It's similar with 30743 bit it uses 2 pipes for $fileDescriptors and in this case it's a pipe and a file
Reproduce code:
---------------
<?
set_time_limit(6000);
$i=0;
mkdir(test);
while(1)
{
$i++;
echo("-$i--\n");
invoke($i);
}
function invoke($i)
{
$commandLine = "echo hello";
$fileDescriptors = array(
1 => array("pipe", "r"),
2 => array("file", "test/$i.txt","w")
);
$pipes = array();
$processHandle = proc_open($commandLine,$fileDescriptors, $pipes);
if (is_resource($processHandle))
{
fclose($pipes[1]);
proc_close($processHandle);
}
}
Expected result:
----------------
Open a new one
Actual result:
--------------
<b>Warning</b>: proc_open(test/2587.txt): failed to open stream: Too many open files in <b>E:\cotcot\leak.php</b> on line <b>24</b><br />
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 03:00:01 2025 UTC |
It appears the problem is actually due to way Windows internally emulate POSIX file descriptors, i.e. the process runs out of the allowed count of the 'POSIX' file descriptors tracked in the C library, not handles per se. This is supported by Windows process viewer, which shows ~constant number of handles for PHP process during the script run, particularily same number of handles when it runs OK and when it starts giving the error. Note the problem only appears when using 'file' in descriptorspec, using 'pipe' instead does workaround the problem. I believe the problem can be fixed by consistently closing the file descriptors rather then just the handles, i.e something along: --- ext/standard/proc_open.c.orig 2006-05-28 21:51:09.000000000 +0200 +++ ext/standard/proc_open.c @@ -462,6 +462,9 @@ static inline HANDLE dup_fd_as_handle(in struct php_proc_open_descriptor_item { int index; /* desired fd number in child process */ php_file_descriptor_t parentend, childend; /* fds for pipes in parent/child */ +#ifdef PHP_WIN32 + int childend_desc; +#endif int mode; /* mode for proc_open code */ int mode_flags; /* mode flags for opening fds */ }; @@ -671,6 +674,7 @@ PHP_FUNCTION(proc_open) #ifdef PHP_WIN32 descriptors[ndesc].childend = (HANDLE)_get_osfhandle(fd); + descriptors[ndesc].childend_desc = fd; #else descriptors[ndesc].childend = fd; #endif @@ -901,6 +905,11 @@ PHP_FUNCTION(proc_open) char *mode_string=NULL; php_stream *stream = NULL; +#ifdef PHP_WIN32 + if (descriptors[i].childend_desc) + _close(descriptors[i].childend_desc); /* closes also the handle */ +#endif + close_descriptor(descriptors[i].childend); switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) { I cannot check this, since I haven't succeeded in setting up a build environment on MS Windows for PHP sources.Alternative (simplier) patch: --- ext/standard/proc_open.c.orig 2006-06-01 18:09:51.000000000 +0200 +++ ext/standard/proc_open.c @@ -670,7 +670,10 @@ PHP_FUNCTION(proc_open) } #ifdef PHP_WIN32 - descriptors[ndesc].childend = (HANDLE)_get_osfhandle(fd); + /* dup the handle, and close the original descriptor to + * avoid leak */ + descriptors[ndesc].childend = dup_fd_as_handle(fd); + _close(fd); #else descriptors[ndesc].childend = fd; #endif