#30743 [Opn->Csd]: proc_open() / proc_close() leak handles on Windows XP
| From: | tony2001@php.net | Date: | Thu, 11 Nov 2004 06:28:24 +0000 |
| Subject: | #30743 [Opn->Csd]: proc_open() / proc_close() leak handles on Windows XP | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-68939@lists.php.net to get a copy of this message | ||
ID: 30743
Updated by: tony2001@php.net
Reported By: ilya77 at gmail dot com
-Status: Open
+Status: Closed
Bug Type: Program Execution
Operating System: Windows XP
PHP Version: 5.0.2
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Fixed in CVS by Wez.
Previous Comments:
------------------------------------------------------------------------
[2004-11-10 16:17:26] ilya77 at gmail dot com
!!!!SOLUTION!!!!
PHP_FUNCTION(proc_close) missed a call to CloseHandle(proc->child), to
close the process handle.
This is what caused the handle leak.
Here's the code:
------------- cut here -------------
/* {{{ proto int proc_close(resource process)
close a process opened by proc_open */
PHP_FUNCTION(proc_close)
{
zval *zproc;
struct php_process_handle *proc;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zproc) ==
FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, &zproc, -1,
"process", le_proc_open);
CloseHandle(proc->child); // ilya.1.0 20041110
zend_list_delete(Z_LVAL_P(zproc));
RETURN_LONG(FG(pclose_ret));
}
/* }}} */
------------- cut here -------------
------------------------------------------------------------------------
[2004-11-10 09:43:27] ilya77 at gmail dot com
Description:
------------
I have a scheduler script, which constantly runs in the background,
invokes processes, and pipes their output back to stdout.
It seems that proc_open() or proc_close() call leaks handles on Windows
platform (I'm running PHP 5.0.2).
In the example code, the handles count would increment by 1 each time
invoke() is called, in other places I saw similar code leak 4 handles
at once during each iteration.
Reproduce code:
---------------
while(1)
{
echo("---\n");
invoke();
sleep(5);
}
function invoke()
{
$commandLine = "echo hello";
$fileDescriptors = array(
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
$processHandle = proc_open($commandLine,
$fileDescriptors, $pipes);
if (is_resource($processHandle))
{
foreach($pipes as $pipeID => $pipeHandle)
fclose($pipeHandle);
proc_close($processHandle);
}
}
Expected result:
----------------
I expected the handle count to increment by X, and decrement by X after
the process completes.
(where it's logical to assume that X = number of pipes + 1 for the
child process handle)
Actual result:
--------------
In fact, the handles count (in windows task manager) for the php.exe
process running the script incremented by 1 each time invoke() in the
example code was executed.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30743&edit=1