php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30743 proc_open() / proc_close() leak handles on Windows XP
Submitted: 2004-11-10 09:43 UTC Modified: 2004-11-11 07:28 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ilya77 at gmail dot com Assigned:
Status: Closed Package: Program Execution
PHP Version: 5.0.2 OS: Windows XP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ilya77 at gmail dot com
New email:
PHP Version: OS:

 

 [2004-11-10 09:43 UTC] 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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-10 16:17 UTC] 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-11 07:28 UTC] tony2001@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 13:01:30 2024 UTC