Fclose (): 18 is not a valid thread resource

I am trying to execute a process using proc_open. I / O for the process is processed using pipes!

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w")
);

Now, as it happens, sometimes the "c-program" opens, which I opened, and I added the max_time_limit check, which will force the process to terminate. I added a callback function - namely onExit - (using "call_user_function") to process the information whenever the "process exits" in a real way or by force.

In the function "exit" I close the input / output channels

 foreach ($pipes as $pipe) {
    fclose($pipe);
 }

The above works great if the process has a valid output. However, in the case when I forcibly killed my process, I called proc_terminate to do this. I understand that the termination of the process will also continue and close any I / O channels, but the problem is that due to the callback, my onExit function process is always called (and so I want it to be ... I need do some more processing). Now, in this case, when I try to close the pipes, I get the following error:

fclose(): 18 is not a valid stream resource 

I tried using "ftell" to check if the channel is valid or not, but this is too wrong. How to check if pipes have already been closed or not?

+5
source share
2 answers

, , :

foreach ($pipes as $pipe) {
  // Finds whether a variable is a resource
  if(is_resource($pipe)) {
    fclose($pipe);
  }
}
+12

, , "" @. .

@fclose($pipe);
-1

All Articles