#30743 [Opn]: proc_open() / proc_close() leak handles on Windows XP

From: Date: Wed, 10 Nov 2004 15:17:29 +0000
Subject: #30743 [Opn]: proc_open() / proc_close() leak handles on Windows XP
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-68896@lists.php.net to get a copy of this message
 ID:               30743
 User updated by:  ilya77 at gmail dot com
 Reported By:      ilya77 at gmail dot com
 Status:           Open
 Bug Type:         Program Execution
 Operating System: Windows XP
 PHP Version:      5.0.2
 New Comment:

!!!!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 -------------


Previous Comments:
------------------------------------------------------------------------

[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


Thread (3 messages)

« previous php.bugs (#68896) next »