php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35131 stream_select() in conjuntion with popen() does not appear to work
Submitted: 2005-11-06 22:07 UTC Modified: 2008-03-16 01:00 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:1 (33.3%)
From: larryjadams at comcast dot net Assigned:
Status: No Feedback Package: Streams related
PHP Version: 5CVS-2007-07-17 OS: WindowsXP Pro SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: larryjadams at comcast dot net
New email:
PHP Version: OS:

 

 [2005-11-06 22:07 UTC] larryjadams at comcast dot net
Description:
------------
If I call $my_fd = popen("mycommand", "rb"); to a function that hangs/suspends and then subsquently call:

stream_select(...) using $read=array($my_fd) with a timeout, the steam_select returns immediately, event if I have set $my_fd as blocking using stream_set_blocking();

I would like stream_select to wait on output from the pipe prior to firing, or to drive a timeout.

Thanks,

Larry Adams
The Cacti Group

Reproduce code:
---------------
if (function_exists("stream_select")) {
  if ($config["cacti_server_os"] == "unix")  {
    $fp = popen($command, "r");
  }else{
    $fp = popen($command, "rb");
  }

  /* set script server timeout */
  $script_timeout = read_config_option("script_timeout");

  /* establish timeout variables */
  $to_sec = floor($script_timeout/1000);
  $to_usec = ($script_timeout%1000)*1000;

  /* Prepare the read array */
  $read = array($fp);

  stream_set_blocking ($fp, 1);
  if (false === ($num_changed_streams = stream_select($read, $write = NULL, $except = NULL, $to_sec, $to_usec))) {
    cacti_log("WARNING: SERVER POPEN Timed out.");
    $output = "U";
  }elseif ($num_changed_streams > 0) {
    $output = fgets($fp, 4096);
  }

  pclose($fp);
}else{
  $output = `$command`;
}

$command = a file with the following:

<?php sleep(20); echo "Hello:20.00\n"; ?>

The stream_select timeout is 5 seconds.


Expected result:
----------------
popen will be called, stream_select will block until timeout has expired, the number of fd's changed will be 0 and my result will be as expected.

Actual result:
--------------
stream_select returns immediately.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-18 13:53 UTC] larryjadams at comcast dot net
The set block option now works.  However, the stream select still waits for the script to return or for ever, whichever comes first :).  Here is the revised code.  Simply place test1.php and test2.php in the c:\ directory.

test1.php
<?php
$command = "php -q c:/test2.php";

if (function_exists("stream_select")) {
	$fp = popen($command, "rb");

	/* set script server timeout in milliseconds */
	$script_timeout = 5000;

	/* establish timeout variables */
	$to_sec = floor($script_timeout/1000);
	$to_usec = ($script_timeout%1000)*1000;

	/* Prepare the read array */
	$read = array($fp);

	stream_set_blocking ($fp, 1);
	if (false === ($num_changed_streams = stream_select($read, $write = NULL, $except = NULL, $to_sec, $to_usec))) {
		echo "WARNING: SERVER POPEN Timed out.\n";
		$output = "U";
	}elseif ($num_changed_streams > 0) {
		$output = fgets($fp, 4096);
		echo "Command Exited with a Result of '" . trim($output) . "'\n";
	}

	pclose($fp);
}else{
	$output = `$command`;
}
?>

test2.php
<?php sleep(20);echo "Ehllo\n"; ?>

TheWitness
 [2007-07-18 14:59 UTC] jani@php.net
I didn't try it on Windows myself, I'll test this later on *nix.
 [2008-01-26 01:10 UTC] jani@php.net
I have no interest in fixing Windows bugs, there's enough work with the real OSes.
 [2008-03-16 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Mar 11 07:01:30 2025 UTC